class QueryTestCase(unittest.TestCase): def setUp(self): self.q = Query() self.q._transaction_id = 500 self.q._from = 27 self.q.rpctype = "ping" def test_build_response(self): nodes = None token = 8192 peers = None r = self.q.build_response(nodes, token, peers) q = self.q expected_r = Response(_transaction_id=q._transaction_id, rpctype=q.rpctype, nodes=nodes, token=token, peers=peers) self.assertEquals(expected_r, r) def test_build_error(self): code = 203 message = "Oops, error" e = self.q.build_error(code, message) q = self.q expected_e = Error(_transaction_id=q._transaction_id, code=code, message=message) self.assertEquals(expected_e, e) def test_repr(self): expected_repr = "<Query: _transaction_id=500 rpctype=ping _from=27>" self.assertEquals(expected_repr, repr(self.q)) def test__eq__(self): q1, q2 = self._gen_equal_announce_peers() self.assertEquals(q1, q2) def test__ne___(self): q1, q2 = self._gen_equal_announce_peers() q1._transaction_id = 88 q2._transaction_id = 66 self.assertNotEquals(q1, q2) def _gen_equal_announce_peers(self): q1 = Query() q2 = Query() q1._transaction_id = q2._transaction_id = 99 q1._from = q2._from = 55 q1.rpctype = q2.rpctype = "announce_peer" q1.token = q2.token = 13 q1.port = q2.port = 123 q1.target_id = q2.target_id = 66 self.assertEquals(q1, q2) return (q1, q2)
class KRPC_Sender_DeferredTestCase(unittest.TestCase): def setUp(self): _swap_out_reactor() self.k_messenger = KRPC_Sender(TreeRoutingTable, 2**50) self.k_messenger.transport = HollowTransport() self.query = Query() self.query.rpctype = "ping" def tearDown(self): _restore_reactor() def _response_equality(self, response, expected_response): self.assertEquals(expected_response._transaction_id, response._transaction_id) self.assertEquals(expected_response._from, response._from) return response def test_callback(self): counter = Counter() d = self.k_messenger.sendQuery(self.query, address, timeout) self.assertTrue(self.query._transaction_id in self.k_messenger._transactions) # Build the response we will "receive" response = self.query.build_response() response._from = 9 d.addCallback(self._response_equality, response) d.addCallback(counter) encoded_response = krpc_coder.encode(response) self.k_messenger.datagramReceived(encoded_response, address) self.assertEquals(1, counter.count) self.assertFalse(self.query._transaction_id in self.k_messenger._transactions) def _error_equality(self, error, expected_error): self.assertEquals(expected_error._transaction_id, error._transaction_id) self.assertEquals(expected_error.code, error.code) return error def test_errback_KRPCError(self): counter = Counter() d = self.k_messenger.sendQuery(self.query, address, timeout) self.assertTrue(self.query._transaction_id in self.k_messenger._transactions) # Build the response we will "receive" error = self.query.build_error() d.addErrback(self._error_equality, error) d.addErrback(counter) encoded_error = krpc_coder.encode(error) self.k_messenger.datagramReceived(encoded_error, address) self.assertEquals(1, counter.count) self.assertFalse(self.query._transaction_id in self.k_messenger._transactions) def test_errback_InvalidKRPCError(self): # Make an invalid query query = Query() query.rpctype = "pingpong" d = self.k_messenger.sendQuery(query, address, timeout) self.assertFalse(self.query._transaction_id in self.k_messenger._transactions) # Cleanup the error d.addErrback(lambda failure: failure.trap(krpc_coder.InvalidKRPCError)) def test_errback_TimeoutError(self): d = self.k_messenger.sendQuery(self.query, address, timeout) self.assertTrue(self.query._transaction_id in self.k_messenger._transactions) d.errback(TimeoutError()) self.assertFalse(self.query._transaction_id in self.k_messenger._transactions) # Cleanup the error d.addErrback(lambda failure: failure.trap(TimeoutError))
class KRPC_Sender_DeferredTestCase(unittest.TestCase): def setUp(self): _swap_out_reactor() self.k_messenger = KRPC_Sender(TreeRoutingTable, 2**50) self.k_messenger.transport = HollowTransport() self.query = Query() self.query.rpctype = "ping" def tearDown(self): _restore_reactor() def _response_equality(self, response, expected_response): self.assertEquals(expected_response._transaction_id, response._transaction_id) self.assertEquals(expected_response._from, response._from) return response def test_callback(self): counter = Counter() d = self.k_messenger.sendQuery(self.query, address, timeout) self.assertTrue( self.query._transaction_id in self.k_messenger._transactions) # Build the response we will "receive" response = self.query.build_response() response._from = 9 d.addCallback(self._response_equality, response) d.addCallback(counter) encoded_response = krpc_coder.encode(response) self.k_messenger.datagramReceived(encoded_response, address) self.assertEquals(1, counter.count) self.assertFalse( self.query._transaction_id in self.k_messenger._transactions) def _error_equality(self, error, expected_error): self.assertEquals(expected_error._transaction_id, error._transaction_id) self.assertEquals(expected_error.code, error.code) return error def test_errback_KRPCError(self): counter = Counter() d = self.k_messenger.sendQuery(self.query, address, timeout) self.assertTrue( self.query._transaction_id in self.k_messenger._transactions) # Build the response we will "receive" error = self.query.build_error() d.addErrback(self._error_equality, error) d.addErrback(counter) encoded_error = krpc_coder.encode(error) self.k_messenger.datagramReceived(encoded_error, address) self.assertEquals(1, counter.count) self.assertFalse( self.query._transaction_id in self.k_messenger._transactions) def test_errback_InvalidKRPCError(self): # Make an invalid query query = Query() query.rpctype = "pingpong" d = self.k_messenger.sendQuery(query, address, timeout) self.assertFalse( self.query._transaction_id in self.k_messenger._transactions) # Cleanup the error d.addErrback(lambda failure: failure.trap(krpc_coder.InvalidKRPCError)) def test_errback_TimeoutError(self): d = self.k_messenger.sendQuery(self.query, address, timeout) self.assertTrue( self.query._transaction_id in self.k_messenger._transactions) d.errback(TimeoutError()) self.assertFalse( self.query._transaction_id in self.k_messenger._transactions) # Cleanup the error d.addErrback(lambda failure: failure.trap(TimeoutError))