def test_cancel_pending_requests(self): """ Ensure all the tasks in the lookup's pending_requests dict are cancelled. """ lookup = Lookup(FindNode, self.target, self.node, self.event_loop) # Sanity check self.assertEqual(3, len(lookup.pending_requests)) tasks = lookup.pending_requests.values() lookup._cancel_pending_requests() self.event_loop.run_until_complete(blip()) self.assertEqual(lookup.pending_requests, {}) for task in tasks: self.assertTrue(task.cancelled())
def test_cancel_pending_requests(self): """ Ensure all the tasks in the lookup's pending_requests dict are cancelled. """ lookup = Lookup(FindNode, self.target, self.node, self.event_loop) # Sanity check self.assertEqual(3, len(lookup.pending_requests)) tasks = lookup.pending_requests.values() lookup._cancel_pending_requests() self.event_loop.run_until_complete(blip()) self.assertEqual(lookup.pending_requests, {}) for task in tasks: self.assertTrue(task.cancelled())
def test_lookup_added_callbacks_work_when_cancelled(self): """ Ensures that the callback added to pending requests by the _lookup method handles cancelled results. This may happen if the lookup is finished because a suitable value has been found (so everything else can be stopped ASAP). """ # Reset event_loop so we start in a clean state. loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) self.event_loop = asyncio.get_event_loop() lookup = Lookup(FindValue, self.target, self.node, self.event_loop) lookup._handle_response = mock.MagicMock() lookup._cancel_pending_requests() for k, v in lookup.pending_requests.items(): v.set_result('foo') self.event_loop.run_until_complete(v) self.assertEqual(lookup._handle_response.call_count, 0)
def test_lookup_added_callbacks_work_when_cancelled(self): """ Ensures that the callback added to pending requests by the _lookup method handles cancelled results. This may happen if the lookup is finished because a suitable value has been found (so everything else can be stopped ASAP). """ # Reset event_loop so we start in a clean state. loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) self.event_loop = asyncio.get_event_loop() lookup = Lookup(FindValue, self.target, self.node, self.event_loop) lookup._handle_response = mock.MagicMock() lookup._cancel_pending_requests() for k, v in lookup.pending_requests.items(): v.set_result('foo') self.event_loop.run_until_complete(v) self.assertEqual(lookup._handle_response.call_count, 0)
def test_cancel(self): """ Ensure that the expected operations happen when the lookup's cancel method is called (the pending requests also need to be cancelled). """ patcher = mock.patch('asyncio.Future.cancel') mock_cancel = patcher.start() lookup = Lookup(FindNode, self.target, self.node, self.event_loop) lookup._cancel_pending_requests = mock.MagicMock() result = lookup.cancel() self.assertTrue(result) self.assertEqual(lookup._cancel_pending_requests.call_count, 1) self.assertEqual(mock_cancel.call_count, 1) patcher.stop()
def test_cancel(self): """ Ensure that the expected operations happen when the lookup's cancel method is called (the pending requests also need to be cancelled). """ patcher = mock.patch('asyncio.Future.cancel') mock_cancel = patcher.start() lookup = Lookup(FindNode, self.target, self.node, self.event_loop) lookup._cancel_pending_requests = mock.MagicMock() result = lookup.cancel() self.assertTrue(result) self.assertEqual(lookup._cancel_pending_requests.call_count, 1) self.assertEqual(mock_cancel.call_count, 1) patcher.stop()