[Async-sig] async testing question
Nathaniel Smith
njs at pobox.com
Sat Jul 1 16:42:10 EDT 2017
On Jul 1, 2017 3:11 AM, "Chris Jerdonek" <chris.jerdonek at gmail.com> wrote:
I have a question about testing async code.
Say I have a coroutine:
async def do_things():
await do_something()
await do_more()
await do_even_more()
And future:
task = ensure_future(do_things())
Is there a way to write a test case to check that task.cancel() would
behave correctly if, say, do_things() is waiting at the line
do_more()?
One possibility for handling this case with a minimum of mocking would be
to hook do_more so that it calls task.cancel and then calls the regular
do_more.
Beyond that it depends on what the actual functions are, I guess. If
do_more naturally blocks under some conditions then you might be able to
set up those conditions and then call cancel. Or you could try
experimenting with tests that call sleep(0) a fixed number of times before
issuing the cancel, and repeat with different iteration counts to find
different cancel points. (This would benefit from some kind of
collaboration with the scheduler, but even a simple hack like this will
probably get you more coverage than you had before. It does assume that
your test never actually sleeps though.)
-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/async-sig/attachments/20170701/0ac87688/attachment.html>
More information about the Async-sig
mailing list