Beispiel #1
0
 def test_handle_response_all_shortlist_contacted_value_not_found(self):
     """
     If there are no more pending requests and all the nodes in the
     shortlist have been contacted then return the shortlist of nearest
     peer nodes to the target if the lookup is a FindNode.
     """
     lookup = Lookup(FindValue, self.target, self.node, self.event_loop)
     lookup._lookup = mock.MagicMock()
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     # Only one item in pending_requests
     for i in range(1, len(uuids)):
         del lookup.pending_requests[uuids[i]]
     self.assertEqual(1, len(lookup.pending_requests))
     # Add K items from shortlist to the contacted set.
     for contact in lookup.shortlist:
         lookup.contacted.add(contact)
     # Cause the lookup to fire.
     msg = Nodes(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal,
                 self.contacts)
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     # The _lookup method should not be called.
     self.assertEqual(lookup._lookup.call_count, 0)
     # The lookup task has fired.
     self.assertTrue(lookup.done())
     with self.assertRaises(ValueNotFound) as result:
         lookup.result()
     self.assertIsInstance(result.exception, ValueNotFound)
     self.assertEqual(result.exception.args[0],
                      "Unable to find value for key: {}"
                      .format(self.target))
Beispiel #2
0
 def test_handle_response_all_shortlist_contacted_value_not_found(self):
     """
     If there are no more pending requests and all the nodes in the
     shortlist have been contacted then return the shortlist of nearest
     peer nodes to the target if the lookup is a FindNode.
     """
     lookup = Lookup(FindValue, self.target, self.node, self.event_loop)
     lookup._lookup = mock.MagicMock()
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     # Only one item in pending_requests
     for i in range(1, len(uuids)):
         del lookup.pending_requests[uuids[i]]
     self.assertEqual(1, len(lookup.pending_requests))
     # Add K items from shortlist to the contacted set.
     for contact in lookup.shortlist:
         lookup.contacted.add(contact)
     # Cause the lookup to fire.
     msg = Nodes(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal,
                 self.contacts)
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     # The _lookup method should not be called.
     self.assertEqual(lookup._lookup.call_count, 0)
     # The lookup task has fired.
     self.assertTrue(lookup.done())
     with self.assertRaises(ValueNotFound) as result:
         lookup.result()
     self.assertIsInstance(result.exception, ValueNotFound)
     self.assertEqual(result.exception.args[0],
                      "Unable to find value for key: {}"
                      .format(self.target))
Beispiel #3
0
 def test_handle_response_value_results_in_node_lookup_callback(self):
     """
     Tests that if a valid Value message is handled then all the other
     pending requests for the lookup are cancelled and the lookup has its
     set_result method called with the Value.
     """
     lookup = Lookup(FindValue, self.target, self.node, self.event_loop)
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     other_request1 = lookup.pending_requests[uuids[1]]
     other_request2 = lookup.pending_requests[uuids[2]]
     msg = Value(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal, self.target,
                 'value', time.time(), time.time() + 99999, self.version,
                 PUBLIC_KEY, 'name', 'signature')
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     self.event_loop.run_until_complete(blip())
     # Check the lookup has fired correctly.
     self.assertTrue(lookup.done())
     self.assertEqual(lookup.result(), msg)
     # Check the other requests are cancelled.
     self.assertTrue(other_request1.cancelled())
     self.assertTrue(other_request2.cancelled())
     # Make sure the pending_requests dict is empty.
     self.assertEqual(0, len(lookup.pending_requests))
     # Ensure the contact that provided the result is NOT in the shortlist.
     self.assertNotIn(contact, lookup.shortlist)
Beispiel #4
0
 def test_handle_response_value_results_in_node_lookup_callback(self):
     """
     Tests that if a valid Value message is handled then all the other
     pending requests for the lookup are cancelled and the lookup has its
     set_result method called with the Value.
     """
     lookup = Lookup(FindValue, self.target, self.node, self.event_loop)
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     other_request1 = lookup.pending_requests[uuids[1]]
     other_request2 = lookup.pending_requests[uuids[2]]
     msg = Value(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal, self.target,
                 'value', time.time(), time.time() + 99999, self.version,
                 PUBLIC_KEY, 'name', 'signature')
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     self.event_loop.run_until_complete(blip())
     # Check the lookup has fired correctly.
     self.assertTrue(lookup.done())
     self.assertEqual(lookup.result(), msg)
     # Check the other requests are cancelled.
     self.assertTrue(other_request1.cancelled())
     self.assertTrue(other_request2.cancelled())
     # Make sure the pending_requests dict is empty.
     self.assertEqual(0, len(lookup.pending_requests))
     # Ensure the contact that provided the result is NOT in the shortlist.
     self.assertNotIn(contact, lookup.shortlist)
