'This handy method cleans all pending tasks from the reactor.
When writing a unit test, consider the following question:
Is the code that you are testing required to release control once it
has done its job, so that it is impossible for it to later come around
(with a delayed reactor task) and do anything further?
If so, then trial will usefully test that for you -- if the code under
test leaves any pending tasks on the reactor then trial will fail it.
On the other hand, some code is *not* required to release control -- some
code is allowed to continuously maintain control by rescheduling reactor
tasks in order to do ongoing work.  Trial will incorrectly require that
code to clean up all its tasks from the reactor.
Most people think that such code should be amended to have an optional
"shutdown" operation that releases all control, but on the contrary it is
good design for some code to *not* have a shutdown operation, but instead
to have a "crash-only" design in which it recovers from crash on startup.
If the code under test is of the "long-running" kind, which is *not*
required to shutdown cleanly in order to pass tests, then you can simply
call testutil.clean_pending() at the end of the unit test, and trial will
be satisfied.'

def clean_pending(self, dummy=None, required_to_quiesce=True):

    pending = reactor.getDelayedCalls()
    active = bool(pending)
    for p in pending:
        if p.active():
            p.cancel()
        else:
            print 'WEIRDNESS!    pending    timed    call    not    active!'
    if (required_to_quiesce and active):
        self.fail('Reactor    was    still    active    when    it    was    required    to    be    quiescent.')
