Skip to content

Commit

Permalink
fix several bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
vilmibm committed Sep 29, 2016
1 parent 3fff8a5 commit 8fb228d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion randomwaite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 97,7 @@ def main():
card = draw_tarot_card()
if debug:
im = generate(card)
logger.debug('saving to', DEBUG_IMAGE_PATH)
logger.debug('saving to %s', DEBUG_IMAGE_PATH)
im.save(DEBUG_IMAGE_PATH)
sys.exit(0)

Expand Down
3 changes: 3 additions & 0 deletions randomwaite/errors.py
Original file line number Diff line number Diff line change
@@ -1,2 1,5 @@
class TinyImageException(Exception):
pass

class TwitterMessedUpException(Exception):
pass
1 change: 1 addition & 0 deletions randomwaite/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 15,5 @@ def get_logger() -> logging.Logger:
maxBytes=MAXLOGSIZE,
backupCount=BACKUP_COUNT))
logger.setLevel(logging.DEBUG)
return logger

20 changes: 15 additions & 5 deletions randomwaite/twitter.py
Original file line number Diff line number Diff line change
@@ -1,35 1,43 @@
import typing as t
from functools import wraps
from io import BytesIO
from time import sleep

from PIL import Image
import tweepy

from . import secrets as sec
from .errors import TwitterMessedUpException
from .logs import get_logger


TwitterClient = tweepy.API

logger = get_logger()

MAX_RETRIES = 10
DECAY_FACTOR = 2

def twitter_retry(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
max_tries = 10
tries = 1
decay_factor = 2
result = None

while result is None and tries <= max_tries:
while result is None:
if tries > MAX_RETRIES:
break

try:
logger.debug('making a call to twitter...')
result = fn(*args, **kwargs)
except tweepy.TweepError as e:
logger.exception('got an error from twitter')
sleep(tries ** decay_factor)
sleep(tries ** DECAY_FACTOR)
tries = 1

if result is None:
raise Exception('twitter is seriously ill, giving up after {} tries'.format(tries))
raise TwitterMessedUpException('giving up after {} tries'.format(tries))

return result
return wrapper
Expand All @@ -53,6 61,8 @@ def post_image(client: TwitterClient,
image: Image,
reply_to_status_id:str=None) -> None:

logger.debug('posting an image')

buffer = BytesIO()
image.save(buffer, format='JPEG')

Expand Down
43 changes: 43 additions & 0 deletions tests/test_twitter.py
Original file line number Diff line number Diff line change
@@ -0,0 1,43 @@
from unittest import TestCase
from unittest.mock import patch

from tweepy import TweepError

import randomwaite.twitter as rt
from randomwaite.errors import TwitterMessedUpException
from randomwaite.twitter import twitter_retry

class TestTwitterRetrier(TestCase):
def test_successful_call(self):
def fake_twitter_call(x, y):
return x y
under_test = twitter_retry(fake_twitter_call)
self.assertEqual(3, under_test(1,2))

@patch('randomwaite.twitter.logger')
def test_handles_one_error(self, logger_mock):
called = False
def fake_twitter_call(x, y):
nonlocal called
if not called:
called = True
raise TweepError('foobar')
return x y
under_test = twitter_retry(fake_twitter_call)

result = under_test(660,6)
self.assertEqual(1, len(logger_mock.exception.call_args_list))
self.assertTrue(called)
self.assertEqual(666, result)

@patch('randomwaite.twitter.sleep')
@patch('randomwaite.twitter.logger')
def test_stops_trying(self, logger_mock, sleep_mock):
def fake_twitter_call(x, y):
raise TweepError('foobar')
under_test = twitter_retry(fake_twitter_call)
with self.assertRaises(TwitterMessedUpException):
under_test(1,2)
self.assertEqual(rt.MAX_RETRIES, len(logger_mock.exception.call_args_list))
self.assertTrue(sleep_mock.called)

0 comments on commit 8fb228d

Please sign in to comment.