def testSimplePositionalArgs(self): """Check that positional arguments we give to the constructor are passed to the function when it is called.""" rc = RetryingCall((lambda *x: x), 9, 10, 11) d = rc.start() d.addCallback(lambda result: self.assertEqual((9, 10, 11), result)) return d
def getTwitterOAuthURL(conf, oauthTokenDict): """ Obtain a URL from twitter.com that we can redirect a user to so they can authenticate themselves and authorize loveme.do to act on their behalf. @param conf: the lovemedo configuration. @param oauthTokenDict: A C{dict} mapping token keys to tokens. @return: A C{Deferred} that fires with the URL for OAuth verification. """ log.msg('Got login URL request.') def _makeURL(result): token = OAuthToken.from_string(result) # Store the token by key so we can find it when the callback comes. oauthTokenDict[token.key] = token request = OAuthRequest.from_token_and_callback( token=token, http_url=conf.authorization_url) url = request.to_url() log.msg('Browser OAuth redirect URL = %r' % url) return url consumer = OAuthConsumer(conf.consumer_key, conf.consumer_secret) request = OAuthRequest.from_consumer_and_token( consumer, callback=conf.callback_url, http_url=conf.request_token_url) request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), consumer, None) r = RetryingCall( client.getPage, conf.request_token_url, headers=request.to_header()) d = r.start() d.addCallback(_makeURL) d.addErrback(log.err) return d
def testSimplePositionalAndKeywordArgs(self): """Check that positional and keyword arguments given to the constructor are passed to the function when it is called.""" rc = RetryingCall((lambda x, y=None: (x, y)), 9, y='hey') d = rc.start() d.addCallback(lambda r: self.assertEqual(r, (9, 'hey'))) return d
def testInitiallyFailingNoFailures(self): """Test the C{_InitiallyFailing} class when it is told not to fail.""" f = _InitiallyFailing(0) rc = RetryingCall(f) d = rc.start() d.addCallback(lambda _: self.assertEqual([], rc.failures)) return d
def testListBackoffIteratorAsTuple(self): """Pass a back-off iterator that is a C{tuple} and make sure the function is called without incident until the list is exhausted.""" rc = RetryingCall(lambda: defer.fail(Exception())) d = rc.start(backoffIterator=(0.01, 0.01, 0.01)) self.failUnlessFailure(d, Exception) return d
def testSimpleKeywordArgs(self): """Check that keyword arguments we give to the constructor are passed to the function when it is called.""" rc = RetryingCall((lambda xxx, yyy: (yyy, xxx)), xxx=10, yyy='no!') d = rc.start() d.addCallback(lambda result: self.assertEqual(('no!', 10), result)) return d
def testFirstFailureReceived(self): """Ensure that the first failure encountered is the one that is passed to the errback when the retrying call gives up.""" f = _ValueErrorThenNameErrorRaiser() rc = RetryingCall(f) d = rc.start(backoffIterator=(0.01, 0.01, 0.01)) self.failUnlessFailure(d, ValueError) return d
def testExactlyOneSuccessfulCall(self): """Ensure that a function that returns a result is only called once.""" f = _CallCounter() rc = RetryingCall(f, 99) d = rc.start() d.addCallback(lambda result: self.assertEqual(99, result)) d.addCallback(lambda _: f.assertCalledOnce()) return d
def userById(uid): r = RetryingCall( client.getPage, '%s/users/show.json?user_id=%s' % (TWITTER_API_URL, uid)) tester = AllowOne404Tester() d = r.start(failureTester=tester.test) d.addCallback(lambda j: json.loads(j)) return d
def userByScreenname(screenname): r = RetryingCall( client.getPage, '%s/users/show.json?screen_name=%s' % (TWITTER_API_URL, urllib.quote(screenname.encode('utf-8')))) tester = AllowOne404Tester() d = r.start(failureTester=tester.test) d.addCallback(lambda j: json.loads(j)) return d
def testSimplestNoFailure(self): """A (retrying) call to a function that returns a constant should return that constant and should encounter no failures.""" rc = RetryingCall(lambda: 20) def _check(result): self.assertEqual(20, result) self.assertEqual([], rc.failures) d = rc.start() d.addCallback(_check) return d
def testSimpleDeferredReturner(self): """Test that adding a callback to the C{Deferred} returned by C{start} works as expected.""" def _ret(result): self.assertEqual(result, 200) return 'floopy' rc = RetryingCall(lambda: defer.succeed(200)) d = rc.start() d.addCallback(_ret) d.addCallback(lambda r: self.assertEqual(r, 'floopy')) return d
def testDontAllowKeyError(self): """Call a function that fails with a C{KeyError}s but with a failure tester that only ignores C{ValueError} and check that the call fails with a C{KeyError}. """ def _failureTester(f): """Return C{None} on any C{ValueError} failure. @param f: a C{Failure}. @return: C{None} if C{f} is an C{ValueError} failure, else return C{f}. """ f.trap(ValueError) f = _InitiallyFailing(3, exceptionList=[KeyError]) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) self.failUnlessFailure(d, KeyError) return d
def test3ValueErrors(self): """Call a function that initially fails three times with C{ValueError}s and then returns a result. """ def _failureTester(f): """Return C{None} on any C{ValueError} failure. @param f: a C{Failure}. @return: C{None} if C{f} is an C{ValueError} failure, else return C{f}. """ f.trap(ValueError) f = _InitiallyFailing(3, result=7, exceptionList=[ValueError] * 3) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) d.addCallback(lambda result: self.assertEqual(7, result)) d.addCallback(lambda _: self.assertEqual(3, len(rc.failures))) return d
def testIgnoreRegularException(self): """Use a failure tester that ignores C{Exception} with an C{_InitiallyFailing} instance that raises 3 errors before returning the correct result.""" def _failureTester(f): """Return C{None} on any C{Exception} failure. @param f: a C{Failure}. @return: C{None} if C{f} is an C{Exception} failure, else return C{f}. """ f.trap(Exception) f = _InitiallyFailing(3, result=5) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) d.addCallback(lambda result: self.assertEqual(5, result)) d.addCallback(lambda _: self.assertEqual(3, len(rc.failures))) return d
def testValueErrorThenNameError(self): """Call a function that fails first with a C{ValueError} and then with a C{NameError} before returning its result. """ def _failureTester(f): """Return C{None} on any C{ValueError} or C{NameError} failure. @param f: a C{Failure}. @return: C{None} if C{f} is a C{ValueError} or C{NameError} failure, else return C{f}. """ if not f.check(ValueError, NameError): return f f = _InitiallyFailing(2, result=15, exceptionList=[ValueError, NameError]) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) d.addCallback(lambda result: self.assertEqual(15, result)) d.addCallback(lambda _: self.assertEqual(2, len(rc.failures))) return d
def getTwitterOAuthURL(oauthTokenDict): log.msg('Got login URL request.') def _cb(result): token = oauth.OAuthToken.from_string(result) # Store the token so we have the oauth_secret when the callback comes. oauthTokenDict[token.key] = token request = oauth.OAuthRequest.from_token_and_callback( token=token, http_url=twitter.AUTHORIZATION_URL) url = request.to_url() log.msg('Browser Twitter auth redirect URL = %r' % url) return url request = oauth.OAuthRequest.from_consumer_and_token( consumer.consumer, callback=defaults.TICKERY_CALLBACK_URL, http_url=twitter.REQUEST_TOKEN_URL) request.sign_request(signature.hmac_sha1, consumer.consumer, None) r = RetryingCall( client.getPage, twitter.REQUEST_TOKEN_URL, headers=request.to_header()) d = r.start() d.addCallback(_cb) return d
def __call__(self, f, *args, **kwargs): retrying_call = RetryingCall(f, *args, **kwargs) return retrying_call.start(backoffIterator=self._backoff_iterator(), failureTester=self._test_failure)
def __call__(self, f, *args, **kwargs): retrying_call = RetryingCall(f, *args, **kwargs) return retrying_call.start( backoffIterator=self._backoff_iterator(), failureTester=self._test_failure)
def testSimplestDeferredReturner(self): """Test a C{Deferred} returning function that returns a constant.""" rc = RetryingCall(lambda: defer.succeed(15)) d = rc.start() d.addCallback(lambda result: self.assertEqual(15, result)) return d