-
Notifications
You must be signed in to change notification settings - Fork 86
/
conftest.py
278 lines (232 loc) · 9.4 KB
/
conftest.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
# 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.
"""Auxiliary fixtures to simplify testing."""
import asyncio
import inspect
import sys
import threading
import channels
import django
import graphene
import pytest
import channels_graphql_ws
import channels_graphql_ws.testing
@pytest.fixture
def event_loop(request):
"""Overwrite `pytest_asyncio` eventloop to fix Windows issue.
Default implementation causes `NotImplementedError` on Windows with
Python 3.8, because they changed default eventloop in 3.8.
NOTE: We do the same thing in the `example/settings.py` because it
imports (and fails) before we have a chance to invoke this fixture.
So, we could avoid adding this fixture, but I feel it is better to
keep the proper solution here as well.
"""
del request
if sys.platform == "win32" and sys.version_info.minor >= 8:
asyncio.set_event_loop_policy(
asyncio.WindowsSelectorEventLoopPolicy() # pylint: disable=no-member
)
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
class DummyQuery(graphene.ObjectType):
"""Dummy query to use if no query supplied to the `gql` call.
Graphene will throw a exception from the `graphene.Schema` constructor
call if the `query` parameter is `None`. The `query` with no fields
will also result in an exception.
Our `gql` function should allow `None` value for the `query`
parameter to make tests code simplier. So this "DummyQuery" will be
used as a substitution for the `query` parameter of the
`graphene.Schema` constructor call.
"""
value = graphene.String()
def resolve_value(self):
"""Resolver to return predefined value which can be tested."""
del self
return "test"
@pytest.fixture
def gql(db, request):
"""PyTest fixture for testing GraphQL WebSocket backends.
The fixture provides a method to setup GraphQL testing backend for
the given GraphQL schema (query, mutation, and subscription). In
particular: it sets up an instance of `GraphqlWsConsumer` and an
instance of `GraphqlWsClient`. The former one is returned
from the function.
Syntax:
gql(
*,
query=None,
mutation=None,
subscription=None,
consumer_attrs=None,
communicator_kwds=None,
subprotocol="graphql-transport-ws",
):
Args:
query: Root GraphQL query. Optional.
mutation: Root GraphQL subscription. Optional.
subscription: Root GraphQL mutation. Optional.
consumer_attrs: `GraphqlWsConsumer` attributes dict. Optional.
communicator_kwds: Extra keyword arguments for the Channels
`channels.testing.WebsocketCommunicator`. Optional.
subprotocol: WebSocket subprotocol to use by the Client. Can
have a value of "graphql-transport-ws" or "graphql-ws".
By default set to "graphql-transport-ws".
Returns:
An instance of the `GraphqlWsClient` class which has many
useful GraphQL-related methods, see the `GraphqlWsClient`
class docstrings for details.
Use like this:
```
def test_something(gql):
client = gql(
# GraphQl schema.
query=MyQuery,
mutation=MyMutation,
subscription=MySubscription,
# `GraphqlWsConsumer` settings.
consumer_attrs={"strict_ordering": True},
# `channels.testing.WebsocketCommunicator` settings.
communicator_kwds={"headers": [...]},
# Subprotocol to test.
subprotocol="graphql-ws"
)
...
```
"""
# NOTE: We need Django DB to be initialized each time we work with
# `GraphqlWsConsumer`, because it uses threads and sometimes calls
# `django.db.close_old_connections()`.
del db
issued_clients = []
def client_constructor(
*,
query=None,
mutation=None,
subscription=None,
consumer_attrs=None,
communicator_kwds=None,
subprotocol="graphql-transport-ws",
):
"""Setup GraphQL consumer and the communicator for tests."""
# Graphene will throw a exception from the `graphene.Schema`
# constructor call if the `query` parameter is `None`. The
# `query` with no fields will also result in exception.
#
# If no `query` provided to the `client_constructor`, then use
# DummyQuery, since Graphene requires one.
if query is None:
query = DummyQuery
class ChannelsConsumer(channels_graphql_ws.GraphqlWsConsumer):
"""Channels WebSocket consumer for GraphQL API."""
# Redefine the group prefix to be able to detect that it is
# really changed.
group_name_prefix = "TEST"
schema = graphene.Schema(
query=query,
mutation=mutation,
subscription=subscription,
auto_camelcase=False,
)
# Set additional attributes to the `ChannelsConsumer`.
if consumer_attrs is not None:
for attr, val in consumer_attrs.items():
setattr(ChannelsConsumer, attr, val)
application = channels.routing.ProtocolTypeRouter(
{
"websocket": channels.routing.URLRouter(
[django.urls.path("graphql/", ChannelsConsumer.as_asgi())]
)
}
)
transport = channels_graphql_ws.testing.GraphqlWsTransport(
application=application,
path="graphql/",
communicator_kwds=communicator_kwds,
subprotocol=subprotocol,
)
client = channels_graphql_ws.testing.GraphqlWsClient(
transport, subprotocol=subprotocol
)
issued_clients.append(client)
return client
yield client_constructor
# Assert all issued client are properly finalized.
for client in reversed(issued_clients):
assert (
not client.connected
), f"Test has left connected client: {request.node.nodeid}!"
@pytest.fixture(scope="session", autouse=True)
def synchronize_inmemory_channel_layer():
"""Monkeypatch `InMemoryChannelLayer` to make it thread safe.
The `InMemoryChannelLayer` is not thread safe by default since it use the
`asyncio.Queue`.
Without this we have a blinking fails in the unit tests run:
Traceback (most recent call last):
File ".../site-packages/promise/promise.py", line 842, in handle_future_result
resolve(future.result())
File ".../tests/test_concurrent.py", line 994, in mutate
await OnChatMessageSentAsync.notify(message=message, user_id=user_id)
File ".../tests/test_concurrent.py", line 940, in notify
await super().broadcast(group=group, payload=message)
File ".../channels_graphql_ws/graphql_ws.py", line 248, in broadcast_async
"payload": serialized_payload,
File ".../site-packages/channels/layers.py", line 351, in group_send
for channel in self.groups.get(group, set()):
graphql.error.GraphQLError:
dictionary changed size during iteration
"""
guard = threading.RLock()
def wrap(func):
if inspect.iscoroutinefunction(func):
async def wrapper(*args, **kwds):
with guard:
return await func(*args, **kwds)
else:
def wrapper(*args, **kwds):
with guard:
return func(*args, **kwds)
return wrapper
# Carefully selected methods to protect. We cannot simply wrap all
# the callables, cause this will lead to deadlocks, e.g. when we
# locked the mutex in `await receive` and then another coroutine
# calls `await send`.
callables_to_protect = [
"_clean_expired",
"_remove_from_groups",
"close",
"flush",
"group_add",
"group_discard",
"group_send",
"new_channel",
"send",
]
for attr_name in callables_to_protect:
setattr(
channels.layers.InMemoryChannelLayer,
attr_name,
wrap(getattr(channels.layers.InMemoryChannelLayer, attr_name)),
)
@pytest.fixture(scope="function", autouse=True)
def extra_print_in_the_beginning():
"""Improve output of `pytest -s` by adding EOL in the beginning."""
print()