Skip to content

Commit 09c1c0d

Browse files
committed
tests/tornado: enhance test_concurrent_requests
This tries to enhance test_concurrent_requests by waiting until all responses have completed. There's still a default timeout set by tornado testing framework (5 seconds) that can be exceeded, but that should be more solid in practice than the previous 0.5 seconds delay that was used. We also make sure threads worked fine by joining them rather than setting them as daemons. This also makes sure we use `tornado.gen.sleep` function when available. Fixes #914
1 parent a7d6f3a commit 09c1c0d

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

tests/contrib/tornado/test_safety.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,35 @@ class TestAsyncConcurrency(TornadoTestCase):
1717
"""
1818
@gen_test
1919
def test_concurrent_requests(self):
20+
REQUESTS_NUMBER = 25
21+
responses = []
22+
2023
# the application must handle concurrent calls
2124
def make_requests():
2225
# use a blocking HTTP client (we're in another thread)
2326
http_client = httpclient.HTTPClient()
2427
url = self.get_url('/nested/')
2528
response = http_client.fetch(url)
29+
responses.append(response)
2630
assert 200 == response.code
2731
assert 'OK' == response.body.decode('utf-8')
2832
# freeing file descriptors
2933
http_client.close()
3034

3135
# blocking call executed in different threads
32-
threads = [threading.Thread(target=make_requests) for _ in range(25)]
36+
threads = [threading.Thread(target=make_requests) for _ in range(REQUESTS_NUMBER)]
3337
for t in threads:
34-
t.daemon = True
3538
t.start()
3639

37-
# wait for the execution; assuming this time as a timeout
38-
yield web.compat.sleep(0.5)
40+
while len(responses) < REQUESTS_NUMBER:
41+
yield web.compat.sleep(0.001)
42+
43+
for t in threads:
44+
t.join()
3945

4046
# the trace is created
4147
traces = self.tracer.writer.pop_traces()
42-
assert 25 == len(traces)
48+
assert REQUESTS_NUMBER == len(traces)
4349
assert 2 == len(traces[0])
4450

4551

tests/contrib/tornado/web/compat.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from tornado.concurrent import Future
2+
import tornado.gen
23
from tornado.ioloop import IOLoop
34

45

@@ -24,12 +25,16 @@ def __init__(self, *args, **kwargs):
2425
super(ThreadPoolExecutor, self).__init__()
2526

2627

27-
def sleep(duration):
28-
"""
29-
Compatibility helper that return a Future() that can be yielded.
30-
This is used because Tornado 4.0 doesn't have a ``gen.sleep()``
31-
function, that we require to test the ``TracerStackContext``.
32-
"""
33-
f = Future()
34-
IOLoop.current().call_later(duration, lambda: f.set_result(None))
35-
return f
28+
if hasattr(tornado.gen, 'sleep'):
29+
sleep = tornado.gen.sleep
30+
else:
31+
# Tornado <= 4.0
32+
def sleep(duration):
33+
"""
34+
Compatibility helper that return a Future() that can be yielded.
35+
This is used because Tornado 4.0 doesn't have a ``gen.sleep()``
36+
function, that we require to test the ``TracerStackContext``.
37+
"""
38+
f = Future()
39+
IOLoop.current().call_later(duration, lambda: f.set_result(None))
40+
return f

0 commit comments

Comments
 (0)