1 ## Copyright 2002-2003 Andrew Loewenstern, All Rights Reserved
2 # see LICENSE.txt for license information
12 if __name__ =="__main__":
13 tests = unittest.defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]])
14 result = unittest.TextTestRunner().run(tests)
17 def connectionForAddr(host, port):
20 class Receiver(protocol.Factory):
24 def krpc_store(self, msg, _krpc_sender):
26 def krpc_echo(self, msg, _krpc_sender):
29 class SimpleTest(TestCase):
35 self.a = listenAirhookStream(4040, self.af)
36 self.b = listenAirhookStream(4041, self.bf)
38 def testSimpleMessage(self):
40 self.a.connectionForAddr(('127.0.0.1', 4041)).protocol.sendRequest('store', {'msg' : "This is a test."})
44 self.assertEqual(self.bf.buf, ["This is a test."])
46 class SimpleTest(TestCase):
52 self.a = listenAirhookStream(4050, self.af)
53 self.b = listenAirhookStream(4051, self.bf)
55 def testSimpleMessage(self):
57 self.a.connectionForAddr(('127.0.0.1', 4051)).protocol.sendRequest('store', {'msg' : "This is a test."})
61 self.assertEqual(self.bf.buf, ["This is a test."])
63 class BlastTest(TestCase):
69 self.a = listenAirhookStream(4060, self.af)
70 self.b = listenAirhookStream(4061, self.bf)
72 def testMessageBlast(self):
73 self.a.connectionForAddr(('127.0.0.1', 4061)).protocol.sendRequest('store', {'msg' : "This is a test."})
77 self.assertEqual(self.bf.buf, ["This is a test."])
81 self.a.connectionForAddr(('127.0.0.1', 4061)).protocol.sendRequest('store', {'msg' : "This is a test."})
84 self.assertEqual(self.bf.buf, ["This is a test."] * 100)
86 class EchoTest(TestCase):
93 self.a = listenAirhookStream(4042, self.af)
94 self.b = listenAirhookStream(4043, self.bf)
97 df = self.a.connectionForAddr(('127.0.0.1', 4043)).protocol.sendRequest('echo', {'msg' : "This is a test."})
98 df.addCallback(self.gotMsg)
103 self.assertEqual(self.msg, "This is a test.")
105 def gotMsg(self, dict):
106 _krpc_sender = dict['_krpc_sender']
110 class ManyEchoTest(TestCase):
117 self.a = listenAirhookStream(4588, self.af)
118 self.b = listenAirhookStream(4589, self.bf)
120 def testManyEcho(self):
121 df = self.a.connectionForAddr(('127.0.0.1', 4589)).protocol.sendRequest('echo', {'msg' : "This is a test."})
122 df.addCallback(self.gotMsg)
127 self.assertEqual(self.msg, "This is a test.")
128 for i in xrange(100):
130 df = self.a.connectionForAddr(('127.0.0.1', 4589)).protocol.sendRequest('echo', {'msg' : "This is a test."})
131 df.addCallback(self.gotMsg)
136 self.assertEqual(self.msg, "This is a test.")
138 def gotMsg(self, dict):
139 _krpc_sender = dict['_krpc_sender']
143 class MultiEchoTest(TestCase):
150 self.a = listenAirhookStream(4048, self.af)
151 self.b = listenAirhookStream(4049, self.bf)
153 def testMultiEcho(self):
155 df = self.a.connectionForAddr(('127.0.0.1', 4049)).protocol.sendRequest('echo', {'msg' : "This is a test."})
156 df.addCallback(self.gotMsg)
161 self.assertEqual(self.msg, "This is a test.")
163 df = self.a.connectionForAddr(('127.0.0.1', 4049)).protocol.sendRequest('echo', {'msg' : "This is another test."})
164 df.addCallback(self.gotMsg)
169 self.assertEqual(self.msg, "This is another test.")
171 df = self.a.connectionForAddr(('127.0.0.1', 4049)).protocol.sendRequest('echo', {'msg' : "This is yet another test."})
172 df.addCallback(self.gotMsg)
177 self.assertEqual(self.msg, "This is yet another test.")
179 def gotMsg(self, dict):
180 _krpc_sender = dict['_krpc_sender']
184 class EchoResetTest(TestCase):
191 self.a = listenAirhookStream(4078, self.af)
192 self.b = listenAirhookStream(4079, self.bf)
194 def testEchoReset(self):
196 df = self.a.connectionForAddr(('127.0.0.1', 4079)).protocol.sendRequest('echo', {'msg' : "This is a test."})
197 df.addCallback(self.gotMsg)
202 self.assertEqual(self.msg, "This is a test.")
204 df = self.a.connectionForAddr(('127.0.0.1', 4079)).protocol.sendRequest('echo', {'msg' : "This is another test."})
205 df.addCallback(self.gotMsg)
210 self.assertEqual(self.msg, "This is another test.")
212 del(self.a.connections[('127.0.0.1', 4079)])
213 df = self.a.connectionForAddr(('127.0.0.1', 4079)).protocol.sendRequest('echo', {'msg' : "This is yet another test."})
214 df.addCallback(self.gotMsg)
219 self.assertEqual(self.msg, "This is yet another test.")
221 def testLotsofEchoReset(self):
224 def gotMsg(self, dict):
225 _krpc_sender = dict['_krpc_sender']
229 class UnknownMethErrTest(TestCase):
235 self.a = listenAirhookStream(4044, self.af)
236 self.b = listenAirhookStream(4045, self.bf)
238 def testUnknownMeth(self):
240 df = self.a.connectionForAddr(('127.0.0.1', 4045)).protocol.sendRequest('blahblah', {'msg' : "This is a test."})
241 df.addErrback(self.gotErr)
246 self.assertEqual(self.err, KRPC_ERROR_METHOD_UNKNOWN)
248 def gotErr(self, err):