Skip to content

Commit

Permalink
send image should raise exception once we exceed tries
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzhak committed May 26, 2017
1 parent bccfa76 commit 5c1a1fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
15 changes: 9 additions & 6 deletions botstory/integrations/fb/messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,18 @@ async def send_image(self, recipient, url, options=None):
delay = options.get('retry_delay', 1)
tries = options.get('retry_times', 3)

while tries > 0 and should_try:
while should_try:
try:
tries -= 1
await self._send_image(recipient, url)
should_try = False
except commonhttp_errors.HttpRequestError:
logger.warning('# retry to send image {}'.format(url))
should_try = True
await asyncio.sleep(delay)
except commonhttp_errors.HttpRequestError as err:
if tries > 0:
tries -= 1
logger.warning('# retry to send image {}'.format(url))
should_try = True
await asyncio.sleep(delay)
else:
raise err

async def _send_image(self, recipient, url):
return await self.http.post(
Expand Down
23 changes: 20 additions & 3 deletions botstory/integrations/fb/messenger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ async def test_retry_send_image():

send_task = fb_interface.send_image(talk.user, 'http://shevchenko.ua/image.gif', options={
'retry_times': 3,
'retry_delay': 1,
'retry_delay': 0.1,
})

async def lazy_fix_http():
# here should pass first 2 retry
await asyncio.sleep(1.5)
await asyncio.sleep(0.15)
# than we change mock http without post raise
# so on 3 try it should pass without problem
story.use(mockhttp.MockHttpInterface())
Expand All @@ -474,7 +474,24 @@ async def lazy_fix_http():
)

should_post_attachment(mock_http, talk)
should_post_attachment(mock_http, talk)


@pytest.mark.asyncio
async def test_retry_send_image_should_fail_on_tries_exceed():
with answer.Talk() as talk:
story = talk.story
fb_interface = story.use(messenger.FBInterface(page_access_token='qwerty1'))
mock_http = story.use(mockhttp.MockHttpInterface(
post_raise=commonhttp_errors.HttpRequestError(),
))
await story.start()

with pytest.raises(commonhttp_errors.HttpRequestError):
await fb_interface.send_image(talk.user, 'http://shevchenko.ua/image.gif', options={
'retry_times': 3,
'retry_delay': 0.1,
})

should_post_attachment(mock_http, talk)


Expand Down

0 comments on commit 5c1a1fe

Please sign in to comment.