-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmultiprocess_test.py
108 lines (84 loc) · 2.82 KB
/
multiprocess_test.py
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
"""
Pattern:
Publishing-Client -> PubSubServer -> Subscribing->Client
"""
import os
import sys
import pytest
import uvicorn
import asyncio
from multiprocessing import Process, Event as ProcEvent
from fastapi import FastAPI
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
logger = get_logger("Test")
# Configurable
PORT = int(os.environ.get("PORT") or "7990")
uri = f"ws://localhost:{PORT}/pubsub"
DATA = "MAGIC"
EVENT_TOPIC = "event/has-happened"
CLIENT_START_SYNC = ProcEvent()
def setup_server():
app = FastAPI()
# PubSub websocket endpoint
endpoint = PubSubEndpoint()
endpoint.register_route(app, path="/pubsub")
uvicorn.run(app, port=PORT)
def setup_publishing_client():
"""
this client will publish an event to the main-test client
"""
async def actual():
# Wait for other client to wake up before publishing to it
CLIENT_START_SYNC.wait(5)
logger.info("Client start sync done")
# Create a client and subscribe to topics
client = PubSubClient()
client.start_client(uri)
# wait for the client to be ready
await client.wait_until_ready()
# publish event
logger.info("Publishing event")
published = await client.publish([EVENT_TOPIC], data=DATA)
assert published.result
logger.info("Starting async publishing client")
asyncio.run(actual())
@pytest.fixture(scope="module")
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.fixture(scope="module")
def pub_client():
# Run the server as a separate process
proc = Process(target=setup_publishing_client, args=(), daemon=True)
proc.start()
yield proc
proc.kill() # Cleanup after test
@pytest.mark.asyncio
async def test_pub_sub_multi_client(server, pub_client):
# 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
assert topic == EVENT_TOPIC
finish.set()
# subscribe for the event
logger.info("Subscribing for events")
client.subscribe(EVENT_TOPIC, on_event)
# start listentining
client.start_client(uri)
await client.wait_until_ready()
# Let the other client know we're ready
logger.info("First client is ready")
CLIENT_START_SYNC.set()
# wait for finish trigger
await asyncio.wait_for(finish.wait(), 10)