Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1922

Show
Ignore:
Timestamp:
03/14/08 10:18:00
Author:
fumanchu
Message:

Various session fixes, including #717 (sessions should have a len function).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/sessions.py

    r1904 r1922  
    463463class MemcachedSession(Session): 
    464464     
     465    # The most popular memcached client for Python isn't thread-safe. 
     466    # Wrap all .get and .set operations in a single lock. 
     467    mc_lock = threading.RLock() 
     468     
     469    # This is a seperate set of locks per session id. 
    465470    locks = {} 
     471     
    466472    servers = ['127.0.0.1:11211'] 
    467473     
     
    480486     
    481487    def _load(self): 
    482         return self.cache.get(self.id) 
     488        self.mc_lock.acquire() 
     489        try: 
     490            return self.cache.get(self.id) 
     491        finally: 
     492            self.mc_lock.release() 
    483493     
    484494    def _save(self, expiration_time): 
    485495        # Send the expiration time as "Unix time" (seconds since 1/1/1970) 
    486         zeroday = datetime.datetime(1970, 1, 1, tzinfo=expiration_time.tzinfo) 
    487         td = expiration_time - zeroday 
    488         td = (td.days * 86400) + td.seconds 
    489         if not self.cache.set(self.id, (self._data, expiration_time), td): 
    490             raise AssertionError("Session data for id %r not set." % self.id) 
     496        td = int(time.mktime(expiration_time.timetuple())) 
     497        self.mc_lock.acquire() 
     498        try: 
     499            if not self.cache.set(self.id, (self._data, expiration_time), td): 
     500                raise AssertionError("Session data for id %r not set." % self.id) 
     501        finally: 
     502            self.mc_lock.release() 
    491503     
    492504    def _delete(self): 
     
    502514        self.locks[self.id].release() 
    503515        self.locked = False 
     516     
     517    def __len__(self): 
     518        """Return the number of active sessions.""" 
     519        raise NotImplementedError 
    504520 
    505521 
     
    532548    """Close the session object for this request.""" 
    533549    sess = getattr(cherrypy.serving, "session", None) 
    534     if sess and sess.locked
     550    if getattr(sess, "locked", False)
    535551        # If the session is still locked we release the lock 
    536552        sess.release_lock() 
  • trunk/cherrypy/test/test_session.py

    r1915 r1922  
    116116     
    117117    def test_0_Session(self): 
     118        self.getPage('/setsessiontype/ram') 
    118119        self.getPage('/clear') 
    119120         
     
    189190         
    190191        data_dict = {} 
     192        errors = [] 
    191193         
    192194        def request(index): 
     
    201203                c.endheaders() 
    202204                response = c.getresponse() 
    203                 self.assertEqual(response.status, 200) 
    204205                body = response.read() 
    205                 self.failUnless(body.isdigit()) 
     206                if response.status != 200 or not body.isdigit(): 
     207                    errors.append((response.status, body)) 
     208                else: 
     209                    data_dict[index] = max(data_dict[index], int(body)) 
    206210                # Uncomment the following line to prove threads overlap. 
    207211##                print index, 
    208             data_dict[index] = max(data_dict[index], int(body)) 
    209212         
    210213        # Start <request_count> requests from each of 
     
    222225        hitcount = max(data_dict.values()) 
    223226        expected = 1 + (client_thread_count * request_count) 
     227         
     228        for e in errors: 
     229            print e 
    224230        self.assertEqual(hitcount, expected) 
    225231     
     
    310316            self.getPage('/testStr', self.cookies) 
    311317            self.assertBody('3') 
     318            self.getPage('/length', self.cookies) 
     319            self.assertErrorPage(500) 
     320            self.assertInBody("NotImplementedError") 
    312321            self.getPage('/delkey?key=counter', self.cookies) 
    313322            self.assertStatus(200) 
     
    382391 
    383392 
    384  
    385393if __name__ == "__main__": 
    386394    setup_server() 

Hosted by WebFaction

Log in as guest/cpguest to create tickets