def test_pinning(self): # Check to make sure the 'other' database shows in in reads first success = False for i in range(100): db = self.router.db_for_read(self.obj1) if db == 'other': success = True break self.assertTrue(success, "The 'other' database was not offered.") # Simulate a write self.router.db_for_write(self.obj1) # Check to make sure that only the master database shows up in reads, # since the thread should now be pinned success = True for i in range(100): db = self.router.db_for_read(self.obj1) if db == 'other': success = False break self.assertTrue(success, "The 'other' database was offered in error.") PinningMixin.unpin_thread() PinningMixin.clear_db_write()
def process_request(self, request): """ Set the thread's pinning flag according to the presence of the session variable. """ pinned_until = request.session.get(PINNING_KEY, False) if pinned_until and pinned_until > datetime.now(): PinningMixin.pin_thread()
def process_response(self, request, response): """ If there was a write to the db, set the session variable to enable pinning. If the variable already exists, the time will be reset. """ if PinningMixin.db_was_written(): pinned_until = datetime.now() + timedelta(seconds=PINNING_SECONDS) request.session[PINNING_KEY] = pinned_until PinningMixin.clear_db_write() PinningMixin.unpin_thread() return response
def process_response(self, request, response): """ If there was a write to the db, set the cookie to enable pinning. If the cookie already exists, the time will be reset. """ if PinningMixin.db_was_written(): response.set_cookie(PINNING_KEY, value='y', max_age=PINNING_SECONDS) PinningMixin.clear_db_write() PinningMixin.unpin_thread() return response
def nav_title(self): from balancer.mixins import PinningMixin pinned = PinningMixin.is_pinned() colours = dict() colours[True] = '#F66' colours[False] = '#6F6' colour = colours[pinned] message = 'pinned' if pinned else 'not pinned' title = _('SQL') + ' <span style="color:%s;">%s</span>' % (colour, message) return mark_safe(title)
def test_middleware(self): for middleware, vehicle in [(PinningSessionMiddleware(), 'session'), (PinningCookieMiddleware(), 'cookie')]: # The first request shouldn't pin the database middleware.process_request(self.mock_request) self.assertFalse(PinningMixin.is_pinned()) # A simulated write should, however PinningMixin.set_db_write() # The response should set the session variable and clear the locals response = middleware.process_response(self.mock_request, self.mock_response) self.assertFalse(PinningMixin.is_pinned()) self.assertFalse(PinningMixin.db_was_written()) if vehicle == 'session': self.assertTrue( self.mock_request.session.get(PINNING_KEY, False) ) else: self.assertEqual(response.cookie, PINNING_KEY) self.mock_request.COOKIES = [response.cookie] # The subsequent request should then pin the database middleware.process_request(self.mock_request) self.assertTrue(PinningMixin.is_pinned()) PinningMixin.unpin_thread() if vehicle == 'session': # After the pinning period has expired, the request should no # longer pin the thread exp = timedelta(seconds=PINNING_SECONDS - 5) self.mock_request.session[PINNING_KEY] = datetime.now() - exp middleware.process_request(self.mock_request) self.assertFalse(PinningMixin.is_pinned()) PinningMixin.unpin_thread()
def test_middleware(self): for middleware, vehicle in [(PinningSessionMiddleware(), 'session'), (PinningCookieMiddleware(), 'cookie')]: # The first request shouldn't pin the database middleware.process_request(self.mock_request) self.assertFalse(PinningMixin.is_pinned()) # A simulated write should, however PinningMixin.set_db_write() # The response should set the session variable and clear the locals response = middleware.process_response(self.mock_request, self.mock_response) self.assertFalse(PinningMixin.is_pinned()) self.assertFalse(PinningMixin.db_was_written()) if vehicle == 'session': self.assertTrue( self.mock_request.session.get(PINNING_KEY, False)) else: self.assertEqual(response.cookie, PINNING_KEY) self.mock_request.COOKIES = [response.cookie] # The subsequent request should then pin the database middleware.process_request(self.mock_request) self.assertTrue(PinningMixin.is_pinned()) PinningMixin.unpin_thread() if vehicle == 'session': # After the pinning period has expired, the request should no # longer pin the thread exp = timedelta(seconds=PINNING_SECONDS - 5) self.mock_request.session[PINNING_KEY] = datetime.now() - exp middleware.process_request(self.mock_request) self.assertFalse(PinningMixin.is_pinned()) PinningMixin.unpin_thread()
def process_request(self, request): """ Set the thread's pinning flag according to the presence of the cookie. """ if PINNING_KEY in request.COOKIES: PinningMixin.pin_thread()