示例#1
0
    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)
示例#2
0
    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)
示例#3
0
    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)
示例#4
0
    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)
示例#5
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])))
示例#6
0
    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)
示例#7
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])))
示例#8
0
    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)
示例#9
0
    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)
示例#10
0
    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)