Last active
January 17, 2016 17:06
-
-
Save clee-jana/743973258170b2ff6d1f to your computer and use it in GitHub Desktop.
Example of replacing explicit time.sleep in integration tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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