Esempio n. 1
0
    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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    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])))
Esempio n. 4
0
    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])))