New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
treq.testing.StubTreq: fix persisting twisted.web.server.Session objects between requests #328
Changes from 1 commit
30a10bb
f8b6e25
00f1b82
c317256
bbf412c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
``treq.testing.StubTreq`` now persists ``twisted.web.server.Session`` instances between requests. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ def __init__(self, rootResource): | |
reactor=self._memoryReactor, | ||
endpointFactory=_EndpointFactory(self._memoryReactor)) | ||
self._rootResource = rootResource | ||
self._serverFactory = Site(self._rootResource, reactor=self._memoryReactor) | ||
self._pumps = set() | ||
|
||
def request(self, method, uri, headers=None, bodyProducer=None): | ||
|
@@ -126,8 +127,7 @@ def check_already_called(r): | |
# Create the protocol and fake transport for the client and server, | ||
# using the factory that was passed to the MemoryReactor for the | ||
# client, and a Site around our rootResource for the server. | ||
serverFactory = Site(self._rootResource, reactor=self._memoryReactor) | ||
serverProtocol = serverFactory.buildProtocol(clientAddress) | ||
serverProtocol = self._serverFactory.buildProtocol(clientAddress) | ||
serverTransport = iosim.FakeTransport( | ||
serverProtocol, isServer=True, | ||
hostAddress=serverAddress, peerAddress=clientAddress) | ||
|
@@ -228,8 +228,8 @@ def __init__(self, resource): | |
:param resource: A :obj:`Resource` object that provides the fake | ||
responses | ||
""" | ||
_agent = RequestTraversalAgent(resource) | ||
_client = HTTPClient(agent=_agent, | ||
self._agent = RequestTraversalAgent(resource) | ||
_client = HTTPClient(agent=self._agent, | ||
data_to_body_producer=_SynchronousProducer) | ||
for function_name in treq.__all__: | ||
function = getattr(_client, function_name, None) | ||
|
@@ -239,7 +239,17 @@ def __init__(self, resource): | |
function = _reject_files(function) | ||
|
||
setattr(self, function_name, function) | ||
self.flush = _agent.flush | ||
self.flush = self._agent.flush | ||
|
||
def cleanSessions(self): | ||
""" | ||
Clean up sessions to prevent leaving behind a dirty reactor. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behavior of leaving a dirty reactor looks like a bug in Twisted to me, actually. The session's delayed call should be registered with the reactor passed to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I filed https://twistedmatrix.com/trac/ticket/10177 for this issue. I think that it could be worked around here by overriding the site's |
||
If you are using :obj:`StubTreq` with :obj:`twisted.web.server.Session` | ||
objects, you most likely have to call this method once you are done, | ||
for example during the tearDown of a unittest TestCase. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For stuff like this using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @twm Are you suggesting replacing this whole method to somehow use said function or to change the documentation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To change the documentation, but it's a moot point if this method is going away. |
||
""" | ||
for sid in list(self._agent._serverFactory.sessions.keys()): | ||
self._agent._serverFactory.sessions[sid].expire() | ||
|
||
|
||
class StringStubbingResource(Resource): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that you can cause the session to use the correct reactor by overriding
sessionFactory
here like this:Then you don't need the cleanup code in the test you added, and more importantly you don't need cleanup code in any test that uses
RequestTraversalAgent
.