def testSimpleArgAndKwArg(self): x = 9 y = 'hey' rc = RetryingCall(lambda x, y=None: (x, y), x, y=y) d = rc.start() d.addCallback(lambda r: self.assertEqual(r, (x, y))) return d
def testDontAllowKeyError(self): def _failureTester(f): f.trap(ValueError) f = InitiallyFailing(3, exceptionList=[KeyError]) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) self.failUnlessFailure(d, KeyError) return d
def userByScreenname(screenname): r = RetryingCall( client.getPage, "%s/users/show.json?screen_name=%s" % (TWITTER_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 testExactlyOneSuccessfulCall(self): result = 99 f = CallCounter() rc = RetryingCall(f, result) d = rc.start() d.addCallback(lambda r: self.assertEqual(r, 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 test1ValueError(self): def _failureTester(f): f.trap(ValueError) result = 5 f = InitiallyFailing(1, result=result, exceptionList=[ValueError]) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) d.addCallback(lambda r: self.assertEqual(r, result)) d.addCallback(lambda _: f.assertMaximallyFailed()) return d
def testIgnoreRegularException(self): def _failureTester(f): f.trap(Exception) result = 5 f = InitiallyFailing(3, result=result) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) d.addCallback(lambda r: self.assertEqual(r, result)) d.addCallback(lambda _: f.assertMaximallyFailed()) return d
def testSimpleDeferredReturner(self): result1 = 15 result2 = 16 def _ret(result): self.assertEqual(result, result1) return result2 rc = RetryingCall(lambda: defer.succeed(result1)) d = rc.start() d.addCallback(_ret) d.addCallback(lambda r: self.assertEqual(r, result2)) return d
def testValueErrorThenNameError(self): def _failureTester(f): if not f.check(ValueError, NameError): return f result = 15 f = InitiallyFailing(2, result=result, exceptionList=[ValueError, NameError]) rc = RetryingCall(f) d = rc.start(failureTester=_failureTester) d.addCallback(lambda r: self.assertEqual(r, result)) d.addCallback(lambda _: f.assertMaximallyFailed()) return d
def _storeAccessToken(self, result, request): accessToken = oauth.OAuthToken.from_string(result) log.msg('Got access token: %s' % accessToken) oaRequest = oauth.OAuthRequest.from_consumer_and_token( consumer.consumer, token=accessToken, http_url=twitter.VERIFY_CREDENTIALS_URL) oaRequest.sign_request( signature.hmac_sha1, consumer.consumer, accessToken) log.msg('Verifying credentials.') r = RetryingCall(client.getPage, oaRequest.to_url()) d = r.start() d.addCallback(self._storeUser, accessToken, request) d.addErrback(log.err) return d
def render_GET(self, request): # The cookie cache is actually only available once the cache # service has started. But that should happen before we get any # requests. log.err('Callback received: %s' % request) oauthToken = request.args['oauth_token'] if oauthToken: oauthToken = oauthToken[0] else: log.err('Received callback with no oauth_token: %s' % request) raise Exception('Received callback with no oauth_token.') oauthVerifier = request.args['oauth_verifier'] if oauthVerifier: oauthVerifier = oauthVerifier[0] else: log.err('Received callback with no oauth_verifier: %s' % request) raise Exception('Received callback with no oauth_verifier.') try: token = self.cache.oauthTokenDict.pop(oauthToken) except KeyError: log.err('Received callback with unknown oauth_token: %s' % oauthToken) raise Exception('Received callback with unknown oauth_token.') oaRequest = oauth.OAuthRequest.from_consumer_and_token( consumer.consumer, token=token, verifier=oauthVerifier, http_url=twitter.ACCESS_TOKEN_URL) oaRequest.sign_request( signature.hmac_sha1, consumer.consumer, token) log.msg('Requesting access token.') r = RetryingCall( client.getPage, oaRequest.to_url(), headers=oaRequest.to_header()) d = r.start() d.addCallback(self._storeAccessToken, request) d.addErrback(log.err) return server.NOT_DONE_YET
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 testSimplestDeferredReturner(self): result = 15 rc = RetryingCall(lambda: defer.succeed(result)) d = rc.start() d.addCallback(lambda r: self.assertEqual(r, result)) return d
def testSimplestNoFailure(self): result = 15 rc = RetryingCall(lambda: result) d = rc.start() d.addCallback(lambda r: self.assertEqual(r, result)) return d
def testListBackoffIteratorAsList(self): rc = RetryingCall(lambda: defer.fail(Exception())) d = rc.start(backoffIterator=[0.05, 0.06, 0.07]) self.failUnlessFailure(d, Exception) return d
def testListBackoffIteratorAsTuple(self): rc = RetryingCall(lambda: defer.fail(Exception())) d = rc.start(backoffIterator=(0.01, 0.01, 0.01)) self.failUnlessFailure(d, Exception) return d
def testInitiallyFailing0Failures(self): f = InitiallyFailing(0) rc = RetryingCall(f) d = rc.start() d.addCallback(lambda _: f.assertMaximallyFailed()) return d
def testFirstFailureReceived(self): f = ValueErrorThenNameErrorRaiser() rc = RetryingCall(f) d = rc.start(backoffIterator=(0.01, 0.01, 0.01)) self.failUnlessFailure(d, ValueError) return d
def testSimpleKwArgs(self): result = 9 rc = RetryingCall(lambda xxx=None: xxx, xxx=result) d = rc.start() d.addCallback(lambda r: self.assertEqual(r, result)) return d