Skip to content

Commit

Permalink
Fix HTTP client selection as used in curl_httpclient_test.
Browse files Browse the repository at this point in the history
AsyncHTTPClient.configure() was working, but it didn't work to instantiate
the client directly like the unit tests were using.
  • Loading branch information
bdarnell committed Jul 19, 2011
1 parent 0d31f1c commit 5d895f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
32 changes: 19 additions & 13 deletions tornado/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,37 @@ def handle_request(response):
are deprecated. The implementation subclass as well as arguments to
its constructor can be set with the static method configure()
"""
_async_clients = weakref.WeakKeyDictionary()
_impl_class = None
_impl_kwargs = None

@classmethod
def _async_clients(cls):
assert cls is not AsyncHTTPClient, "should only be called on subclasses"
if not hasattr(cls, '_async_client_dict'):
cls._async_client_dict = weakref.WeakKeyDictionary()
return cls._async_client_dict

def __new__(cls, io_loop=None, max_clients=10, force_instance=False,
**kwargs):
io_loop = io_loop or IOLoop.instance()
if io_loop in cls._async_clients and not force_instance:
return cls._async_clients[io_loop]
if cls is AsyncHTTPClient:
if cls._impl_class is None:
from tornado.simple_httpclient import SimpleAsyncHTTPClient
AsyncHTTPClient._impl_class = SimpleAsyncHTTPClient
impl = AsyncHTTPClient._impl_class
else:
impl = cls
if io_loop in impl._async_clients() and not force_instance:
return impl._async_clients()[io_loop]
else:
if cls is AsyncHTTPClient:
if cls._impl_class is None:
from tornado.simple_httpclient import SimpleAsyncHTTPClient
AsyncHTTPClient._impl_class = SimpleAsyncHTTPClient
impl = cls._impl_class
else:
impl = cls
instance = super(AsyncHTTPClient, cls).__new__(impl)
args = {}
if cls._impl_kwargs:
args.update(cls._impl_kwargs)
args.update(kwargs)
instance.initialize(io_loop, max_clients, **args)
if not force_instance:
cls._async_clients[io_loop] = instance
impl._async_clients()[io_loop] = instance
return instance

def close(self):
Expand All @@ -144,8 +150,8 @@ def close(self):
create and destroy http clients. No other methods may be called
on the AsyncHTTPClient after close().
"""
if self._async_clients.get(self.io_loop) is self:
del self._async_clients[self.io_loop]
if self._async_clients().get(self.io_loop) is self:
del self._async_clients()[self.io_loop]

def fetch(self, request, callback, **kwargs):
"""Executes a request, calling callback with an `HTTPResponse`.
Expand Down
5 changes: 4 additions & 1 deletion tornado/test/curl_httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

class CurlHTTPClientCommonTestCase(HTTPClientCommonTestCase):
def get_http_client(self):
return CurlAsyncHTTPClient(io_loop=self.io_loop)
client = CurlAsyncHTTPClient(io_loop=self.io_loop)
# make sure AsyncHTTPClient magic doesn't give us the wrong class
self.assertTrue(isinstance(client, CurlAsyncHTTPClient))
return client

# Remove the base class from our namespace so the unittest module doesn't
# try to run it again.
Expand Down
6 changes: 4 additions & 2 deletions tornado/test/simple_httpclient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

class SimpleHTTPClientCommonTestCase(HTTPClientCommonTestCase):
def get_http_client(self):
return SimpleAsyncHTTPClient(io_loop=self.io_loop,
force_instance=True)
client = SimpleAsyncHTTPClient(io_loop=self.io_loop,
force_instance=True)
self.assertTrue(isinstance(client, SimpleAsyncHTTPClient))
return client

# Remove the base class from our namespace so the unittest module doesn't
# try to run it again.
Expand Down

0 comments on commit 5d895f0

Please sign in to comment.