-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
283 lines (215 loc) · 8.03 KB
/
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import annotations
import base64
import sys
import pathlib
from warnings import filterwarnings
try:
sys.path.insert(0, str(pathlib.Path(__file__).parent))
except Exception:
raise
import asyncio
import json
import queue
import socket
import sqlite3
import threading
from collections import OrderedDict
from collections.abc import Sequence, Mapping, Iterable
from http.client import responses
from random import randbytes
from traceback import format_exception
from typing import Callable, Coroutine, Any, Generator, Literal, IO, Hashable
from uuid import uuid4
import time
import multiprocessing
import random
import unittest
from src import wsbridge
from src.wsbridge import baseclient, simpleservice
filterwarnings(
'ignore',
message=r'^unclosed.*',
module="base_events",
category=ResourceWarning
)
def log_ok(*msg: object):
print("", end="", flush=True)
print("\x1b[32mOK:", *msg, "\x1b[m", flush=True)
def _intro():
log_ok(f"""
known warnings:
- "ResourceWarning: unclosed ..." (conflict with test environment)
- "ResourceWarning: Enable tracemalloc to get the object allocation traceback" (conflict with test environment)
- "Task was destroyed but it is pending!" (shutdown bug)
- "concurrent.futures._base.CancelledError" (shutdown bug)
""".strip())
for _ in range(3):
time.sleep(.5)
print(".", end="", flush=True)
print(flush=True)
_intro()
class _TestConnection(baseclient.Connection):
def __init__(self, server_host: str, server_port: int):
super().__init__(server_host, server_port)
self.connected = False
self.exp = False
def connect(self) -> bytes:
try:
self.connected = super().connect()
return self.connected
except Exception as e:
self.exp = e
self.connected = -1
def run(self):
self.connect()
super().run()
def start(self):
super(baseclient.Connection, self).start()
def wait_connected(self):
while not self.connected:
time.sleep(.001)
if self.exp:
raise self.exp
class TestBasics(unittest.TestCase):
def test_single(self):
port = 9990
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(("localhost", port))
server = wsbridge.SimpleService(sock, mediations_per_thread=1)
server.start(wait_iterations=True)
try:
client_a = _TestConnection("localhost", port)
client_a.start()
client_b = _TestConnection("localhost", port)
client_b.start()
client_a.wait_connected()
client_b.wait_connected()
client_a.send(b'42')
self.assertEqual(b'42', client_b.recv())
client_b.send(b'42')
self.assertEqual(b'42', client_a.recv())
client_c = _TestConnection("localhost", port)
client_c.start()
client_d = _TestConnection("localhost", port)
client_d.start()
with self.assertRaises(ConnectionResetError):
client_c.wait_connected()
client_d.wait_connected()
client_a.send(b'42')
self.assertEqual(b'42', client_b.recv())
client_b.send(b'42')
self.assertEqual(b'42', client_a.recv())
client_a.close()
client_b.recv()
client_c = _TestConnection("localhost", port)
client_c.start()
client_d = _TestConnection("localhost", port)
client_d.start()
client_c.wait_connected()
client_d.wait_connected()
client_c.send(b'42')
self.assertEqual(b'42', client_d.recv())
client_d.send(b'42')
self.assertEqual(b'42', client_c.recv())
finally:
server.shutdown()
def test_two_threads(self):
port = 9991
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(("localhost", port))
server = wsbridge.SimpleService(sock, threads=2, mediations_per_thread=1)
server.start(wait_iterations=True)
try:
client_a = _TestConnection("localhost", port)
client_a.start()
client_b = _TestConnection("localhost", port)
client_b.start()
client_a.wait_connected()
client_b.wait_connected()
client_a.send(b'42')
self.assertEqual(b'42', client_b.recv())
client_b.send(b'42')
self.assertEqual(b'42', client_a.recv())
client_c = _TestConnection("localhost", port)
client_c.start()
client_d = _TestConnection("localhost", port)
client_d.start()
client_c.wait_connected()
client_d.wait_connected()
client_e = _TestConnection("localhost", port)
client_e.start()
client_f = _TestConnection("localhost", port)
client_f.start()
with self.assertRaises(ConnectionResetError):
client_e.wait_connected()
client_f.wait_connected()
client_a.send(b'42')
self.assertEqual(b'42', client_b.recv())
client_b.send(b'42')
self.assertEqual(b'42', client_a.recv())
client_a.close()
client_b.recv()
client_e = _TestConnection("localhost", port)
client_e.start()
client_f = _TestConnection("localhost", port)
client_f.start()
client_e.wait_connected()
client_f.wait_connected()
client_e.send(b'42')
self.assertEqual(b'42', client_f.recv())
client_f.send(b'42')
self.assertEqual(b'42', client_e.recv())
finally:
server.shutdown()
def test_no_limit_per_t(self):
for t in (4, 1):
port = 9090 + t
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(("localhost", port))
server = wsbridge.SimpleService(sock, threads=t, mediations_per_thread=0)
server.start(wait_iterations=True)
clients = list()
for i in range(20):
client_a = _TestConnection("localhost", port)
client_a.start()
client_b = _TestConnection("localhost", port)
client_b.start()
clients.append(client_a)
clients.append(client_b)
client_a.wait_connected()
client_b.wait_connected()
for i in range(20):
clients[i * 2].close()
server.shutdown()
def test_no_t_limit(self):
port = 9099
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.bind(("localhost", port))
server = wsbridge.SimpleService(sock, threads=0)
server.start(wait_iterations=True)
clients = list()
for i in range(4):
client_a = _TestConnection("localhost", port)
client_a.start()
client_b = _TestConnection("localhost", port)
client_b.start()
clients.append(client_a)
clients.append(client_b)
client_a.wait_connected()
client_b.wait_connected()
mt = 0
for t in threading.enumerate():
log_ok(t)
if isinstance(t, simpleservice.MediatorsThread):
mt += 1
for i in range(4):
clients[i * 2].close()
server.shutdown()