def test_buildProtocolReturnsNone(self):
        """
        When the factory's C{buildProtocol} returns L{None} the connection is
        gracefully closed.
        """
        connectionLost = Deferred()
        reactor = self.buildReactor()
        serverFactory = MyServerFactory()
        serverFactory.protocolConnectionLost = connectionLost

        # Make sure the test ends quickly.
        stopOnError(self, reactor)

        class NoneFactory(ServerFactory):
            def buildProtocol(self, address):
                return None

        listening = self.endpoints.server(reactor).listen(serverFactory)

        def listened(port):
            clientFactory = NoneFactory()
            endpoint = self.endpoints.client(reactor, port.getHost())
            return endpoint.connect(clientFactory)
        connecting = listening.addCallback(listened)

        def connectSucceeded(protocol):
            self.fail(
                "Stream client endpoint connect succeeded with %r, "
                "should have failed with NoProtocol." % (protocol,))
        def connectFailed(reason):
            reason.trap(NoProtocol)
        connecting.addCallbacks(connectSucceeded, connectFailed)

        def connected(ignored):
            # Now that the connection attempt has failed continue waiting for
            # the server-side connection to be lost.  This is the behavior this
            # test is primarily concerned with.
            return connectionLost
        disconnecting = connecting.addCallback(connected)

        # Make sure any errors that happen in that process get logged quickly.
        disconnecting.addErrback(log.err)

        def disconnected(ignored):
            # The Deferred has to succeed at this point (because log.err always
            # returns None).  If an error got logged it will fail the test.
            # Stop the reactor now so the test can complete one way or the
            # other now.
            reactor.stop()
        disconnecting.addCallback(disconnected)

        self.runReactor(reactor)

        When the factory's C{buildProtocol} returns L{None} the connection is
        gracefully closed.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/internet/test/test_tcp.py