def testCallbackException(self): """ Tests the callback method in case of exception """ # Set the callback before calling the method flag = threadpool.EventData() future = threadpool.FutureResult() future.set_callback(self._callback, flag) self.assertFalse(flag.is_set(), "Flag already set") # Execute try: future.execute(self._raise_call, None, None) except Exception as ex: # Store it exception = ex else: self.fail("Exception wasn't propagated") # Check event content self.assertTrue(flag.is_set(), "Callback method not called") self.assertIs(flag.exception, exception, "Exception not set") self.assertIsNone(flag.data, "Data set") # ... Re-set the callback (should be re-called) flag.clear() self.assertFalse(flag.is_set(), "Flag already set") future.set_callback(self._callback, flag) # Check event content self.assertTrue(flag.is_set(), "Callback method not called") self.assertIs(flag.exception, exception, "Exception not set") self.assertIsNone(flag.data, "Data set")
def testBadCallback(self): """ Tests behavior on callback error """ future = threadpool.FutureResult() args = (1, 2, 3) flag = threadpool.EventData() def dummy(): """ Callback without arguments """ flag.set() # Bad number of arguments: no exception must be raised future.set_callback(dummy) future.execute(self._simple_call, args, None) self.assertFalse(flag.is_set(), "Flag shouldn't be set...") def raising(data, exception, ex): """ Callback raising an exception """ flag.set() raise ex exception = ValueError("Dummy error") future.set_callback(raising, exception) self.assertTrue(flag.is_set(), "Callback not called")
def testCallback(self): """ Tests the callback method """ # Set the callback before calling the method flag = threadpool.EventData() future = threadpool.FutureResult() future.set_callback(self._callback, flag) self.assertFalse(flag.is_set(), "Flag already set") # Execute args = (1, 2, 3) future.execute(self._simple_call, args, None) # Check event content self.assertTrue(flag.is_set(), "Callback method not called") self.assertIsNone(flag.exception, "Exception set") self.assertEqual(flag.data, args, "Data not set") # ... Re-set the callback (should be re-called) flag.clear() self.assertFalse(flag.is_set(), "Flag already set") future.set_callback(self._callback, flag) # Check event content self.assertTrue(flag.is_set(), "Callback method not called") self.assertIsNone(flag.exception, "Exception set") self.assertEqual(flag.data, args, "Data not set")