示例#1
0
class RetryingAuthenticatorTests(SynchronousTestCase):
    """
    Tests for `RetryingAuthenticator`
    """
    def setUp(self):
        """
        Create RetryingAuthenticator
        """
        self.clock = Clock()
        self.mock_auth = iMock(IAuthenticator)
        self.authenticator = RetryingAuthenticator(self.clock,
                                                   self.mock_auth,
                                                   max_retries=3,
                                                   retry_interval=4)

    def test_delegates(self):
        """
        `RetryingAuthenticator` calls internal authenticator and returns its result
        """
        self.mock_auth.authenticate_tenant.return_value = succeed('result')
        d = self.authenticator.authenticate_tenant(23)
        self.assertEqual(self.successResultOf(d), 'result')
        self.mock_auth.authenticate_tenant.assert_called_once_with(23,
                                                                   log=None)

    def test_retries(self):
        """
        `RetryingAuthenticator` retries internal authenticator if it fails
        """
        self.mock_auth.authenticate_tenant.side_effect = lambda *a, **kw: fail(
            APIError(500, '2'))
        d = self.authenticator.authenticate_tenant(23)
        # mock_auth is called and there is no result
        self.assertNoResult(d)
        self.mock_auth.authenticate_tenant.assert_called_once_with(23,
                                                                   log=None)
        # Advance clock and mock_auth is called again
        self.clock.advance(4)
        self.assertEqual(self.mock_auth.authenticate_tenant.call_count, 2)
        self.assertNoResult(d)
        # advance clock and mock_auth's success return is propogated
        self.mock_auth.authenticate_tenant.side_effect = lambda *a, **kw: succeed(
            'result')
        self.clock.advance(4)
        self.assertEqual(self.successResultOf(d), 'result')

    def test_retries_times_out(self):
        """
        `RetryingAuthenticator` retries internal authenticator and times out if it
        keeps failing for certain period of time
        """
        self.mock_auth.authenticate_tenant.side_effect = lambda *a, **kw: fail(
            APIError(500, '2'))
        d = self.authenticator.authenticate_tenant(23)
        self.assertNoResult(d)
        self.clock.pump([4] * 4)
        f = self.failureResultOf(d, APIError)
        self.assertEqual(f.value.code, 500)
示例#2
0
 def setUp(self):
     """
     Create RetryingAuthenticator
     """
     self.clock = Clock()
     self.mock_auth = iMock(IAuthenticator)
     self.authenticator = RetryingAuthenticator(self.clock,
                                                self.mock_auth,
                                                max_retries=3,
                                                retry_interval=4)
示例#3
0
class RetryingAuthenticatorTests(TestCase):
    """
    Tests for `RetryingAuthenticator`
    """

    def setUp(self):
        """
        Create RetryingAuthenticator
        """
        self.clock = Clock()
        self.mock_auth = iMock(IAuthenticator)
        self.authenticator = RetryingAuthenticator(
            self.clock, self.mock_auth, max_retries=3, retry_interval=4)

    def test_delegates(self):
        """
        `RetryingAuthenticator` calls internal authenticator and returns its result
        """
        self.mock_auth.authenticate_tenant.return_value = succeed('result')
        d = self.authenticator.authenticate_tenant(23)
        self.assertEqual(self.successResultOf(d), 'result')
        self.mock_auth.authenticate_tenant.assert_called_once_with(23, log=None)

    def test_retries(self):
        """
        `RetryingAuthenticator` retries internal authenticator if it fails
        """
        self.mock_auth.authenticate_tenant.side_effect = lambda *a, **kw: fail(APIError(500, '2'))
        d = self.authenticator.authenticate_tenant(23)
        # mock_auth is called and there is no result
        self.assertNoResult(d)
        self.mock_auth.authenticate_tenant.assert_called_once_with(23, log=None)
        # Advance clock and mock_auth is called again
        self.clock.advance(4)
        self.assertEqual(self.mock_auth.authenticate_tenant.call_count, 2)
        self.assertNoResult(d)
        # advance clock and mock_auth's success return is propogated
        self.mock_auth.authenticate_tenant.side_effect = lambda *a, **kw: succeed('result')
        self.clock.advance(4)
        self.assertEqual(self.successResultOf(d), 'result')

    def test_retries_times_out(self):
        """
        `RetryingAuthenticator` retries internal authenticator and times out if it
        keeps failing for certain period of time
        """
        self.mock_auth.authenticate_tenant.side_effect = lambda *a, **kw: fail(APIError(500, '2'))
        d = self.authenticator.authenticate_tenant(23)
        self.assertNoResult(d)
        self.clock.pump([4] * 4)
        f = self.failureResultOf(d, APIError)
        self.assertEqual(f.value.code, 500)
示例#4
0
 def setUp(self):
     """
     Create RetryingAuthenticator
     """
     self.clock = Clock()
     self.mock_auth = iMock(IAuthenticator)
     self.authenticator = RetryingAuthenticator(
         self.clock, self.mock_auth, max_retries=3, retry_interval=4)