'The ``Deferred`` returned by ``AgentScript.main`` doesn\'t fire
until after the ``Deferred`` returned by the ``stopService`` method of
the service created by ``service_factory``.'

def test_main_deferred_fires_after_service_stop(self):

    shutdown_deferred = Deferred()
    class SlowShutdown(Service, ):
        def stopService(self):
            return shutdown_deferred
    service = SlowShutdown()
    agent = AgentScript(service_factory=(lambda reactor, options: service))
    stop_deferred = agent.main(self.reactor, self.options)
    self.reactor.fireSystemEvent('shutdown')
    self.assertNoResult(stop_deferred)
    shutdown_deferred.callback(None)
    self.assertIs(None, self.successResultOf(stop_deferred))
