def test_loginLogout(self):
        """
        Test that login can be performed with IUsernamePassword credentials and
        that when the connection is dropped the avatar is logged out.
        """
        self.portal.registerChecker(
            checkers.InMemoryUsernamePasswordDatabaseDontUse(user=b'pass'))
        creds = credentials.UsernamePassword(b"user", b"pass")

        # NOTE: real code probably won't need anything where we have the
        # "BRAINS!" argument, passing None is fine. We just do it here to
        # test that it is being passed. It is used to give additional info to
        # the realm to aid perspective creation, if you don't need that,
        # ignore it.
        mind = "BRAINS!"

        loginCompleted = Deferred()

        d = self.clientFactory.login(creds, mind)
        def cbLogin(perspective):
            self.assertTrue(self.realm.lastPerspective.loggedIn)
            self.assertIsInstance(perspective, pb.RemoteReference)
            return loginCompleted

        def cbDisconnect(ignored):
            self.clientFactory.disconnect()
            self.completeClientLostConnection()

        d.addCallback(cbLogin)
        d.addCallback(cbDisconnect)

        def cbLogout(ignored):
            self.assertTrue(self.realm.lastPerspective.loggedOut)
        d.addCallback(cbLogout)

        self.establishClientAndServer()
        self.pump.flush()
        # The perspective passed to cbLogin has gone out of scope.
        # Ensure its __del__ runs...
        gc.collect()
        # ...and send its decref message to the server
        self.pump.flush()
        # Now allow the client to disconnect.
        loginCompleted.callback(None)
        return d

        Test that login can be performed with IUsernamePassword credentials and
        that when the connection is dropped the avatar is logged out.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/spread/test/test_pb.py