Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Attempt to aggressively close connections to prevent spurious unclean reactor errors. #22

Merged
merged 2 commits into from

2 participants

@dreid
Owner

No description provided.

@Julian

Maybe this hack could / should be shipped somewhere exposed in treq?

Users testing treq applications are going to want to do it too.

@dreid
Owner

I don't know that I think this should be a thing exposed by treq.

I definitely don't think exposing it to users should block this PR because, fixing these integration tests is already blocking other PRs.

Which brings me to the point of this PR. Is there a cleaner way to do it?

@dreid
Owner

Merging because @hynek says this fixes #21

@dreid dreid merged commit a60c38b into master
@Julian

Yes I definitely didn't mean to hold up merging this.

I have to play with it a bit more, if I come up with something I'll open anothe ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 6 deletions.
  1. +6 −5 treq/client.py
  2. +22 −1 treq/test/test_treq_integration.py
View
11 treq/client.py
@@ -71,11 +71,12 @@ def with_config(cls, **kwargs):
if not reactor:
from twisted.internet import reactor
- agent = Agent(
- reactor,
- pool=HTTPConnectionPool(
- reactor,
- persistent=kwargs.get('persistent', True)))
+ pool = kwargs.get('pool')
+ if not pool:
+ persistent = kwargs.get('persistent', True)
+ pool = HTTPConnectionPool(reactor, persitent=persistent)
+
+ agent = Agent(reactor, pool=pool)
if kwargs.get('allow_redirects', True):
agent = RedirectAgent(agent)
View
23 treq/test/test_treq_integration.py
@@ -1,5 +1,10 @@
from twisted.trial.unittest import TestCase
from twisted.internet.defer import inlineCallbacks
+from twisted.internet.task import deferLater
+from twisted.internet import reactor
+from twisted.internet.tcp import Client
+
+from twisted.web.client import HTTPConnectionPool
from treq.test.util import DEBUG, is_pypy, has_ssl
@@ -23,7 +28,7 @@ def print_response(response):
def with_baseurl(method):
def _request(self, url, *args, **kwargs):
- return method(self.baseurl + url, *args, persistent=False, **kwargs)
+ return method(self.baseurl + url, *args, pool=self.pool, **kwargs)
return _request
@@ -36,6 +41,22 @@ class TreqIntegrationTests(TestCase):
put = with_baseurl(treq.put)
delete = with_baseurl(treq.delete)
+ def setUp(self):
+ self.pool = HTTPConnectionPool(reactor, False)
+
+ def tearDown(self):
+ def _check_fds(_):
+ # This appears to only be necessary for HTTPS tests.
+ # For the normal HTTP tests then closeCachedConnections is
+ # sufficient.
+ fds = set(reactor.getReaders() + reactor.getReaders())
+ if not [fd for fd in fds if isinstance(fd, Client)]:
+ return
+
+ return deferLater(reactor, 0, _check_fds, None)
+
+ return self.pool.closeCachedConnections().addBoth(_check_fds)
+
@inlineCallbacks
def assert_data(self, response, expected_data):
body = yield treq.json_content(response)
Something went wrong with that request. Please try again.