aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_streaming.py')
-rw-r--r--tests/test_streaming.py145
1 files changed, 141 insertions, 4 deletions
diff --git a/tests/test_streaming.py b/tests/test_streaming.py
index ac8e691..33d1381 100644
--- a/tests/test_streaming.py
+++ b/tests/test_streaming.py
@@ -1,11 +1,51 @@
1import six 1import six
2import pytest 2import pytest
3import itertools 3import itertools
4from mastodon.streaming import StreamListener 4from mastodon.streaming import StreamListener, CallbackStreamListener
5from mastodon.Mastodon import MastodonMalformedEventError 5from mastodon.Mastodon import MastodonMalformedEventError
6 6from mastodon import Mastodon
7 7
8 8import threading
9import time
10
11# For monkeypatching so we can make vcrpy better
12import vcr.stubs
13
14streamingIsPatched = False
15realConnections = []
16
17def patchStreaming():
18 global streamingIsPatched
19 if streamingIsPatched == True:
20 return
21 streamingIsPatched = True
22
23 realGetResponse = vcr.stubs.VCRConnection.getresponse
24 def fakeGetResponse(*args, **kwargs):
25 if args[0]._vcr_request.path.startswith("/api/v1/streaming/"):
26 realConnections.append(args[0].real_connection)
27 realConnectionRealGetresponse = args[0].real_connection.getresponse
28 def fakeRealConnectionGetresponse(*args, **kwargs):
29 response = realConnectionRealGetresponse(*args, **kwargs)
30 real_body = b""
31 try:
32 while True:
33 chunk = response.read(1)
34 real_body += chunk
35 except AttributeError:
36 pass # Connection closed
37 response.read = (lambda: real_body)
38 return response
39 args[0].real_connection.getresponse = fakeRealConnectionGetresponse
40 return realGetResponse(*args, **kwargs)
41 vcr.stubs.VCRConnection.getresponse = fakeGetResponse
42
43def streamingClose():
44 global realConnections
45 for connection in realConnections:
46 connection.close()
47 realConnections = []
48
9class Listener(StreamListener): 49class Listener(StreamListener):
10 def __init__(self): 50 def __init__(self):
11 self.updates = [] 51 self.updates = []
@@ -114,6 +154,25 @@ def test_unknown_event():
114 assert listener.deletes == [] 154 assert listener.deletes == []
115 assert listener.heartbeats == 0 155 assert listener.heartbeats == 0
116 156
157def test_invalid_event():
158 """But not too tolerant"""
159 listener = Listener()
160 with pytest.raises(MastodonMalformedEventError):
161 listener.handle_stream_([
162 'event: whatup',
163 'data: {}',
164 '',
165 ])
166
167def test_invalid_json():
168 """But not too tolerant"""
169 listener = Listener()
170 with pytest.raises(MastodonMalformedEventError):
171 listener.handle_stream_([
172 'event: blahblah',
173 'data: {kjaslkdjalskdjasd asdkjhak ajdasldasd}',
174 '',
175 ])
117 176
118def test_missing_event_name(): 177def test_missing_event_name():
119 listener = Listener() 178 listener = Listener()
@@ -210,3 +269,81 @@ def test_multiline_payload():
210 '', 269 '',
211 ]) 270 ])
212 assert listener.updates == [{"foo": "bar"}] 271 assert listener.updates == [{"foo": "bar"}]
272
273@pytest.mark.vcr(match_on=['path'])
274def test_stream_user(api, api2):
275 patchStreaming()
276
277 updates = []
278 notifications = []
279 deletes = []
280 listener = CallbackStreamListener(
281 update_handler = lambda x: updates.append(x),
282 notification_handler = lambda x: notifications.append(x),
283 delete_handler = lambda x: deletes.append(x)
284 )
285
286 posted = []
287 def do_activities():
288 time.sleep(5)
289 posted.append(api.status_post("only real cars respond."))
290 posted.append(api2.status_post("@mastodonpy_test beep beep I'm a jeep"))
291 posted.append(api2.status_post("on the internet, nobody knows you're a plane"))
292 time.sleep(1)
293 api.status_delete(posted[0])
294 time.sleep(2)
295 streamingClose()
296
297 t = threading.Thread(args=(), target=do_activities)
298 t.start()
299
300 stream = None
301 try:
302 stream = api.stream_user(listener, run_async=True)
303 time.sleep(13)
304 finally:
305 if stream != None:
306 stream.close()
307
308 assert len(updates) == 1
309 assert len(notifications) == 1
310 assert len(deletes) == 1
311
312 assert updates[0].id == posted[0].id
313 assert deletes[0] == posted[0].id
314 assert notifications[0].status.id == posted[1].id
315
316 t.join()
317
318@pytest.mark.vcr(match_on=['path'])
319def test_stream_user_local(api, api2):
320 patchStreaming()
321
322 updates = []
323 notifications = []
324 listener = CallbackStreamListener(
325 local_update_handler = lambda x: updates.append(x),
326 )
327
328 posted = []
329 def do_activities():
330 time.sleep(5)
331 posted.append(api.status_post("it's cool guy"))
332 time.sleep(3)
333 streamingClose()
334
335 t = threading.Thread(args=(), target=do_activities)
336 t.start()
337
338 stream = None
339 try:
340 stream = api.stream_user(listener, run_async=True)
341 time.sleep(13)
342 finally:
343 if stream != None:
344 stream.close()
345
346 assert len(updates) == 1
347 assert updates[0].id == posted[0].id
348
349 t.join() \ No newline at end of file
Powered by cgit v1.2.3 (git 2.41.0)