-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add helper function to wait for async websocket result in tests
- Loading branch information
1 parent
25b303e
commit 01acea2
Showing
3 changed files
with
66 additions
and
10 deletions.
There are no files selected for viewing
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
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
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 file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import time | ||
|
||
|
||
def wait_for_condition(condition_func, timeout=5, poll_interval=0.1): | ||
""" | ||
This function is particularly useful in scenarios where asynchronous operations | ||
are involved. For instance, in a WebSocket-based system, certain events or | ||
state changes, like setting a flag in a callback, may not occur instantly. | ||
The wait_for_condition function ensures that the test waits long enough for | ||
these asynchronous events to complete, preventing race conditions or false | ||
negatives in test outcomes. | ||
""" | ||
start_time = time.time() | ||
while time.time() - start_time < timeout: | ||
if condition_func(): | ||
return True | ||
time.sleep(poll_interval) | ||
return False |