Permalink
Browse files

Merge pull request #22 from dreid/clean-integration-tests

Attempt to aggressively close connections to prevent spurious unclean reactor errors.
  • Loading branch information...
2 parents 5c06b61 + a29f5c2 commit a60c38b0f916ebd082fe872b216c030080f3c750 @dreid dreid committed Jan 29, 2013
Showing with 28 additions and 6 deletions.
  1. +6 −5 treq/client.py
  2. +22 −1 treq/test/test_treq_integration.py
View
@@ -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)
@@ -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)

0 comments on commit a60c38b

Please sign in to comment.