Example #1
0
    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")
Example #2
0
    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")
Example #3
0
    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")
Example #4
0
    def testSimple(self):
        """
        Simple, error-less execution
        """
        # Create the future object
        future = threadpool.FutureResult()

        # Assert we have no result yet
        self.assertFalse(future.done(), "Execution flag up")
        self.assertRaises(OSError, future.result, 0)

        # Execute the method
        result1, result2, result3 = range(3)
        future.execute(self._simple_call, (result1, result2),
                       {"result": result3})

        # Assert it is done
        self.assertTrue(future.done(), "Execution flag not updated")
        self.assertEqual(future.result(), (result1, result2, result3),
                         "Invalid result")
Example #5
0
    def testRaise(self):
        """
        Tests the traversal of an exception
        """
        # Let the method raise its exception
        future = threadpool.FutureResult()

        try:
            future.execute(self._raise_call, None, None)
        except ValueError as ex:
            exception = ex
        else:
            self.fail("Execute didn't propagate the error")

        # The call must be considered as done
        self.assertTrue(future.done(), "Execution flag not updated")
        try:
            future.result()
        except ValueError as ex:
            self.assertIs(ex, exception, "Result exception changed")
        else:
            self.fail("Result didn't propagate the error")
Example #6
0
    def testTimeout(self):
        """
        Checks the timeout exit of result()
        """
        future = threadpool.FutureResult()
        result = object()

        # Call the method in a new thread
        thread = threading.Thread(target=future.execute,
                                  args=(_slow_call, (1, result), None))
        thread.daemon = True
        thread.start()

        # Check without wait
        self.assertRaises(OSError, future.result, 0)
        self.assertFalse(future.done(), "Execution flag up")

        # Check waiting a little
        self.assertRaises(OSError, future.result, .2)
        self.assertFalse(future.done(), "Execution flag up")

        # Check waiting longer
        self.assertIs(future.result(2), result, "Invalid result")
        self.assertTrue(future.done(), "Execution flag not updated")