We have a complex multiservice that needs to do some fairly intricate accounting when shutting down in order to implement a "graceful" shutdown.
I'm trying to write tests for this under trial. The issue is that the reactor is effectively a process-global resource, and shutting down my service means that trial's reactor is also stopped, which (of course) makes it explode.
This is documented to be a no-no in trial, but I need some kind of workaround that allows me to write my tests. My first thought was to use a mock.Mock, but this means we're not really using a reactor that's shutting down, which isn't going to give me behavior that's faithful to the actual shutdown process.
I believe what I need is a way to separate trial's reactor from the reactor of my service-under-test. Sharing a mutable resource between the test system and the system under test is surely an anti-pattern.
There's a difference between shutting down a service and stopping a reactor. You should be able to test most of the desired behavior with
myservice.stopService
. To test the code that actually initiates the shutdown, just mock outreactor.stop
withself.patch(reactor, 'stop', mock.Mock())
, and later assert that it was called. If you want to link the two, then have your mockstop
call your service'sstopService
.