def test_no_double_set(self): """ Should throw exception if trying to set the result twice """ # Create a new result r = AsyncResponse() # resolve with the first value r.resolve(TEST_RESPONSE) # try to resolve again r.resolve(TEST_RESPONSE)
def test_create_and_resolve(self): """ The most basic test for getting and setting results """ # Create a new result r = AsyncResponse() # Should not be ready self.assertFalse(r.completed) # set the result r.resolve(TEST_RESPONSE) # Is the result ready self.assertTrue(r.completed) # test getting the result self.assertEqual(r.response(), TEST_RESPONSE)
def test_resolved_with_exception(self): """ Test resolving AsyncResponse with exception """ resp = AsyncResponse() # resolve with an exception resp.resolve(ValueError('Test Error')) self.assertTrue(resp.is_exception) # Assert that response is raising self.assertRaises(ValueError, resp.response) # Assert that using all_completed/as_completed raises self.assertRaises(ValueError, AsyncResponse.all_completed, [resp]) self.assertRaises(ValueError, lambda: list(AsyncResponse.as_completed([resp])))
def test_callback(self): """ Test that a callback is called when setting the result """ cb_result = {} # Setup the callback def cb(value): cb_result['value'] = value cb_result['called'] = True # Create a result with a callback r = AsyncResponse(cb) # set the result r.resolve(TEST_RESPONSE) self.assertTrue(cb_result['called']) self.assertEqual(cb_result['value'], TEST_RESPONSE)
def test_add_callback(self): """ Tests adding a callback after the result is created """ r = AsyncResponse() cb_result = {} # Setup the callback def cb(value): cb_result['value'] = value cb_result['called'] = True # Add the callback r.add_callback(cb) r.resolve(TEST_RESPONSE) self.assertTrue(cb_result['called']) self.assertEqual(cb_result['value'], TEST_RESPONSE)