Modify khashmir's config system to not use the const module.
[quix0rs-apt-p2p.git] / apt_dht_Khashmir / test_krpc.py
1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
3
4 from unittest import defaultTestLoader, TestCase, TextTestRunner
5 import sys
6
7 from twisted.internet import protocol
8 from twisted.internet import reactor
9
10 from krpc import KRPC, hostbroker, KRPC_ERROR_METHOD_UNKNOWN
11
12 KRPC.noisy = 0
13
14 if __name__ =="__main__":
15     tests = defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]])
16     result = TextTestRunner().run(tests)
17
18
19 def connectionForAddr(host, port):
20     return host
21     
22
23 class Receiver(protocol.Factory):
24     protocol = KRPC
25     def __init__(self):
26         self.buf = []
27     def krpc_store(self, msg, _krpc_sender):
28         self.buf += [msg]
29     def krpc_echo(self, msg, _krpc_sender):
30         return msg
31
32 def make(port):
33     af = Receiver()
34     a = hostbroker(af)
35     a.protocol = KRPC
36     p = reactor.listenUDP(port, a)
37     return af, a, p
38     
39 class KRPCTests(TestCase):
40     def setUp(self):
41         self.noisy = 0
42         self.af, self.a, self.ap = make(1180)
43         self.bf, self.b, self.bp = make(1181)
44
45     def tearDown(self):
46         self.ap.stopListening()
47         self.bp.stopListening()
48         reactor.iterate()
49         reactor.iterate()
50
51     def testSimpleMessage(self):
52         self.noisy = 0
53         self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('store', {'msg' : "This is a test."})
54         reactor.iterate()
55         reactor.iterate()
56         reactor.iterate()
57         self.assertEqual(self.bf.buf, ["This is a test."])
58
59     def testMessageBlast(self):
60         self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('store', {'msg' : "This is a test."})
61         reactor.iterate()
62         reactor.iterate()
63         reactor.iterate()
64         self.assertEqual(self.bf.buf, ["This is a test."])
65         self.bf.buf = []
66         
67         for i in range(100):
68             self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('store', {'msg' : "This is a test."})
69             reactor.iterate()
70             #self.bf.buf = []
71         self.assertEqual(self.bf.buf, ["This is a test."] * 100)
72
73     def testEcho(self):
74         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
75         df.addCallback(self.gotMsg)
76         reactor.iterate()
77         reactor.iterate()
78         reactor.iterate()
79         reactor.iterate()
80         self.assertEqual(self.msg, "This is a test.")
81
82     def gotMsg(self, dict):
83         _krpc_sender = dict['_krpc_sender']
84         msg = dict['rsp']
85         self.msg = msg
86
87     def testManyEcho(self):
88         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
89         df.addCallback(self.gotMsg)
90         reactor.iterate()
91         reactor.iterate()
92         reactor.iterate()
93         reactor.iterate()
94         self.assertEqual(self.msg, "This is a test.")
95         for i in xrange(100):
96             self.msg = None
97             df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
98             df.addCallback(self.gotMsg)
99             reactor.iterate()
100             reactor.iterate()
101             reactor.iterate()
102             reactor.iterate()
103             self.assertEqual(self.msg, "This is a test.")
104
105     def testMultiEcho(self):
106         self.noisy = 1
107         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
108         df.addCallback(self.gotMsg)
109         reactor.iterate()
110         reactor.iterate()
111         reactor.iterate()
112         reactor.iterate()
113         self.assertEqual(self.msg, "This is a test.")
114
115         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is another test."})
116         df.addCallback(self.gotMsg)
117         reactor.iterate()
118         reactor.iterate()
119         reactor.iterate()
120         reactor.iterate()
121         self.assertEqual(self.msg, "This is another test.")
122
123         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is yet another test."})
124         df.addCallback(self.gotMsg)
125         reactor.iterate()
126         reactor.iterate()
127         reactor.iterate()
128         reactor.iterate()
129         self.assertEqual(self.msg, "This is yet another test.")
130
131     def testEchoReset(self):
132         self.noisy = 1
133         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is a test."})
134         df.addCallback(self.gotMsg)
135         reactor.iterate()
136         reactor.iterate()
137         reactor.iterate()
138         reactor.iterate()
139         self.assertEqual(self.msg, "This is a test.")
140
141         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is another test."})
142         df.addCallback(self.gotMsg)
143         reactor.iterate()
144         reactor.iterate()
145         reactor.iterate()
146         reactor.iterate()
147         self.assertEqual(self.msg, "This is another test.")
148
149         del(self.a.connections[('127.0.0.1', 1181)])
150         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('echo', {'msg' : "This is yet another test."})
151         df.addCallback(self.gotMsg)
152         reactor.iterate()
153         reactor.iterate()
154         reactor.iterate()
155         reactor.iterate()
156         self.assertEqual(self.msg, "This is yet another test.")
157
158     def testLotsofEchoReset(self):
159         for i in range(100):
160             self.testEchoReset()
161             
162     def testUnknownMeth(self):
163         self.noisy = 1
164         df = self.a.connectionForAddr(('127.0.0.1', 1181)).sendRequest('blahblah', {'msg' : "This is a test."})
165         df.addErrback(self.gotErr)
166         reactor.iterate()
167         reactor.iterate()
168         reactor.iterate()
169         reactor.iterate()
170         self.assertEqual(self.err, KRPC_ERROR_METHOD_UNKNOWN)
171
172     def gotErr(self, err):
173         self.err = err.value
174