Skip to content

Commit

Permalink
Go ahead and pin the whole of any POST request to master.
Browse files Browse the repository at this point in the history
Refactor middleware tests to save duplication.
  • Loading branch information
erikrose committed Jun 18, 2010
1 parent b04f1e7 commit 25ca0c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
7 changes: 1 addition & 6 deletions multidb/middleware.py
Expand Up @@ -27,17 +27,12 @@ class PinningRouterMiddleware(object):
def process_request(self, request):
"""Set the thread's pinning flag according to the presence of the
incoming cookie."""
if PINNING_COOKIE in request.COOKIES:
if PINNING_COOKIE in request.COOKIES or request.method == 'POST':
pin_this_thread()
else:
# In case the last request this thread served was pinned:
unpin_this_thread()

# We could also set the pinning flag at the top of POST requests.
# But, at the moment, we rely on ourselves to be smart enough to
# manually direct the necessary requests to the master within the scope
# of one request. Too optimistic?

def process_response(self, request, response):
"""On a POST request, assume there was a DB write and set the cookie.
Expand Down
39 changes: 24 additions & 15 deletions multidb/tests/__init__.py
Expand Up @@ -79,30 +79,39 @@ def test_pinned_reads(self):
class MiddlewareTests(UnpinningTestCase):
"""Tests for the middleware that supports pinning"""

def test_process_request(self):
"""Make sure the thread gets set as pinned when the cookie is on the
request and as unpinned when it isn't."""
request = HttpRequest()
request.COOKIES[PINNING_COOKIE] = 'y'

middleware = PinningRouterMiddleware()
middleware.process_request(request)
def setUp(self):
super(MiddlewareTests, self).setUp()

# Every test uses these, so they're okay as attrs.
self.request = HttpRequest()
self.middleware = PinningRouterMiddleware()

def test_pin_on_cookie(self):
"""Thread should pin when the cookie is set."""
self.request.COOKIES[PINNING_COOKIE] = 'y'
self.middleware.process_request(self.request)
assert this_thread_is_pinned()

del request.COOKIES[PINNING_COOKIE]
middleware.process_request(request)
def test_unpin_on_no_cookie(self):
"""Thread should unpin when cookie is absent and method isn't POST."""
pin_this_thread()
self.middleware.process_request(self.request)
assert not this_thread_is_pinned()

def test_pin_on_post(self):
"""Thread should pin when method is POST."""
self.request.method = 'POST'
self.middleware.process_request(self.request)
assert this_thread_is_pinned()

def test_process_response(self):
"""Make sure the cookie gets set on POST requests and not otherwise."""
request = HttpRequest()
middleware = PinningRouterMiddleware()

response = middleware.process_response(request, HttpResponse())
response = self.middleware.process_response(self.request, HttpResponse())
assert PINNING_COOKIE not in response.cookies

request.method = 'POST'
response = middleware.process_response(request, HttpResponse())
self.request.method = 'POST'
response = self.middleware.process_response(self.request, HttpResponse())
assert PINNING_COOKIE in response.cookies
eq_(response.cookies[PINNING_COOKIE]['max-age'],
PINNING_SECONDS)

0 comments on commit 25ca0c6

Please sign in to comment.