[Async-sig] async testing question

Chris Jerdonek chris.jerdonek at gmail.com
Tue Jul 4 15:03:44 EDT 2017


[Pulling in comments that were added to a different thread]

On Tue, Jul 4, 2017 at 3:03 AM, Dima Tisnek <dimaqq at gmail.com> wrote:
> Come to think of it, what sane tests need is a custom event loop or clever
> mocks around asyncio.sleep, asyncio.Condition.wait, etc. So that code under
> test never sleeps.
> ...
> In any case you should not have to add delays in your mocks or fixtures to
> hack specific order of task execution by the event loop.

Regarding guaranteeing a certain execution order, and going back to an
earlier question of mine, is there a way to introspect a task to find
out the name of the function it is currently waiting on?

It seems like such a function could go a long way towards guaranteeing
a required ordering, and without having to introduce sleeps, etc.
Inside a mock, you would be able to wait exactly until needed
conditions are satisfied.

I was experimenting with task.get_stack() [1], and it seems you can
get the line number of where a task is waiting. But using the line
number would be more brittle than using the function name.

--Chris


[1] https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.get_stack


More information about the Async-sig mailing list