I need to check that certain operations have occurred in a particular order in threaded/asynchronous code. Something along the lines of:
def test_threaded_stuff():
# I can define the callbacks to the operation
op = start_operation(callback1, callback2, cbargs...)
op.checkpoint1.wait()
# check that callback1 and callback2 have been invoked,
# in that order
op.checkpoint2.wait()
# check that they have been invoked again, in reverse order
I can provide test callbacks that will be invoked by the operation, but I cannot place py.test assertions inside them because I need to test the overall order of their execution, not the state of any individual callback. Also, some of the callbacks are executed in separate threads which are not under the control of py.test.
To test such code, I came up with the following pattern:
def callback1(log):
log(1)
def callback2(log):
log(2)
def test_threaded_stuff():
events = []
op = start_operation(cb1, cb2, events.append)
op.checkpoint1.wait()
assert events == [1, 2]
op.checkpoint2.wait()
assert events == [1, 2, 2, 1]
Is there an idiomatic way to express this in py.test? For example, a callable fixture that automatically logs its invocations, so that I can query the invocations in my tests.
If concrete examples are needed, this file is an example, as are other files in the same directory.