stb-tester
stb-tester copied to clipboard
RFC: Stbt.threading APIs
This is more of a code sketch for now, but it mirrors some of the test-scripts that I've been writing that have involved threading.
Makes it easier to write multi-threaded test-scripts where one is waiting for more than one event at a time. This has two significant parts:
-
We add the ability to interrupt a thread that we've previously spawned - as long as they are blocked on
get_frameortime.sleep. This takes care of tearing threads down once we're done with them without requiring the user to add any complicated logic for this purpose. This is the invasive part that requires changes to_stbt.coreThis same mechanism could be used to implement a timeout
contextmanager. -
We make it easy to start a thread and get a value back from it. This helps ensure that if one of our threads raises an exception it is propagated back to the main thread.
Example:
@spawn()
def look_for_logo():
return wait_until(lambda: match("logo.png"))
@spawn()
def motion():
return wait_for_motion()
with look_for_logo, motion:
look_for_logo.join()
This is built on top of #449. Only the last commit is relevant (for now).
Perhaps this should be split into two parts.
- A simple mechanism for getting a handle to a thread (when spawning) and interrupting it.
- A convenience API for spawning threads with the nice future like thead handles for getting values back.
TODO:
- [x] Merge #449
- [ ] Merge #468
- [ ] Discuss
- [ ] Tests