def setUp(self): self.ksimple = KRPC_Simple(TEST_NODE_ID) self.ksimple.transport = HollowTransport() self.ksimple._reactor = HollowReactor()
class KRPC_Simple_TestCase(unittest.TestCase): def setUp(self): self.ksimple = KRPC_Simple(TEST_NODE_ID) self.ksimple.transport = HollowTransport() self.ksimple._reactor = HollowReactor() def test_get_liveResultGrowsWithSingleResponse(self): seed_node = self._init_seed_node() live_search = self.ksimple.get(890) self.assertTrue(seed_node in live_search.queried_nodes) query = self._grab_outbound_get_peers() result_peer = test_nodes[33].address response = query.build_response(peers=[result_peer]) responding_node = self._encode_and_respond(response) results = live_search.get_results() self.assertEquals(1, len(results)) self.assertTrue(live_search.queried_nodes) for result in results: self.assertEquals(result_peer, result) break self.assertTrue(live_search.is_complete) def test_get_protoSendsAnotherGetPeersQueryOnResponseWithNodes(self): seed_node = self._init_seed_node() live_search = self.ksimple.get(890) self.assertTrue(seed_node in live_search.queried_nodes) first_query = self._grab_outbound_get_peers() # any address tuple will do as a result_peer result_node = test_nodes[1] response = first_query.build_response(nodes=[result_node]) self.assertEquals(0, len(live_search.get_results())) self._encode_and_respond(response) self.assertFalse(live_search.is_complete) self.assertTrue(result_node in live_search.queried_nodes) second_query = self._grab_outbound_get_peers() self.assertNotEquals(second_query, first_query) self.assertFalse(live_search.is_complete) def test_get_liveResultDoesntGrowWithError(self): seed_node = self._init_seed_node() live_search = self.ksimple.get(TEST_TARGET_ID) self.assertTrue(seed_node in live_search.queried_nodes) query = self._grab_outbound_get_peers() error = query.build_error() self._encode_and_respond(error) results = live_search.get_results() self.assertEquals(0, len(results)) def test_get_completionWithNoPeers(self): live_search = self.ksimple.get(TEST_TARGET_ID) self.assertEquals(0, len(live_search.get_results())) self.assertTrue(live_search.is_complete) def test_put(self): self.assertTrue(False) def _encode_and_respond(self, krpc): responding_node = test_nodes[22] krpc._from = responding_node.node_id encoded_krpc = krpc_coder.encode(krpc) self.ksimple.datagramReceived(encoded_krpc, responding_node.address) return responding_node def _init_seed_node(self): # prepare proto for search seed_node = test_nodes[0] node_accepted = self.ksimple.routing_table.offer_node(seed_node) self.assertTrue(node_accepted) return seed_node def _grab_outbound_get_peers(self): encoded_query = self.ksimple.transport.packet self.ksimple.transport._reset() self.assertNotEquals(None, encoded_query) query = krpc_coder.decode(encoded_query) self.assertEquals("get_peers", query.rpctype) return query