Make the streaming tests somewhat more stable

This commit is contained in:
Lorenz Diener 2018-07-14 01:04:06 +02:00
pare 21464aa3c6
commit 91de8ca061

Veure arxiu

@ -8,43 +8,52 @@ from mastodon import Mastodon
import threading import threading
import time import time
import select
# For monkeypatching so we can make vcrpy better # For monkeypatching so we can make vcrpy better
import vcr.stubs import vcr.stubs
streamingIsPatched = False streaming_is_patched = False
realConnections = [] real_connections = []
close_connections = False
def patchStreaming(): def patchStreaming():
global streamingIsPatched global streaming_is_patched
if streamingIsPatched == True: global close_connections
if streaming_is_patched == True:
return return
streamingIsPatched = True streaming_is_patched = True
realGetResponse = vcr.stubs.VCRConnection.getresponse real_get_response = vcr.stubs.VCRConnection.getresponse
def fakeGetResponse(*args, **kwargs): def fake_get_response(*args, **kwargs):
global close_connections
close_connections = False
if args[0]._vcr_request.path.startswith("/api/v1/streaming/"): if args[0]._vcr_request.path.startswith("/api/v1/streaming/"):
realConnections.append(args[0].real_connection) real_connections.append(args[0].real_connection)
realConnectionRealGetresponse = args[0].real_connection.getresponse real_connection_real_get_response = args[0].real_connection.getresponse
def fakeRealConnectionGetresponse(*args, **kwargs): def fakeRealConnectionGetresponse(*args, **kwargs):
response = realConnectionRealGetresponse(*args, **kwargs) response = real_connection_real_get_response(*args, **kwargs)
real_body = b"" real_body = b""
try: try:
while True: while close_connections == False:
chunk = response.read(1) if len(select.select([response], [], [], 0.01)[0]) > 0:
real_body += chunk chunk = response.read(1)
real_body += chunk
except AttributeError: except AttributeError:
pass # Connection closed pass # Connection closed
print(real_body)
response.read = (lambda: real_body) response.read = (lambda: real_body)
return response return response
args[0].real_connection.getresponse = fakeRealConnectionGetresponse args[0].real_connection.getresponse = fakeRealConnectionGetresponse
return realGetResponse(*args, **kwargs) return real_get_response(*args, **kwargs)
vcr.stubs.VCRConnection.getresponse = fakeGetResponse vcr.stubs.VCRConnection.getresponse = fake_get_response
def streamingClose(): def streaming_close():
global realConnections global real_connections
for connection in realConnections: for connection in real_connections:
connection.close() connection.close()
realConnections = [] real_connections = []
close_connections = True
class Listener(StreamListener): class Listener(StreamListener):
def __init__(self): def __init__(self):
@ -296,8 +305,8 @@ def test_stream_user(api, api2):
posted.append(api2.status_post("on the internet, nobody knows you're a plane")) posted.append(api2.status_post("on the internet, nobody knows you're a plane"))
time.sleep(1) time.sleep(1)
api.status_delete(posted[0]) api.status_delete(posted[0])
time.sleep(2) time.sleep(10)
streamingClose() streaming_close()
t = threading.Thread(args=(), target=do_activities) t = threading.Thread(args=(), target=do_activities)
t.start() t.start()
@ -305,7 +314,7 @@ def test_stream_user(api, api2):
stream = None stream = None
try: try:
stream = api.stream_user(listener, run_async=True) stream = api.stream_user(listener, run_async=True)
time.sleep(13) time.sleep(20)
finally: finally:
if stream != None: if stream != None:
stream.close() stream.close()
@ -338,8 +347,8 @@ def test_stream_user_local(api, api2):
def do_activities(): def do_activities():
time.sleep(5) time.sleep(5)
posted.append(api.status_post("it's cool guy")) posted.append(api.status_post("it's cool guy"))
time.sleep(3) time.sleep(10)
streamingClose() streaming_close()
t = threading.Thread(args=(), target=do_activities) t = threading.Thread(args=(), target=do_activities)
t.start() t.start()
@ -347,7 +356,7 @@ def test_stream_user_local(api, api2):
stream = None stream = None
try: try:
stream = api.stream_user(listener, run_async=True) stream = api.stream_user(listener, run_async=True)
time.sleep(13) time.sleep(20)
finally: finally:
if stream != None: if stream != None:
stream.close() stream.close()
@ -355,4 +364,4 @@ def test_stream_user_local(api, api2):
assert len(updates) == 1 assert len(updates) == 1
assert updates[0].id == posted[0].id assert updates[0].id == posted[0].id
t.join() t.join()