def _patch_counter_and_input_krpc(self, krpc, method_name, num_calls=1, address=None): if address is None: address = ("127.0.0.1", 8888) k_messenger = KRPC_Sender(TreeRoutingTable, 2**50) # Patch in our counter counter = Counter() setattr(k_messenger, method_name, counter) # Pass in the krpc k_messenger.datagramReceived(krpc_coder.encode(krpc), address) self.assertEquals(num_calls, counter.count)
def __init__(self, routing_table_class=TreeRoutingTable, node_id=None, _reactor=None): if node_id is None: node_id = random.getrandbits(constants.id_size) # Verify the node_id is valid basic_coder.encode_network_id(node_id) KRPC_Sender.__init__(self, routing_table_class, node_id, _reactor) # Datastore is used for storing peers on torrents self._datastore = defaultdict(set) self._token_generator = _TokenGenerator()
def test_responseReceived(self): # Make a query that we will "send" query = Query() query.rpctype = "ping" # Make the protocol and patch in our counter, transport, and reactor counter = Counter() k_messenger = KRPC_Sender(TreeRoutingTable, 2**50) k_messenger.transport = HollowTransport() k_messenger.responseReceived = counter # Send the query and receive the response k_messenger.sendQuery(query, address, timeout) self.assertTrue(query._transaction_id in k_messenger._transactions) # Make a response that we will "receive" response = query.build_response() response._from = 9 k_messenger.datagramReceived(krpc_coder.encode(response), address) _restore_reactor() self.assertEquals(1, counter.count)
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"
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))