Ticket #1938: session-delayedcall-1938.diff

File session-delayedcall-1938.diff, 2.8 KB (added by acapnotic, 16 years ago)

patch against rev 17599

  • twisted/web/server.py

     
    3535
    3636# Twisted Imports
    3737from twisted.spread import pb
    38 from twisted.internet import reactor, protocol, defer, address
     38from twisted.internet import reactor, protocol, defer, address, task
    3939from twisted.web import http
    4040from twisted.python import log, reflect, roots, failure, components
    4141from twisted import copyright
     
    399399        self.site = site
    400400        self.uid = uid
    401401        self.expireCallbacks = []
    402         self.touch()
    403402        self.sessionNamespaces = {}
     403        self.checkExpiredLoop = task.LoopingCall(self.checkExpired)
     404        self.touch()
    404405
    405406    def notifyOnExpire(self, callback):
    406407        """Call this callback when the session expires or logs out.
     
    415416        for c in self.expireCallbacks:
    416417            c()
    417418        self.expireCallbacks = []
     419        self.checkExpiredLoop.stop()
     420        # Break reference cycle.
     421        self.checkExpiredLoop = None
    418422
    419423    def touch(self):
    420424        self.lastModified = time.time()
     
    424428        if time.time() - self.lastModified > 900:
    425429            if self.site.sessions.has_key(self.uid):
    426430                self.expire()
    427             else:
    428                 pass
    429                 #log.msg("no session to expire: %s" % self.uid)
    430         else:
    431             #log.msg("session given the will to live for 30 more minutes")
    432             reactor.callLater(1800, self.checkExpired)
    433431
     432
     433
    434434version = "TwistedWeb/%s" % copyright.version
    435435
    436436
     
    467467        """Generate a new Session instance, and store it for future reference.
    468468        """
    469469        uid = self._mkuid()
    470         s = Session(self, uid)
    471         session = self.sessions[uid] = s
    472         reactor.callLater(1800, s.checkExpired)
     470        session = self.sessions[uid] = Session(self, uid)
     471        session.checkExpiredLoop.start(1800)
    473472        return session
    474473
    475474    def getSession(self, uid):
  • twisted/web/test/test_web.py

     
    8686        assert site.getResourceFor(DummyRequest([''])) is sres2, "Got the wrong resource."
    8787
    8888
     89
     90class SessionTest(unittest.TestCase):
     91
     92    def setUp(self):
     93        self.site = server.Site(SimpleResource())
     94
     95    def test_delayedCallCleanup(self):
     96        """Checking to make sure Sessions do not leave extra DelayedCalls.
     97        """
     98        session = self.site.makeSession()
     99        session.touch()
     100        session.expire()
     101        # If Trial doesn't complain about this being a broken test,
     102        # then I call it passing.
     103
     104
     105
    89106# Conditional requests:
    90107# If-None-Match, If-Modified-Since
    91108