-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathbasic_test.py
More file actions
179 lines (150 loc) · 5.57 KB
/
basic_test.py
File metadata and controls
179 lines (150 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import os
import sys
import pytest
import asyncio
import uvicorn
import requests
from fastapi import FastAPI
from multiprocessing import Process
from fastapi_websocket_rpc.utils import gen_uid
from fastapi_websocket_rpc.logger import get_logger
# Add parent path to use local src as package for tests
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))
)
from fastapi_websocket_pubsub import PubSubEndpoint, PubSubClient
from fastapi_websocket_pubsub.event_notifier import ALL_TOPICS
logger = get_logger("Test")
# Configurable
PORT = int(os.environ.get("PORT") or "7990")
uri = f"ws://localhost:{PORT}/pubsub"
trigger_url = f"http://localhost:{PORT}/trigger"
DATA = "MAGIC"
EVENT_TOPIC = "event/has-happened"
def setup_server_rest_route(app, endpoint: PubSubEndpoint):
@app.get("/trigger")
async def trigger_events():
logger.info("Triggered via HTTP route - publishing event")
# Publish an event named 'steel'
# Since we are calling back (RPC) to the client- this would deadlock if we wait on it
asyncio.create_task(endpoint.publish([EVENT_TOPIC], data=DATA))
return "triggered"
def setup_server():
app = FastAPI()
# PubSub websocket endpoint
endpoint = PubSubEndpoint()
endpoint.register_route(app, path="/pubsub")
# Regular REST endpoint - that publishes to PubSub
setup_server_rest_route(app, endpoint)
uvicorn.run(app, port=PORT)
@pytest.fixture()
def server():
# Run the server as a separate process
proc = Process(target=setup_server, args=(), daemon=True)
proc.start()
yield proc
proc.kill() # Cleanup after test
@pytest.mark.asyncio
async def test_subscribe_http_trigger(server):
# finish trigger
finish = asyncio.Event()
# Create a client and subscribe to topics
async with PubSubClient() as client:
async def on_event(data, topic):
assert data == DATA
finish.set()
# subscribe for the event
client.subscribe(EVENT_TOPIC, on_event)
# start listentining
client.start_client(uri)
# wait for the client to be ready to receive events
await client.wait_until_ready()
# trigger the server via an HTTP route
requests.get(trigger_url)
# wait for finish trigger
await asyncio.wait_for(finish.wait(), 5)
@pytest.mark.asyncio
async def test_pub_sub(server):
# finish trigger
finish = asyncio.Event()
# Create a client and subscribe to topics
async with PubSubClient() as client:
async def on_event(data, topic):
assert data == DATA
finish.set()
# subscribe for the event
client.subscribe(EVENT_TOPIC, on_event)
# start listentining
client.start_client(uri)
# wait for the client to be ready to receive events
await client.wait_until_ready()
# publish events (with sync=False toa void deadlocks waiting on the publish to ourselves)
published = await client.publish(
[EVENT_TOPIC], data=DATA, sync=False, notifier_id=gen_uid()
)
assert published.result
# wait for finish trigger
await asyncio.wait_for(finish.wait(), 5)
@pytest.mark.asyncio
async def test_pub_sub_with_all_topics(server):
"""
Check client gets event when subscribing via ALL_TOPICS
"""
# finish trigger
finish = asyncio.Event()
# Create a client and subscribe to topics
async with PubSubClient() as client:
async def on_event(data, topic):
assert data == DATA
finish.set()
# subscribe for the event
client.subscribe(ALL_TOPICS, on_event)
# start listentining
client.start_client(uri)
# wait for the client to be ready to receive events
await client.wait_until_ready()
# publish events (with sync=False toa void deadlocks waiting on the publish to ourselves)
published = await client.publish(
[EVENT_TOPIC], data=DATA, sync=False, notifier_id=gen_uid()
)
assert published.result
# wait for finish trigger
await asyncio.wait_for(finish.wait(), 5)
@pytest.mark.asyncio
async def test_pub_sub_unsub(server):
"""
Check client can unsubscribe topic and subscribe again.
"""
# finish trigger
finish = asyncio.Event()
async with PubSubClient() as client:
async def on_event(data, topic):
assert data == DATA
finish.set()
# subscribe for the event
client.subscribe(EVENT_TOPIC, on_event)
# start listentining
client.start_client(uri)
# wait for the client to be ready to receive events
await client.wait_until_ready()
# trigger the server via an HTTP route
requests.get(trigger_url)
# wait for finish trigger
await asyncio.wait_for(finish.wait(), 5)
assert finish.is_set()
# unsubscribe and see that we don't get a message
finish.clear()
await client.unsubscribe(EVENT_TOPIC)
requests.get(trigger_url)
# wait for finish trigger which isn't coming
with pytest.raises(asyncio.TimeoutError) as excinfo:
await asyncio.wait_for(finish.wait(), 5)
assert not finish.is_set()
# subscribe again and observe that we get the trigger
finish.clear()
await client.subscribe(EVENT_TOPIC, on_event)
# trigger the server via an HTTP route
requests.get(trigger_url)
# wait for finish trigger
await asyncio.wait_for(finish.wait(), 5)
assert finish.is_set()