diff options
author | Lorenz Diener <[email protected]> | 2018-07-14 01:04:06 +0200 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2018-07-14 01:04:06 +0200 |
commit | 91de8ca061524712cae1d1b327cc2b2881c37548 (patch) | |
tree | b859622acca64551e905f927b4e0a3aa36f31d45 /tests | |
parent | 21464aa3c66401b022e49eac4d279ba380dcaf16 (diff) | |
download | mastodon.py-91de8ca061524712cae1d1b327cc2b2881c37548.tar.gz |
Make the streaming tests somewhat more stable
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_streaming.py | 61 |
1 files changed, 35 insertions, 26 deletions
diff --git a/tests/test_streaming.py b/tests/test_streaming.py index 605ce89..2e52f35 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py | |||
@@ -8,43 +8,52 @@ from mastodon import Mastodon | |||
8 | import threading | 8 | import threading |
9 | import time | 9 | import time |
10 | 10 | ||
11 | import select | ||
12 | |||
11 | # For monkeypatching so we can make vcrpy better | 13 | # For monkeypatching so we can make vcrpy better |
12 | import vcr.stubs | 14 | import vcr.stubs |
13 | 15 | ||
14 | streamingIsPatched = False | 16 | streaming_is_patched = False |
15 | realConnections = [] | 17 | real_connections = [] |
18 | close_connections = False | ||
16 | 19 | ||
17 | def patchStreaming(): | 20 | def patchStreaming(): |
18 | global streamingIsPatched | 21 | global streaming_is_patched |
19 | if streamingIsPatched == True: | 22 | global close_connections |
23 | if streaming_is_patched == True: | ||
20 | return | 24 | return |
21 | streamingIsPatched = True | 25 | streaming_is_patched = True |
22 | 26 | ||
23 | realGetResponse = vcr.stubs.VCRConnection.getresponse | 27 | real_get_response = vcr.stubs.VCRConnection.getresponse |
24 | def fakeGetResponse(*args, **kwargs): | 28 | def fake_get_response(*args, **kwargs): |
29 | global close_connections | ||
30 | close_connections = False | ||
25 | if args[0]._vcr_request.path.startswith("/api/v1/streaming/"): | 31 | if args[0]._vcr_request.path.startswith("/api/v1/streaming/"): |
26 | realConnections.append(args[0].real_connection) | 32 | real_connections.append(args[0].real_connection) |
27 | realConnectionRealGetresponse = args[0].real_connection.getresponse | 33 | real_connection_real_get_response = args[0].real_connection.getresponse |
28 | def fakeRealConnectionGetresponse(*args, **kwargs): | 34 | def fakeRealConnectionGetresponse(*args, **kwargs): |
29 | response = realConnectionRealGetresponse(*args, **kwargs) | 35 | response = real_connection_real_get_response(*args, **kwargs) |
30 | real_body = b"" | 36 | real_body = b"" |
31 | try: | 37 | try: |
32 | while True: | 38 | while close_connections == False: |
33 | chunk = response.read(1) | 39 | if len(select.select([response], [], [], 0.01)[0]) > 0: |
34 | real_body += chunk | 40 | chunk = response.read(1) |
41 | real_body += chunk | ||
35 | except AttributeError: | 42 | except AttributeError: |
36 | pass # Connection closed | 43 | pass # Connection closed |
44 | print(real_body) | ||
37 | response.read = (lambda: real_body) | 45 | response.read = (lambda: real_body) |
38 | return response | 46 | return response |
39 | args[0].real_connection.getresponse = fakeRealConnectionGetresponse | 47 | args[0].real_connection.getresponse = fakeRealConnectionGetresponse |
40 | return realGetResponse(*args, **kwargs) | 48 | return real_get_response(*args, **kwargs) |
41 | vcr.stubs.VCRConnection.getresponse = fakeGetResponse | 49 | vcr.stubs.VCRConnection.getresponse = fake_get_response |
42 | 50 | ||
43 | def streamingClose(): | 51 | def streaming_close(): |
44 | global realConnections | 52 | global real_connections |
45 | for connection in realConnections: | 53 | for connection in real_connections: |
46 | connection.close() | 54 | connection.close() |
47 | realConnections = [] | 55 | real_connections = [] |
56 | close_connections = True | ||
48 | 57 | ||
49 | class Listener(StreamListener): | 58 | class Listener(StreamListener): |
50 | def __init__(self): | 59 | def __init__(self): |
@@ -296,8 +305,8 @@ def test_stream_user(api, api2): | |||
296 | posted.append(api2.status_post("on the internet, nobody knows you're a plane")) | 305 | posted.append(api2.status_post("on the internet, nobody knows you're a plane")) |
297 | time.sleep(1) | 306 | time.sleep(1) |
298 | api.status_delete(posted[0]) | 307 | api.status_delete(posted[0]) |
299 | time.sleep(2) | 308 | time.sleep(10) |
300 | streamingClose() | 309 | streaming_close() |
301 | 310 | ||
302 | t = threading.Thread(args=(), target=do_activities) | 311 | t = threading.Thread(args=(), target=do_activities) |
303 | t.start() | 312 | t.start() |
@@ -305,7 +314,7 @@ def test_stream_user(api, api2): | |||
305 | stream = None | 314 | stream = None |
306 | try: | 315 | try: |
307 | stream = api.stream_user(listener, run_async=True) | 316 | stream = api.stream_user(listener, run_async=True) |
308 | time.sleep(13) | 317 | time.sleep(20) |
309 | finally: | 318 | finally: |
310 | if stream != None: | 319 | if stream != None: |
311 | stream.close() | 320 | stream.close() |
@@ -338,8 +347,8 @@ def test_stream_user_local(api, api2): | |||
338 | def do_activities(): | 347 | def do_activities(): |
339 | time.sleep(5) | 348 | time.sleep(5) |
340 | posted.append(api.status_post("it's cool guy")) | 349 | posted.append(api.status_post("it's cool guy")) |
341 | time.sleep(3) | 350 | time.sleep(10) |
342 | streamingClose() | 351 | streaming_close() |
343 | 352 | ||
344 | t = threading.Thread(args=(), target=do_activities) | 353 | t = threading.Thread(args=(), target=do_activities) |
345 | t.start() | 354 | t.start() |
@@ -347,7 +356,7 @@ def test_stream_user_local(api, api2): | |||
347 | stream = None | 356 | stream = None |
348 | try: | 357 | try: |
349 | stream = api.stream_user(listener, run_async=True) | 358 | stream = api.stream_user(listener, run_async=True) |
350 | time.sleep(13) | 359 | time.sleep(20) |
351 | finally: | 360 | finally: |
352 | if stream != None: | 361 | if stream != None: |
353 | stream.close() | 362 | stream.close() |
@@ -355,4 +364,4 @@ def test_stream_user_local(api, api2): | |||
355 | assert len(updates) == 1 | 364 | assert len(updates) == 1 |
356 | assert updates[0].id == posted[0].id | 365 | assert updates[0].id == posted[0].id |
357 | 366 | ||
358 | t.join() \ No newline at end of file | 367 | t.join() |