Beispiel #5
0
 def test_handle_response_all_shortlist_contacted_return_nodes(self):
     """
     If there are no more pending requests and all the nodes in the
     shortlist have been contacted then return the shortlist of nearest
     peer nodes to the target if the lookup is a FindNode.
     """
     lookup = Lookup(FindNode, self.target, self.node, self.event_loop)
     lookup._lookup = mock.MagicMock()
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     # Only one item in pending_requests
     for i in range(1, len(uuids)):
         del lookup.pending_requests[uuids[i]]
     self.assertEqual(1, len(lookup.pending_requests))
     # Add K items from shortlist to the contacted set.
     for contact in lookup.shortlist:
         lookup.contacted.add(contact)
     # Cause the lookup to fire.
     msg = Nodes(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal,
                 self.contacts)
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     # The _lookup method should not be called.
     self.assertEqual(lookup._lookup.call_count, 0)
     # The lookup task has fired.
     self.assertTrue(lookup.done())
     # Check the result is the ordered shortlist of contacts that are
     # closest to the target.
     # It should be a list...
     self.assertIsInstance(lookup.result(), list)
     # It should be a list that's the lookup's shortlist...
     self.assertEqual(lookup.result(), lookup.shortlist)
     # It should be a list that's the lookup's shortlist in order.
     ordered = sort_peer_nodes(lookup.shortlist, self.target)
     self.assertEqual(lookup.result(), ordered)
Beispiel #6
0
 def test_handle_response_all_shortlist_contacted_return_nodes(self):
     """
     If there are no more pending requests and all the nodes in the
     shortlist have been contacted then return the shortlist of nearest
     peer nodes to the target if the lookup is a FindNode.
     """
     lookup = Lookup(FindNode, self.target, self.node, self.event_loop)
     lookup._lookup = mock.MagicMock()
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     # Only one item in pending_requests
     for i in range(1, len(uuids)):
         del lookup.pending_requests[uuids[i]]
     self.assertEqual(1, len(lookup.pending_requests))
     # Add K items from shortlist to the contacted set.
     for contact in lookup.shortlist:
         lookup.contacted.add(contact)
     # Cause the lookup to fire.
     msg = Nodes(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal,
                 self.contacts)
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     # The _lookup method should not be called.
     self.assertEqual(lookup._lookup.call_count, 0)
     # The lookup task has fired.
     self.assertTrue(lookup.done())
     # Check the result is the ordered shortlist of contacts that are
     # closest to the target.
     # It should be a list...
     self.assertIsInstance(lookup.result(), list)
     # It should be a list that's the lookup's shortlist...
     self.assertEqual(lookup.result(), lookup.shortlist)
     # It should be a list that's the lookup's shortlist in order.
     ordered = sort_peer_nodes(lookup.shortlist, self.target)
     self.assertEqual(lookup.result(), ordered)
Beispiel #7
0
 def test_handle_response_value_never_expires(self):
     """
     Ensures an expired Value is handled correctly.
     """
     lookup = Lookup(FindValue, self.target, self.node, self.event_loop)
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     msg = Value(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal, self.target,
                 'value', time.time(), 0.0, self.version, PUBLIC_KEY,
                 'name', 'signature')
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     self.assertEqual(lookup.result(), msg)
Beispiel #8
0
 def test_handle_response_value_never_expires(self):
     """
     Ensures an expired Value is handled correctly.
     """
     lookup = Lookup(FindValue, self.target, self.node, self.event_loop)
     uuids = [uuid for uuid in lookup.pending_requests.keys()]
     uuid = uuids[0]
     contact = lookup.shortlist[0]
     msg = Value(uuid, self.node.network_id, self.node.network_id,
                 self.reply_port, self.version, self.seal, self.target,
                 'value', time.time(), 0.0, self.version, PUBLIC_KEY,
                 'name', 'signature')
     response = asyncio.Future()
     response.set_result(msg)
     lookup._handle_response(uuid, contact, response)
     self.assertEqual(lookup.result(), msg)