FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
openprotocol-python/tests/application/test_client.py at main · Industware-cloud/openprotocol-python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Industware-cloud
/
openprotocol-python
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
1
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
openprotocol-python
/
tests
/
application
/
test_client.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
420 lines (313 loc) · 11.4 KB
Breadcrumbs
openprotocol-python
/
tests
/
application
/
test_client.py
Copy path
File metadata and controls
420 lines (313 loc) · 11.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import
asyncio
import
pytest
from
unittest
.
mock
import
AsyncMock
from
openprotocol
.
application
.
base_messages
import
(
OpenProtocolEventSubscribe
,
CommunicationPositiveAck
,
CommunicationNegativeAck
,
OpenProtocolEventUnsubscribe
,
)
from
openprotocol
.
application
.
communication
import
CommunicationStartAcknowledge
from
openprotocol
.
application
.
client
import
OpenProtocolClient
from
openprotocol
.
core
.
message
import
OpenProtocolRawMessage
from
openprotocol
.
core
.
mid_base
import
(
OpenProtocolMessage
,
MidCodec
,
MessageType
,
register_messages
,
)
class
DummyMessageRecv
(
OpenProtocolMessage
):
MID
=
9999
REVISION
=
1
MESSAGE_TYPE
=
MessageType
.
REQ_REPLY_MESSAGE
def
__init__
(
self
,
payload
=
"hello"
):
super
().
__init__
(
self
.
REVISION
)
self
.
payload
=
payload
@
classmethod
def
from_message
(
cls
,
msg
:
OpenProtocolRawMessage
):
return
cls
(
msg
.
payload
)
def
encode
(
self
)
->
OpenProtocolRawMessage
:
return
self
.
create_message
(
self
.
REVISION
,
self
.
payload
)
class
DummyMessageNoResp
(
OpenProtocolMessage
):
MID
=
9997
REVISION
=
1
MESSAGE_TYPE
=
MessageType
.
REQ_MESSAGE
expected_response_mids
=
set
([])
def
__init__
(
self
,
payload
=
"hello"
):
super
().
__init__
(
self
.
REVISION
)
self
.
payload
=
payload
def
encode
(
self
)
->
OpenProtocolRawMessage
:
return
self
.
create_message
(
self
.
REVISION
,
self
.
payload
)
@
classmethod
def
from_message
(
cls
,
msg
:
OpenProtocolRawMessage
):
return
cls
(
msg
.
payload
)
class
DummyMessageSend
(
OpenProtocolMessage
):
MID
=
9998
REVISION
=
1
MESSAGE_TYPE
=
MessageType
.
REQ_MESSAGE
expected_response_mids
=
{
DummyMessageRecv
.
MID
}
def
__init__
(
self
,
payload
=
"hello"
):
super
().
__init__
(
self
.
REVISION
)
self
.
payload
=
payload
@
classmethod
def
from_message
(
cls
,
msg
:
OpenProtocolRawMessage
):
return
cls
(
msg
.
payload
)
def
encode
(
self
)
->
OpenProtocolRawMessage
:
return
self
.
create_message
(
self
.
REVISION
,
self
.
payload
)
class
DummyMessageSendRes
(
OpenProtocolMessage
):
MID
=
9998
REVISION
=
1
MESSAGE_TYPE
=
MessageType
.
REQ_MESSAGE
expected_response_mids
=
{
1234
}
def
__init__
(
self
,
payload
=
"hello"
):
super
().
__init__
(
self
.
REVISION
)
self
.
payload
=
payload
@
classmethod
def
from_message
(
cls
,
msg
:
OpenProtocolRawMessage
):
return
cls
(
msg
.
payload
)
def
encode
(
self
)
->
OpenProtocolRawMessage
:
return
self
.
create_message
(
self
.
REVISION
,
self
.
payload
)
register_messages
(
DummyMessageRecv
,
DummyMessageSendRes
)
@
pytest
.
mark
.
asyncio
async
def
test_connect_starts_client
(
monkeypatch
):
# Arrange
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
# Replace send_receive with mock (we’re not testing message exchange here)
# Replace listener loop so it doesn’t block forever
async
def
dummy_listener_loop
():
await
asyncio
.
sleep
(
0
)
monkeypatch
.
setattr
(
client
,
"_listener_loop"
,
dummy_listener_loop
)
async
def
fake_send
(
_
):
# Simulate that listener loop sets result a bit later
await
asyncio
.
sleep
(
0.01
)
if
client
.
_pending_future
:
client
.
_pending_future
.
set_result
(
CommunicationStartAcknowledge
(
1
,
1
,
1
,
"test"
,
"test1"
)
)
mock_transport
.
send
.
side_effect
=
fake_send
# Act
await
client
.
connect
()
# Assert
mock_transport
.
connect
.
assert_awaited_once
()
assert
client
.
_running
assert
client
.
_startup_done
assert
isinstance
(
client
.
_listener_task
,
asyncio
.
Task
)
mock_transport
.
send
.
assert_called_once
()
# Cleanup
client
.
_listener_task
.
cancel
()
try
:
await
client
.
_listener_task
except
asyncio
.
CancelledError
:
pass
@
pytest
.
mark
.
asyncio
async
def
test_send_receive_sets_future_result
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_running
=
True
client
.
_startup_done
=
True
async
def
fake_send
(
_
):
# Simulate that listener loop sets result a bit later
await
asyncio
.
sleep
(
0.01
)
if
client
.
_pending_future
:
client
.
_pending_future
.
set_result
(
DummyMessageRecv
(
"world"
))
mock_transport
.
send
.
side_effect
=
fake_send
result
=
await
client
.
send_receive
(
DummyMessageSend
())
assert
isinstance
(
result
,
DummyMessageRecv
)
assert
result
.
payload
==
"world"
mock_transport
.
send
.
assert_called_once
()
@
pytest
.
mark
.
asyncio
async
def
test_send_receive_no_response
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_running
=
True
client
.
_startup_done
=
True
with
pytest
.
raises
(
ValueError
):
await
client
.
send_receive
(
DummyMessageNoResp
())
mock_transport
.
send
.
assert_not_called
()
@
pytest
.
mark
.
asyncio
async
def
test_listener_loop_dispatches_reply
():
mock_transport
=
AsyncMock
()
mock_transport
.
receive
=
AsyncMock
(
side_effect
=
[
MidCodec
.
encode
(
DummyMessageRecv
(
"OK"
)),
asyncio
.
CancelledError
(),
# to break the loop
]
)
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_running
=
True
client
.
_pending_future
=
asyncio
.
get_running_loop
().
create_future
()
client
.
_pending_expected
=
{
DummyMessageRecv
.
MID
}
task
=
asyncio
.
create_task
(
client
.
_listener_loop
())
await
asyncio
.
sleep
(
0.1
)
# The listener should set the result
assert
client
.
_pending_future
.
done
()
assert
isinstance
(
client
.
_pending_future
.
result
(),
DummyMessageRecv
)
task
.
cancel
()
@
pytest
.
mark
.
asyncio
async
def
test_send_receive_not_expected_mid
():
mock_transport
=
AsyncMock
()
mock_transport
.
receive
=
AsyncMock
(
side_effect
=
[
MidCodec
.
encode
(
DummyMessageSendRes
(
"OK"
)),
asyncio
.
CancelledError
(),
# to break the loop
]
)
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_running
=
True
client
.
_pending_future
=
asyncio
.
get_running_loop
().
create_future
()
client
.
_pending_expected
=
set
(
DummyMessageSendRes
.
expected_response_mids
)
task
=
asyncio
.
create_task
(
client
.
_listener_loop
())
await
asyncio
.
sleep
(
0.1
)
# The listener should set the result
assert
client
.
_pending_future
.
done
()
with
pytest
.
raises
(
ValueError
):
await
client
.
_pending_future
.
result
()
task
.
cancel
()
await
asyncio
.
sleep
(
0.1
)
assert
task
.
done
()
class
DummySubscribeMidNoEvent
(
OpenProtocolEventSubscribe
):
MID
=
42
REVISION
=
1
def
encode
(
self
):
return
b"dummy"
@
classmethod
def
from_message
(
cls
,
msg
):
return
cls
(
msg
.
revision
)
class
DummySubscribeMid
(
OpenProtocolEventSubscribe
):
MID
=
42
REVISION
=
1
MID_EVENT
=
10
def
encode
(
self
):
return
b"dummy"
@
classmethod
def
from_message
(
cls
,
msg
):
return
cls
()
class
DummyUnsubscribeMidNoEvent
(
OpenProtocolEventUnsubscribe
):
MID
=
44
REVISION
=
1
def
encode
(
self
):
return
b"dummy"
@
classmethod
def
from_message
(
cls
,
msg
):
return
cls
()
class
DummyUnsubscribeMid
(
OpenProtocolEventUnsubscribe
):
MID
=
44
REVISION
=
1
MID_EVENT
=
10
def
encode
(
self
):
return
b"dummy"
@
classmethod
def
from_message
(
cls
,
msg
):
return
cls
()
@
pytest
.
mark
.
asyncio
async
def
test_subscribe_incorrect_mid
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
with
pytest
.
raises
(
RuntimeError
):
await
client
.
subscribe
(
DummySubscribeMidNoEvent
)
@
pytest
.
mark
.
asyncio
async
def
test_unsubscribe_incorrect_mid
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
with
pytest
.
raises
(
RuntimeError
):
await
client
.
unsubscribe
(
DummyUnsubscribeMidNoEvent
)
@
pytest
.
mark
.
asyncio
async
def
test_subscribe_waits_for_ack_and_registers
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_startup_done
=
True
client
.
_running
=
True
# Mock send_receive to return ACK
client
.
send_receive
=
AsyncMock
(
return_value
=
CommunicationPositiveAck
(
DummySubscribeMid
.
REVISION
,
DummySubscribeMid
.
MID
)
)
await
client
.
subscribe
(
DummySubscribeMid
)
# Now it should be registered
assert
DummySubscribeMid
.
MID_EVENT
in
client
.
_subscribed_mids
client
.
send_receive
.
assert_awaited_once
()
@
pytest
.
mark
.
asyncio
async
def
test_subscribe_raises_on_nack
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_startup_done
=
True
client
.
_running
=
True
client
.
send_receive
=
AsyncMock
(
return_value
=
CommunicationNegativeAck
(
DummySubscribeMid
.
REVISION
,
DummySubscribeMid
.
MID
,
1
)
)
with
pytest
.
raises
(
RuntimeError
):
await
client
.
subscribe
(
DummySubscribeMid
)
# Should NOT register
assert
DummySubscribeMid
.
MID
not
in
client
.
_subscribed_mids
@
pytest
.
mark
.
asyncio
async
def
test_get_subscription_waits_then_receives
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_startup_done
=
True
client
.
_running
=
True
msg
=
DummyMessageSend
()
async
def
delayed_put
():
await
asyncio
.
sleep
(
0.1
)
await
client
.
_subscription_queue
.
put
(
msg
)
asyncio
.
create_task
(
delayed_put
())
result
=
await
client
.
get_subscription
()
assert
result
is
msg
@
pytest
.
mark
.
asyncio
async
def
test_unsubscribe_success_registers_removed
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
# prepare client as if already connected/subscribed
client
.
_running
=
True
client
.
_startup_done
=
True
client
.
_subscribed_mids
.
add
(
DummyUnsubscribeMid
.
MID
)
# Mock send_receive to return ACK
client
.
send_receive
=
AsyncMock
(
return_value
=
CommunicationPositiveAck
(
DummyUnsubscribeMid
.
REVISION
,
DummyUnsubscribeMid
.
MID
)
)
# Act
await
client
.
unsubscribe
(
DummyUnsubscribeMid
)
# Assert: unsubscribed removed and send_receive called
assert
DummyUnsubscribeMid
.
MID_EVENT
not
in
client
.
_subscribed_mids
client
.
send_receive
.
assert_awaited_once
()
@
pytest
.
mark
.
asyncio
async
def
test_unsubscribe_rejected_raises_and_keeps_subscription
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_running
=
True
client
.
_startup_done
=
True
client
.
_subscribed_mids
.
add
(
DummyUnsubscribeMid
.
MID_EVENT
)
# Mock send_receive to return NACK
client
.
send_receive
=
AsyncMock
(
return_value
=
CommunicationNegativeAck
(
DummyUnsubscribeMid
.
REVISION
,
DummyUnsubscribeMid
.
MID
,
1
)
)
# Act / Assert
with
pytest
.
raises
(
RuntimeError
):
await
client
.
unsubscribe
(
DummyUnsubscribeMid
)
# Ensure subscription still present
assert
DummyUnsubscribeMid
.
MID_EVENT
in
client
.
_subscribed_mids
client
.
send_receive
.
assert_awaited_once
()
@
pytest
.
mark
.
asyncio
async
def
test_unsubscribe_rejected_raises_and_keeps_subscription_force
():
mock_transport
=
AsyncMock
()
client
=
OpenProtocolClient
(
mock_transport
)
client
.
_running
=
True
client
.
_startup_done
=
True
client
.
_subscribed_mids
.
add
(
DummyUnsubscribeMid
.
MID_EVENT
)
# Mock send_receive to return NACK
client
.
send_receive
=
AsyncMock
(
return_value
=
CommunicationNegativeAck
(
DummyUnsubscribeMid
.
REVISION
,
DummyUnsubscribeMid
.
MID
,
1
)
)
await
client
.
unsubscribe
(
DummyUnsubscribeMid
,
force
=
True
)
# Ensure subscription still present
assert
DummyUnsubscribeMid
.
MID_EVENT
not
in
client
.
_subscribed_mids
client
.
send_receive
.
assert_awaited_once
()
Back
|
FazBrowse Home
|
New Git URL