def test_handleFoundValues(self): self._connecting_to_connected() self.wire_protocol[self.addr1] = self.con self.protocol.router.addContact(self.node1) self.protocol.router.addContact(self.node2) self.protocol.router.addContact(self.node3) node = Node(digest("s")) nearest = self.protocol.router.findNeighbors(node) spider = ValueSpiderCrawl(self.protocol, node, nearest, 20, 3) val = Value() val.valueKey = digest("contractID") val.serializedData = self.node1.getProto().SerializeToString() val.ttl = 10 val1 = val.SerializeToString() value = spider._handleFoundValues([val1]) self.assertEqual(value[0], val.SerializeToString()) # test handle multiple values val.serializedData = self.node2.getProto().SerializeToString() val2 = val.SerializeToString() val.valueKey = digest("contractID2") val3 = val.SerializeToString() found_values = [val1, val2, val2, val3] self.assertEqual(spider._handleFoundValues(found_values), [val3, val2]) # test store value at nearest without value spider.nearestWithoutValue.push(self.node1) spider._handleFoundValues(found_values) self.clock.advance(constants.PACKET_TIMEOUT) connection.REACTOR.runUntilCurrent() self.assertTrue(len(self.proto_mock.send_datagram.call_args_list) > 1) self.proto_mock.send_datagram.call_args_list = []
def test_handleFoundValues(self): self._connecting_to_connected() self.wire_protocol[self.addr1] = self.con self.protocol.router.addContact(self.node1) self.protocol.router.addContact(self.node2) self.protocol.router.addContact(self.node3) node = Node(digest("s")) nearest = self.protocol.router.findNeighbors(node) spider = ValueSpiderCrawl(self.protocol, node, nearest, dht.constants.KSIZE, dht.constants.ALPHA) val = Value() val.valueKey = digest("contractID") val.serializedData = self.node1.getProto().SerializeToString() val1 = val.SerializeToString() value = spider._handleFoundValues([(val1, )]) self.assertEqual(value[0], val.SerializeToString()) # test handle multiple values val.serializedData = self.node2.getProto().SerializeToString() val2 = val.SerializeToString() found_values = [(val1, ), (val1, ), (val2, )] self.assertEqual(spider._handleFoundValues(found_values), (val1, )) # test store value at nearest without value spider.nearestWithoutValue.push(self.node1) spider._handleFoundValues(found_values) self.clock.advance(100 * constants.PACKET_TIMEOUT) connection.REACTOR.runUntilCurrent() self.assertTrue(len(self.proto_mock.send_datagram.call_args_list) > 1) self.proto_mock.send_datagram.call_args_list = []
def test_get(self): v = Value() v.valueKey = self.key1 v.serializedData = self.value testv = [v.SerializeToString()] p = PersistentStorage(":memory:") p[self.keyword1] = (self.key1, self.value) self.assertEqual(testv, p.get(self.keyword1))
def test_get(self): v = Value() v.valueKey = self.key1 v.serializedData = self.value testv = [v.SerializeToString()] f = ForgetfulStorage() f[self.keyword1] = (self.key1, self.value) self.assertEqual(testv, f.get(self.keyword1))
def test_get(self): v = Value() v.valueKey = self.key1 v.serializedData = self.value v.ttl = 10 testv = [v.SerializeToString()] p = ForgetfulStorage() p[self.keyword1] = (self.key1, self.value, 10) self.assertEqual(testv, p.get(self.keyword1))
def test_nodesFound(self): self._connecting_to_connected() self.wire_protocol[self.addr1] = self.con self.wire_protocol[self.addr2] = self.con self.wire_protocol[self.addr3] = self.con self.protocol.router.addContact(self.node1) self.protocol.router.addContact(self.node2) self.protocol.router.addContact(self.node3) # test resonse with uncontacted nodes node = Node(digest("s")) nearest = self.protocol.router.findNeighbors(node) spider = ValueSpiderCrawl(self.protocol, node, nearest, dht.constants.KSIZE, dht.constants.ALPHA) response = (True, (self.node1.getProto().SerializeToString(), self.node2.getProto().SerializeToString(), self.node3.getProto().SerializeToString())) responses = {self.node1.id: response} spider._nodesFound(responses) self.clock.advance(100 * constants.PACKET_TIMEOUT) connection.REACTOR.runUntilCurrent() self.assertEqual(len(self.proto_mock.send_datagram.call_args_list), 4) # test all been contacted spider = ValueSpiderCrawl(self.protocol, node, nearest, dht.constants.KSIZE, dht.constants.ALPHA) for peer in spider.nearest.getUncontacted(): spider.nearest.markContacted(peer) response = (True, (self.node1.getProto().SerializeToString(), self.node2.getProto().SerializeToString(), self.node3.getProto().SerializeToString())) responses = {self.node2.id: response} resp = spider._nodesFound(responses) self.assertTrue(resp is None) # test didn't happen spider = ValueSpiderCrawl(self.protocol, node, nearest, dht.constants.KSIZE, dht.constants.ALPHA) response = (False, (self.node1.getProto().SerializeToString(), self.node2.getProto().SerializeToString(), self.node3.getProto().SerializeToString())) responses = {self.node1.id: response} spider._nodesFound(responses) self.assertTrue(len(spider.nearest) == 2) # test got value val = Value() val.valueKey = digest("contractID") val.serializedData = self.protocol.sourceNode.getProto( ).SerializeToString() response = (True, ("value", val.SerializeToString())) responses = {self.node3.id: response} spider.nearestWithoutValue = NodeHeap(node, 1) value = spider._nodesFound(responses) self.assertEqual(value[0], val.SerializeToString())
def get(self, keyword, default=None): self.cull() if keyword in self.data: ret = [] for k, v in self[keyword].items(): value = Value() value.valueKey = k value.serializedData = v ret.append(value.SerializeToString()) return ret return default
def get(self, keyword, default=None): self.cull() if len(self[keyword]) > 0: ret = [] for k, v, birthday in self[keyword]: value = Value() value.valueKey = k value.serializedData = v value.ttl = int(round(self.ttl - (time.time() - birthday))) ret.append(value.SerializeToString()) return ret return default
def test_nodesFound(self): self._connecting_to_connected() self.wire_protocol[self.addr1] = self.con self.wire_protocol[self.addr2] = self.con self.wire_protocol[self.addr3] = self.con self.protocol.router.addContact(self.node1) self.protocol.router.addContact(self.node2) self.protocol.router.addContact(self.node3) # test response with uncontacted nodes node = Node(digest("s")) nearest = self.protocol.router.findNeighbors(node) spider = ValueSpiderCrawl(self.protocol, node, nearest, 20, 3) response = (True, (self.node1.getProto().SerializeToString(), self.node2.getProto().SerializeToString(), self.node3.getProto().SerializeToString())) responses = {self.node1.id: response} spider._nodesFound(responses) self.clock.advance(constants.PACKET_TIMEOUT) connection.REACTOR.runUntilCurrent() self.assertEqual(len(self.proto_mock.send_datagram.call_args_list), 4) # test all been contacted spider = ValueSpiderCrawl(self.protocol, node, nearest, 20, 3) for peer in spider.nearest.getUncontacted(): spider.nearest.markContacted(peer) response = (True, (self.node1.getProto().SerializeToString(), self.node2.getProto().SerializeToString(), self.node3.getProto().SerializeToString())) responses = {self.node2.id: response} resp = spider._nodesFound(responses) self.assertTrue(resp is None) # test didn't happen spider = ValueSpiderCrawl(self.protocol, node, nearest, 20, 3) response = (False, (self.node1.getProto().SerializeToString(), self.node2.getProto().SerializeToString(), self.node3.getProto().SerializeToString())) responses = {self.node1.id: response} spider._nodesFound(responses) self.assertTrue(len(spider.nearest) == 2) # test got value val = Value() val.valueKey = digest("contractID") val.serializedData = self.protocol.sourceNode.getProto().SerializeToString() val.ttl = 10 response = (True, ("value", val.SerializeToString())) responses = {self.node3.id: response} spider.nearestWithoutValue = NodeHeap(node, 1) value = spider._nodesFound(responses) self.assertEqual(value[0], val.SerializeToString())