-
Notifications
You must be signed in to change notification settings - Fork 86
/
test_async.py
266 lines (207 loc) · 8.88 KB
/
test_async.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
# Copyright (C) DATADVANCE, 2010-2023
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""Check different asynchronous workflows."""
# NOTE: The GraphQL schema is defined at the end of the file.
import time
import uuid
from datetime import datetime
import graphene
import pytest
import channels_graphql_ws
@pytest.mark.asyncio
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
async def test_broadcast(gql, subprotocol):
"""Test that the asynchronous 'broadcast()' call works correctly.
Because we cannot use sync 'broadcasts()' method in the thread
which has running event loop.
Test simply checks that there is no problem in sending
notification messages via the `OnMessageSent` subscription
in the asynchronous `mutate()` method of the `SendMessage`
mutation.
"""
print("Establish & initialize WebSocket GraphQL connection.")
# Test subscription notifications order, even with disabled ordering
# notifications must be send in the order they were broadcasted.
settings = {"strict_ordering": False}
client_sender = gql(
mutation=Mutation,
subscription=Subscription,
consumer_attrs=settings,
subprotocol=subprotocol,
)
client_recipient = gql(
mutation=Mutation,
subscription=Subscription,
consumer_attrs=settings,
subprotocol=subprotocol,
)
await client_sender.connect_and_init()
await client_recipient.connect_and_init()
print("Subscribe to GraphQL subscription.")
sub_id = await client_recipient.start(
query="""
subscription on_message_sent {
on_message_sent { message }
}
""",
operation_name="on_message_sent",
)
await client_recipient.assert_no_messages()
print("Trigger the subscription by mutation to receive notification.")
message = f"Hi! {str(uuid.uuid4().hex)}"
msg_id = await client_sender.start(
query="""
mutation send_message($message: String!) {
send_message(message: $message) {
success
}
}
""",
variables={"message": message},
operation_name="send_message",
)
# Mutation response.
resp = await client_sender.receive_next(msg_id)
assert resp["data"] == {"send_message": {"success": True}}
await client_sender.receive_complete(msg_id)
# Subscription notification.
resp = await client_recipient.receive_next(sub_id)
data = resp["data"]["on_message_sent"]
assert data["message"] == message, "Subscription notification contains wrong data!"
print("Trigger sequence of timestamps with delayed publish.")
count = 10
msg_id = await client_sender.start(
query="""
mutation send_timestamps($count: Int!) {
send_timestamps(count: $count) {
success
}
}
""",
variables={"count": count},
operation_name="send_timestamps",
)
# Mutation response.
resp = await client_sender.receive_next(msg_id)
assert resp["data"] == {"send_timestamps": {"success": True}}
await client_sender.receive_complete(msg_id)
timestamps = []
for _ in range(count):
resp = await client_recipient.receive_next(sub_id)
data = resp["data"]["on_message_sent"]
timestamps.append(data["message"])
assert timestamps == sorted(
timestamps
), "Server does not preserve messages order for subscription!"
print("Disconnect and wait the application to finish gracefully.")
await client_sender.assert_no_messages(
"Unexpected message received at the end of the test!"
)
await client_sender.finalize()
await client_recipient.assert_no_messages(
"Unexpected message received at the end of the test!"
)
await client_recipient.finalize()
@pytest.mark.parametrize("subprotocol", ["graphql-transport-ws", "graphql-ws"])
@pytest.mark.asyncio
async def test_subscribe_unsubscribe(gql, subprotocol):
"""Test that the asynchronous `unsubscribe()` works.
Test the server able to handle client unsubscribe before connection
were initialized properly.
In real life this case is rare if not impossible. But in our async
tests it is common since server and client are launched by using
same eventloop.
"""
print("Establish & initialize WebSocket GraphQL connection.")
# Test subscription notifications order, even with disabled ordering
# notifications must be send in the order they were broadcasted.
client = gql(mutation=Mutation, subscription=Subscription, subprotocol=subprotocol)
await client.connect_and_init()
print("Subscribe to GraphQL subscription.")
sub_id = await client.start(
query="""
subscription on_message_sent {
on_message_sent { message }
}
""",
operation_name="on_message_sent",
)
await client.complete(sub_id)
# If server was not able to handle unsubscribe command, then test
# will hang here.
await client.receive(assert_type="complete")
await client.finalize()
# ---------------------------------------------------------------- GRAPHQL BACKEND SETUP
class SendMessage(graphene.Mutation, name="SendMessagePayload"): # type: ignore
"""Test mutation to send message to `OnMessageSent` subscription."""
class Arguments:
"""That is how mutation arguments are defined."""
message = graphene.String(description="Some text notification.", required=True)
success = graphene.Boolean()
@staticmethod
async def mutate(root, info, message):
"""Send notification and return `success` status."""
del root, info
await OnMessageSent.broadcast(payload={"message": message})
return SendMessage(success=True)
class SendTimestamps(graphene.Mutation, name="SendTimestampsPayload"): # type: ignore
"""Send monotonic timestamps by `OnMessageSent` subscription.
Broadcast messages contains timestamp and publish delay, while
timestamps are increasing delays otherwise are decreasing by 0.1s
from the first to the last timestamp. Delay is executed by the
publish callback on server, and if server does not preserve messages
order client will get timestamps in the wrong order.
"""
class Arguments:
"""That is how mutation arguments are defined."""
count = graphene.Int(description="Number of timestamps to send.", required=True)
success = graphene.Boolean()
@staticmethod
async def mutate(root, info, count):
"""Send increasing timestamps with decreasing delays."""
del root, info
for idx in range(count):
now = datetime.fromtimestamp(time.monotonic())
payload = {"message": now.isoformat(), "delay": (count - idx) / 10}
await OnMessageSent.broadcast(payload=payload)
return SendTimestamps(success=True)
class OnMessageSent(channels_graphql_ws.Subscription):
"""Test GraphQL simple subscription.
Subscribe to receive messages.
"""
message = graphene.String(description="Some text notification.", required=True)
@staticmethod
async def subscribe(payload, info):
"""This method is needed to assure `async` variant works OK."""
del payload, info
@staticmethod
async def publish(payload, info):
"""Publish query result to all subscribers may be with delay."""
del info
time.sleep(payload.get("delay") or 0)
return OnMessageSent(message=payload["message"])
class Mutation(graphene.ObjectType):
"""GraphQL mutations."""
send_message = SendMessage.Field()
send_timestamps = SendTimestamps.Field()
class Subscription(graphene.ObjectType):
"""GraphQL subscriptions."""
on_message_sent = OnMessageSent.Field()