Skip to content

Commit 33948bb

Browse files
committed
TESTS fix flaky tests on windows
1 parent 14397ad commit 33948bb

File tree

2 files changed

+109
-67
lines changed

2 files changed

+109
-67
lines changed

tests/test_graphqlws_subscription.py

Lines changed: 108 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
'{{"type":"next","id":"{query_id}","payload":{{"data":{{"number":{number}}}}}}}'
1919
)
2020

21-
COUNTING_DELAY = 2 * MS
22-
PING_SENDING_DELAY = 5 * MS
23-
PONG_TIMEOUT = 2 * MS
21+
COUNTING_DELAY = 20 * MS
22+
PING_SENDING_DELAY = 50 * MS
23+
PONG_TIMEOUT = 100 * MS
2424

2525
# List which can used to store received messages by the server
2626
logged_messages: List[str] = []
@@ -48,100 +48,140 @@ async def server_countdown_template(ws, path):
4848

4949
count_found = search("count: {:d}", query)
5050
count = count_found[0]
51-
print(f"Countdown started from: {count}")
51+
print(f" Server: Countdown started from: {count}")
5252

5353
pong_received: asyncio.Event = asyncio.Event()
5454

5555
async def counting_coro():
56-
for number in range(count, -1, -1):
57-
await ws.send(
58-
countdown_server_answer.format(query_id=query_id, number=number)
59-
)
60-
await asyncio.sleep(COUNTING_DELAY)
56+
print(" Server: counting task started")
57+
try:
58+
for number in range(count, -1, -1):
59+
await ws.send(
60+
countdown_server_answer.format(
61+
query_id=query_id, number=number
62+
)
63+
)
64+
await asyncio.sleep(COUNTING_DELAY)
65+
finally:
66+
print(" Server: counting task ended")
6167

68+
print(" Server: starting counting task")
6269
counting_task = asyncio.ensure_future(counting_coro())
6370

6471
async def keepalive_coro():
65-
while True:
66-
await asyncio.sleep(PING_SENDING_DELAY)
67-
try:
68-
# Send a ping
69-
await WebSocketServerHelper.send_ping(
70-
ws, payload="dummy_ping_payload"
71-
)
72-
73-
# Wait for a pong
72+
print(" Server: keepalive task started")
73+
try:
74+
while True:
75+
await asyncio.sleep(PING_SENDING_DELAY)
7476
try:
75-
await asyncio.wait_for(pong_received.wait(), PONG_TIMEOUT)
76-
except asyncio.TimeoutError:
77-
print("\nNo pong received in time!\n")
77+
# Send a ping
78+
await WebSocketServerHelper.send_ping(
79+
ws, payload="dummy_ping_payload"
80+
)
81+
82+
# Wait for a pong
83+
try:
84+
await asyncio.wait_for(
85+
pong_received.wait(), PONG_TIMEOUT
86+
)
87+
except asyncio.TimeoutError:
88+
print(
89+
"\n Server: No pong received in time!\n"
90+
)
91+
break
92+
93+
pong_received.clear()
94+
95+
except websockets.exceptions.ConnectionClosed:
7896
break
79-
80-
pong_received.clear()
81-
82-
except websockets.exceptions.ConnectionClosed:
83-
break
97+
finally:
98+
print(" Server: keepalive task ended")
8499

85100
if keepalive:
101+
print(" Server: starting keepalive task")
86102
keepalive_task = asyncio.ensure_future(keepalive_coro())
87103

88104
async def receiving_coro():
89-
nonlocal counting_task
90-
while True:
91-
92-
try:
93-
result = await ws.recv()
94-
logged_messages.append(result)
95-
except websockets.exceptions.ConnectionClosed:
96-
break
97-
98-
json_result = json.loads(result)
99-
100-
answer_type = json_result["type"]
101-
102-
if answer_type == "complete" and json_result["id"] == str(query_id):
103-
print("Cancelling counting task now")
104-
counting_task.cancel()
105-
if keepalive:
106-
print("Cancelling keep alive task now")
107-
keepalive_task.cancel()
108-
109-
elif answer_type == "ping":
110-
if answer_pings:
111-
payload = json_result.get("payload", None)
112-
await WebSocketServerHelper.send_pong(ws, payload=payload)
105+
print(" Server: receiving task started")
106+
try:
107+
nonlocal counting_task
108+
while True:
113109

114-
elif answer_type == "pong":
115-
pong_received.set()
110+
try:
111+
result = await ws.recv()
112+
logged_messages.append(result)
113+
except websockets.exceptions.ConnectionClosed:
114+
break
116115

116+
json_result = json.loads(result)
117+
118+
answer_type = json_result["type"]
119+
120+
if answer_type == "complete" and json_result["id"] == str(
121+
query_id
122+
):
123+
print("Cancelling counting task now")
124+
counting_task.cancel()
125+
if keepalive:
126+
print("Cancelling keep alive task now")
127+
keepalive_task.cancel()
128+
129+
elif answer_type == "ping":
130+
if answer_pings:
131+
payload = json_result.get("payload", None)
132+
await WebSocketServerHelper.send_pong(
133+
ws, payload=payload
134+
)
135+
136+
elif answer_type == "pong":
137+
pong_received.set()
138+
finally:
139+
print(" Server: receiving task ended")
140+
if keepalive:
141+
keepalive_task.cancel()
142+
143+
print(" Server: starting receiving task")
117144
receiving_task = asyncio.ensure_future(receiving_coro())
118145

119146
try:
147+
print(" Server: waiting for counting task to complete")
120148
await counting_task
121149
except asyncio.CancelledError:
122-
print("Now counting task is cancelled")
150+
print(" Server: Now counting task is cancelled")
123151

124-
receiving_task.cancel()
125-
126-
try:
127-
await receiving_task
128-
except asyncio.CancelledError:
129-
print("Now receiving task is cancelled")
152+
print(" Server: sending complete message")
153+
await WebSocketServerHelper.send_complete(ws, query_id)
130154

131155
if keepalive:
156+
print(" Server: cancelling keepalive task")
132157
keepalive_task.cancel()
133158
try:
134159
await keepalive_task
135160
except asyncio.CancelledError:
136-
print("Now keepalive task is cancelled")
161+
print(" Server: Now keepalive task is cancelled")
162+
163+
print(" Server: waiting for client to close the connection")
164+
try:
165+
await asyncio.wait_for(receiving_task, 1000 * MS)
166+
except asyncio.TimeoutError:
167+
pass
168+
169+
print(" Server: cancelling receiving task")
170+
receiving_task.cancel()
171+
172+
try:
173+
await receiving_task
174+
except asyncio.CancelledError:
175+
print(" Server: Now receiving task is cancelled")
137176

138-
await WebSocketServerHelper.send_complete(ws, query_id)
139177
except websockets.exceptions.ConnectionClosedOK:
140178
pass
141179
except AssertionError as e:
142-
print(f"\nAssertion failed: {e!s}\n")
180+
print(f"\n Server: Assertion failed: {e!s}\n")
143181
finally:
182+
print(" Server: waiting for websocket connection to close")
144183
await ws.wait_closed()
184+
print(" Server: connection closed")
145185

146186
return server_countdown_template
147187

@@ -406,6 +446,7 @@ async def test_graphqlws_subscription_with_keepalive(
406446
count -= 1
407447

408448
assert count == -1
449+
assert "ping" in session.transport.payloads
409450
assert session.transport.payloads["ping"] == "dummy_ping_payload"
410451
assert (
411452
session.transport.payloads["connection_ack"] == "dummy_connection_ack_payload"
@@ -570,18 +611,19 @@ async def test_graphqlws_subscription_manual_pings_with_payload(
570611
number = result["number"]
571612
print(f"Number received: {number}")
572613

573-
assert number == count
574-
count -= 1
575-
576614
payload = {"count_received": count}
577615

578616
await transport.send_ping(payload=payload)
579617

580-
await transport.pong_received.wait()
618+
await asyncio.wait_for(transport.pong_received.wait(), 10000 * MS)
619+
581620
transport.pong_received.clear()
582621

583622
assert transport.payloads["pong"] == payload
584623

624+
assert number == count
625+
count -= 1
626+
585627
assert count == -1
586628

587629

tests/test_websocket_subscription.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ async def test_websocket_subscription_with_keepalive_with_timeout_ok(
391391

392392
path = "/graphql"
393393
url = f"ws://{server.hostname}:{server.port}{path}"
394-
sample_transport = WebsocketsTransport(url=url, keep_alive_timeout=(10 * MS))
394+
sample_transport = WebsocketsTransport(url=url, keep_alive_timeout=(20 * MS))
395395

396396
client = Client(transport=sample_transport)
397397

0 commit comments

Comments
 (0)