-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
245 lines (193 loc) Β· 6.53 KB
/
Copy pathtest2.py
File metadata and controls
245 lines (193 loc) Β· 6.53 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import requests
import websocket
from time import sleep, time
import uuid
from json import loads
from auth import User
MSG_BASE = "http://127.0.0.1:8080/messages"
WS_URL = "ws://127.0.0.1:8080/messages/ws/"
CONCURRENT_CONNECTIONS = 12
# -----------------------------
# Helpers
# -----------------------------
from threading import Thread
# timer utility
class Timer:
def __init__(self) -> None:
self.start = time()
@property
def value(self) -> float:
return time() - self.start
class WorkPool:
def __init__(self, nworkers, handler, work: list = []) -> None:
self.handler = handler
self.nworkers = nworkers
self.proceed = False
self._outputs: list[tuple] = [None for i in work] # type: ignore
self.threads: list[Thread] = []
self.work: list = work
def worker_handler(self):
while self.proceed and len(self.work) > 0:
i = len(self.work)
inp = self.work.pop()
self._outputs[i - 1] = (inp, self.handler(*inp))
def resume(self):
if not self.proceed:
self.threads = [
Thread(target=self.worker_handler) for i in range(self.nworkers)
]
self.proceed = True
for th in self.threads:
th.start()
def start(self):
self.resume()
return self
@property
def output(self):
return self._outputs
def pause(self):
self.proceed = False
for th in self.threads:
th.join()
def wait(self):
for th in self.threads:
th.join()
def user(name):
return {"username": f"{name}_{uuid.uuid4().hex[:6]}", "password": "password123"}
def create_conversation(session, token, participants):
with session.post(
f"{MSG_BASE}/conversations",
json={"participants": participants},
headers={"Authorization": f"Bearer {token}"},
) as r:
return (r.json())["name"]
def send_message(session, token, conv, text):
session.post(
f"{MSG_BASE}/conversations/{conv}/messages",
json={"text": text},
headers={"Authorization": f"Bearer {token}"},
)
def send_pmessage(session, token, peer, text):
session.post(
f"{MSG_BASE}/inbox/{peer}/messages",
json={"text": text},
headers={"Authorization": f"Bearer {token}"},
)
def fetch_messages(session, token, conv=None, peer=None):
if conv:
with session.get(
f"{MSG_BASE}/conversations/{conv}/messages",
headers={"Authorization": f"Bearer {token}"},
) as r:
return r.json()
if peer:
with session.get(
f"{MSG_BASE}/inbox/messages",
headers={"Authorization": f"Bearer {token}"},
params={"source": peer},
) as r:
return r.json()
def fetch_pmessages(session, token, conv):
with session.get(
f"{MSG_BASE}//{conv}/messages",
headers={"Authorization": f"Bearer {token}"},
) as r:
return r.json()
def fetch_receipts(session: requests.Session, token, message):
with session.get(
f"{MSG_BASE}/messages/{message}/receipts",
headers={"Authorization": f"Bearer {token}"},
) as r:
return r.json()
def ws_client(user: User, handler):
def on_message(ws, message):
print(message)
handler(loads(message))
def on_connect(ws):
print("connected", user.username)
ws = websocket.WebSocketApp(
WS_URL,
header={"Authorization": f"Bearer {user.access}"},
on_error=print,
on_message=on_message,
on_open=on_connect,
)
ws.run_forever()
# -----------------------------
# Test
# -----------------------------
if __name__ == "__main__":
session = requests.Session()
names = ["alice", "bob", "carol", "diana", "elvis", "felix"]
names = [f"{name}_{uuid.uuid4().hex[:6]}" for name in names]
users: list[str] = [user(name) for name in names] # type: ignore
print("π Registering users...")
work = [(u,) for u in names]
wp = WorkPool(10, User.register, work)
wp.start()
wp.wait()
work = [inp for inp, out in wp.output]
print("π Logging in...")
wp = WorkPool(len(work), User, work)
wp.start().wait()
users: list[User] = [out for inp, out in wp.output]
inboxes: list[list] = [[] for i in users]
print("π‘ Connecting WebSockets...")
usrs = [u for u in users]
WorkPool(
len(users),
ws_client,
[(u, t.append) for u, t in zip(usrs, inboxes)],
).start()
print(users)
# -----------------------------
# Peer-to-peer
# -----------------------------
print("π¬ Testing P2P conversation...")
# conv_p2p = create_conversation(session, users[0].access, [users[1].username])
send_pmessage(session, users[0].access, users[1].username, "P2P hello")
sleep(2)
print(inboxes)
history = fetch_messages(session, users[0].access, peer=users[1].username)
print(inboxes[1])
assert any("P2P hello" in m["text"] for m in inboxes[1])
print("β
P2P OK")
print("Testing message receipts")
receipts = fetch_receipts(session, users[0].access, history[0]["id"])
print(receipts)
# -----------------------------
# Group chat
# -----------------------------
print("π₯ Testing group chat...")
conv_group = create_conversation(
session, users[0].access, [u.username for u in users]
)
send_message(session, users[1].access, conv_group, "Hello group")
sleep(1)
print(inboxes)
for inbox in inboxes:
if inbox != inboxes[1]:
assert any("Hello group" in m["text"] for m in inbox)
history = fetch_messages(session, users[-1].access, conv=conv_group)
# assert any("Hello group" in m["text"] for m in history)
print("β
Group chat OK")
print("Testing load capacity")
msgs = 0
def func():
global msgs
send_message(session, users[0].access, conv_group, f"Hello for the th time")
msgs += 1
timer = Timer()
thds = []
wp = WorkPool(CONCURRENT_CONNECTIONS, func, [() for i in range(900)]).start()
wp.wait()
print(
f"took {timer.value} seconds to send {msgs} messages: {msgs/timer.value} req/sec"
)
print(
f"received {len(inboxes[1])} messages in {timer.value} seconds: {len(inboxes[1])/timer.value} msgs/sec"
)
sleep(2)
history = fetch_messages(session, users[-1].access, conv_group)
print(f"written {len(history)} messages in {timer.value} seconds")
print("\nπ ALL MESSAGE TESTS PASSED")