Skip to content

Instantly share code, notes, and snippets.

@clee-jana
Last active January 17, 2016 17:06
Show Gist options
  • Save clee-jana/743973258170b2ff6d1f to your computer and use it in GitHub Desktop.
Save clee-jana/743973258170b2ff6d1f to your computer and use it in GitHub Desktop.
Example of replacing explicit time.sleep in integration tests
# original
make_request_that_triggers_async_processing()
time.sleep(30)
result = make_request_to_validate_changes()
self.assertEqual(‘new expected value’, result[‘data’])
# better
def check_for_expected_change(retries=5):
result = make_request_to_validate_changes()
if 'new_expected_value' == result[‘data’]:
return True
if retries <= 0:
return False
time.sleep(1)
return check_for_expected_changes(retries - 1)
make_request_that_triggers_async_processing()
if not check_for_expected_change(30):
self.fail('expected change was not reflected in api response')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment