def test_all_completed_class_method(self): """ Test the 'all_completed' class method """ responses = [] for i in range(10): responses.append(AsyncResponse()) # Create a method that will be executed on a different thread # And resolve each async response def resolver(): time.sleep(1) for i in responses: i.resolve(TEST_RESPONSE) # Start the resolving thread threading.Thread(target=resolver).start() results = AsyncResponse.all_completed(responses) self.assertEquals(len(results), len(responses)) for i in results: self.assertEquals(i, TEST_RESPONSE)
def test_as_completed_class_method(self): """ Test the 'as_completed' iterator """ responses = [] for i in range(10): responses.append(AsyncResponse()) # Setup counter completed = 0 # Create a method that will be executed on a different thread # And resolve each async response def resolver(): time.sleep(1) for i in responses: i.resolve(TEST_RESPONSE) # Start the resolving thread threading.Thread(target=resolver).start() for i in AsyncResponse.as_completed(responses): self.assertEquals(i, TEST_RESPONSE) completed += 1 # test it was completed self.assertEquals(completed, 10)
def test_response_timeout(self): """ Test response with a timeout """ # New result r = AsyncResponse() # Get the value, with 1 second timeout. this should always fail r.response(1)
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)
def test_all_completed_with_timeout(self): """ Test the 'all_completed' class method """ responses = [] for i in range(10): responses.append(AsyncResponse()) # Create a method that will be executed on a different thread # And resolve each async response def resolver(): time.sleep(10) for i in responses: i.resolve(TEST_RESPONSE) # Start the resolving thread t = threading.Thread(target=resolver) t.daemon = True t.start() results = AsyncResponse.all_completed(responses, timeout=1)