def test_unregisterProtocol_before_stopFactory(self):
     """Connections can go away before stopFactory occurs without causing
     errors.
     """
     ctf = gracefulshutdown.ConnTrackingFactoryWrapper(Factory())
     p = Protocol()
     ctf.registerProtocol(p)
     ctf.unregisterProtocol(p)  # No error raised.
 def test_200_when_available(self):
     """When the factory is available a 200 response is generated."""
     ctf = gracefulshutdown.ConnTrackingFactoryWrapper(Factory())
     r = gracefulshutdown.ServerAvailableResource([ctf])
     request = self.make_dummy_http_request()
     r.render_HEAD(request)
     self.assertEqual(200, request.code)
     # GET works too
     request = self.make_dummy_http_request()
     body = r.render_GET(request)
     self.assertEqual(200, request.code)
     self.assertTrue(body.startswith('Available\n'))
    def test_allConnectionsGone_when_no_connections(self):
        """
        The allConnectionsGone deferred is fired immediately when there are no
        connections when stopFactory occurs.
        """
        ctf = gracefulshutdown.ConnTrackingFactoryWrapper(Factory())
        self.was_fired = False
        self.assertTrue(ctf.isAvailable())
        ctf.stopFactory()
        self.assertFalse(ctf.isAvailable())

        def cb(ignored):
            self.was_fired = True

        ctf.allConnectionsGone.addCallback(cb)
        self.assertTrue(self.was_fired)
 def test_503_after_shutdown_starts(self):
     """
     When the factory is unavailable (i.e. stopFactory was called) a 503
     response is generated.
     """
     ctf = gracefulshutdown.ConnTrackingFactoryWrapper(Factory())
     r = gracefulshutdown.ServerAvailableResource([ctf])
     ctf.stopFactory()
     request = self.make_dummy_http_request()
     r.render_HEAD(request)
     self.assertEqual(503, request.code)
     # GET works too
     request = self.make_dummy_http_request()
     body = r.render_GET(request)
     self.assertEqual(503, request.code)
     self.assertTrue(body.startswith('Unavailable\n'))
    def test_allConnectionsGone_when_exactly_one_connection(self):
        """
        When there is one connection allConnectionsGone fires when that
        connection goes away.
        """
        ctf = gracefulshutdown.ConnTrackingFactoryWrapper(Factory())
        # Make one connection
        p = Protocol()
        ctf.registerProtocol(p)
        ctf.stopFactory()
        self.was_fired = False

        def cb(ignored):
            self.was_fired = True

        ctf.allConnectionsGone.addCallback(cb)
        self.assertFalse(self.was_fired)
        ctf.unregisterProtocol(p)
        self.assertTrue(self.was_fired)
    def test_allConnectionsGone_when_more_than_one_connection(self):
        """
        When there are two connections allConnectionsGone fires when both
        connections go away.
        """
        ctf = gracefulshutdown.ConnTrackingFactoryWrapper(Factory())
        # Make two connection
        p1 = Protocol()
        p2 = Protocol()
        ctf.registerProtocol(p1)
        ctf.registerProtocol(p2)
        ctf.stopFactory()
        self.was_fired = False

        def cb(ignored):
            self.was_fired = True

        ctf.allConnectionsGone.addCallback(cb)
        self.assertFalse(self.was_fired)
        ctf.unregisterProtocol(p1)
        self.assertFalse(self.was_fired)
        ctf.unregisterProtocol(p2)
        self.assertTrue(self.was_fired)
 def test_isAvailable_initial_state(self):
     """Initially a ConnTrackingFactoryWrapper is available."""
     ctf = gracefulshutdown.ConnTrackingFactoryWrapper(Factory())
     self.assertTrue(ctf.isAvailable())