-
Notifications
You must be signed in to change notification settings - Fork 22
/
test_comms.py
1390 lines (1077 loc) · 45.1 KB
/
test_comms.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import logging
import multiprocessing
import pprint
import threading
import time as ttime
import pytest
import zmq
from bluesky_queueserver.manager.comms import (
CommJsonRpcError,
CommTimeoutError,
JSONRPCResponseManager,
PipeJsonRpcReceive,
PipeJsonRpcSendAsync,
ZMQCommSendAsync,
ZMQCommSendThreads,
generate_zmq_keys,
generate_zmq_public_key,
validate_zmq_key,
)
from bluesky_queueserver.tests.common import format_jsonrpc_msg
def count_threads_with_name(name):
"""
Returns the number of currently existing threads with the given name
"""
n_count = 0
for th in threading.enumerate():
if th.name.startswith(name):
n_count += 1
return n_count
# =======================================================================
# Class CommJsonRpcError
def test_CommJsonRpcError_1():
"""
Basic test for `CommJsonRpcError` class.
"""
err_msg, err_code, err_type = "Some error occured", 25, "RuntimeError"
ex = CommJsonRpcError(err_msg, err_code, err_type)
assert ex.message == err_msg, "Message set incorrectly"
assert ex.error_code == err_code, "Error code set incorrectly"
assert ex.error_type == err_type, "Error type set incorrectly"
def test_CommJsonRpcError_2():
"""
Basic test for `CommJsonRpcError` class.
"""
err_msg, err_code, err_type = "Some error occured", 25, "RuntimeError"
ex = CommJsonRpcError(err_msg, err_code, err_type)
s = str(ex)
assert err_msg in s, "Error message is not found in printed error message"
assert str(err_code) in s, "Error code is not found in printed error message"
assert err_type in s, "Error type is not found in printed error message"
repr = f"CommJsonRpcError('{err_msg}', {err_code}, '{err_type}')"
assert ex.__repr__() == repr, "Error representation is printed incorrectly"
def test_CommJsonRpcError_3_fail():
"""
`CommJsonRpcError` class. Failing cases
"""
err_msg, err_code, err_type = "Some error occured", 25, "RuntimeError"
ex = CommJsonRpcError(err_msg, err_code, err_type)
# The pattern 'can't set attribute': Python 3.10 and older
# The pattern 'object has no setter': Python 3.11
with pytest.raises(AttributeError, match="can't set attribute|object has no setter"):
ex.message = err_msg
with pytest.raises(AttributeError, match="can't set attribute|object has no setter"):
ex.error_code = err_code
with pytest.raises(AttributeError, match="can't set attribute|object has no setter"):
ex.error_type = err_type
# =======================================================================
# Class PipeJsonRpcReceive
def test_PipeJsonRpcReceive_1():
"""
Create, start and stop `PipeJsonRpcReceive` object
"""
conn1, conn2 = multiprocessing.Pipe()
new_name = "Unusual Thread Name"
assert count_threads_with_name(new_name) == 0, "No threads are expected to exist"
pc = PipeJsonRpcReceive(conn=conn2, name=new_name)
pc.start()
assert count_threads_with_name(new_name) == 2, "Two threads are expected to exist"
pc.start() # Expected to do nothing
pc.stop()
ttime.sleep(0.15) # Wait until the thread stops (wait more than the 0.1s polling period)
assert count_threads_with_name(new_name) == 0, "No threads are expected to exist"
pc.start() # Restart
assert count_threads_with_name(new_name) == 2, "Two thread are expected to exist"
pc.stop()
@pytest.mark.parametrize(
"method, params, result, notification",
[
("method_handler1", [], 5, False),
("method_handler1", [], 5, True),
("method1", [], 5, False),
("method2", [5], 15, False),
("method2", {"value": 5}, 15, False),
("method2", {}, 12, False),
("method3", {"value": 5}, 20, False),
("method3", {}, 18, False),
("method4", {"value": 5}, 20, False),
("method4", {}, 19, False),
],
)
def test_PipeJsonRpcReceive_2(method, params, result, notification):
"""
The case of single requests.
"""
value_nonlocal = None
def method_handler1():
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return 5
def method_handler2(value=2):
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return value + 10
def method_handler3(*, value=3):
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return value + 15
class SomeClass:
def method_handler4(self, *, value=4):
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return value + 15
some_class = SomeClass()
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2)
pc.add_method(method_handler1) # No name is specified, default name is "method_handler1"
pc.add_method(method_handler1, "method1")
pc.add_method(method_handler2, "method2")
pc.add_method(method_handler3, "method3")
pc.add_method(some_class.method_handler4, "method4")
pc.start()
for n in range(3):
value_nonlocal = None
request = format_jsonrpc_msg(method, params, notification=notification)
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5): # Set timeout large enough
if not notification:
response = conn1.recv()
response = json.loads(response)
assert response["id"] == request["id"], "Response ID does not match message ID."
assert "result" in response, f"Key 'result' is not contained in response: {response}"
assert response["result"] == result, f"Result does not match the expected: {response}"
assert value_nonlocal == "function_was_called", "Non-local variable has incorrect value"
else:
assert False, "Notification was sent but response was received."
else:
# If request is a notification, there should be no response.
if not notification:
assert False, "Timeout occurred while waiting for response."
else:
assert value_nonlocal == "function_was_called", "Non-local variable has incorrect value"
pc.stop()
def test_PipeJsonRpcReceive_3():
"""
Test sending multiple requests
"""
def method_handler3(*, value=3):
return value + 15
class SomeClass:
def method_handler4(self, *, value=4):
return value + 7
some_class = SomeClass()
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2)
pc.add_method(method_handler3, "method3")
pc.add_method(some_class.method_handler4, "method4")
pc.start()
# Non-existing method ('Method not found' error)
request = [
format_jsonrpc_msg("method3", {"value": 7}),
format_jsonrpc_msg("method4", {"value": 3}, notification=True),
format_jsonrpc_msg("method4", {"value": 9}),
]
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5): # Set timeout large enough
response = conn1.recv()
response = json.loads(response)
assert len(response) == 2, "Unexpected number of response messages"
assert response[0]["id"] == request[0]["id"], "Response ID does not match message ID."
assert response[1]["id"] == request[2]["id"], "Response ID does not match message ID."
assert response[0]["result"] == 22, "Response ID does not match message ID."
assert response[1]["result"] == 16, "Response ID does not match message ID."
else:
assert False, "Timeout occurred while waiting for response."
pc.stop()
def test_PipeJsonRpcReceive_4():
"""
Test if all outdated unprocessed messages are deleted from the pipe
as the processing thread is started.
"""
def method_handler3(*, value=3):
return value + 15
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2)
pc.add_method(method_handler3, "method3")
# The thread is not started yet, but we still send a message through the pipe.
# This message is expected to be deleted once the thread starts.
request1a = [
format_jsonrpc_msg("method3", {"value": 5}),
]
request1b = [
format_jsonrpc_msg("method3", {"value": 6}),
]
conn1.send(json.dumps(request1a))
conn1.send(json.dumps(request1b))
# Start the processing thread. The messages that were already sent are expected to be ignored.
pc.start()
# Send another message. This message is expected to be processed.
request2 = [
format_jsonrpc_msg("method3", {"value": 7}),
]
conn1.send(json.dumps(request2))
if conn1.poll(timeout=0.5): # Set timeout large enough
response = conn1.recv()
response = json.loads(response)
assert len(response) == 1, "Unexpected number of response messages"
assert response[0]["id"] == request2[0]["id"], "Response ID does not match message ID."
assert response[0]["result"] == 22, "Response ID does not match message ID."
else:
assert False, "Timeout occurred while waiting for response."
pc.stop()
# fmt: off
@pytest.mark.parametrize("clear_buffer", [False, True])
# fmt: on
def test_PipeJsonRpcReceive_5(clear_buffer):
"""
Checking that the buffer overflow does not overflow the pipe.
"""
n_calls = 0
start_processing = False
def method_handler3():
nonlocal n_calls
while not start_processing:
ttime.sleep(0.05)
n_calls += 1
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2)
pc.add_method(method_handler3, "method3")
pc.start()
# Non-existing method ('Method not found' error)
request = format_jsonrpc_msg("method3")
n_buf = pc._msg_recv_buffer_size
conn1.send(json.dumps(request))
ttime.sleep(1)
for _ in range(n_buf * 2):
conn1.send(json.dumps(request))
ttime.sleep(1)
if clear_buffer:
ttime.sleep(1)
pc.clear_buffer()
start_processing = True
ttime.sleep(1)
assert n_calls == (1 if clear_buffer else (n_buf + 1))
pc.stop()
def test_PipeJsonRpcReceive_6_failing():
"""
Those tests are a result of exploration of how `json-rpc` error processing works.
"""
def method_handler3(*, value=3):
return value + 15
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2)
pc.add_method(method_handler3, "method3")
pc.start()
# ------- Incorrect argument (arg list instead of required kwargs) -------
# Returns 'Server Error' (-32000)
request = format_jsonrpc_msg("method3", [5])
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5): # Set timeout large enough
response = conn1.recv()
response = json.loads(response)
assert response["error"]["code"] == -32000, f"Incorrect error reported: {response}"
assert response["error"]["data"]["type"] == "TypeError", "Incorrect error type."
else:
assert False, "Timeout occurred while waiting for response."
# ------- Incorrect argument (incorrect argument type) -------
# 'json-prc' doesn't check parameter types. Instead the handler will crash if the argument
# type is not suitable.
request = format_jsonrpc_msg("method3", {"value": "abc"})
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5):
response = conn1.recv()
response = json.loads(response)
assert response["error"]["code"] == -32000, f"Incorrect error reported: {response}"
assert response["error"]["data"]["type"] == "TypeError", "Incorrect error type."
else:
assert False, "Timeout occurred while waiting for response."
# ------- Incorrect argument (extra argument) -------
request = format_jsonrpc_msg("method3", {"value": 5, "unknown": 10})
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5):
response = conn1.recv()
response = json.loads(response)
assert response["error"]["code"] == -32602, f"Incorrect error reported: {response}"
else:
assert False, "Timeout occurred while waiting for response."
# ------- Non-existing method ('Method not found' error) -------
request = format_jsonrpc_msg("method_handler3", {"value": 5})
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5):
response = conn1.recv()
response = json.loads(response)
assert response["error"]["code"] == -32601, f"Incorrect error reported: {response}"
else:
assert False, "Timeout occurred while waiting for response."
pc.stop()
def test_PipeJsonRpcReceive_7_failing():
"""
Exception is raised inside the handler is causing 'Server Error' -32000.
Returns error type (Exception type) and message. It is questionable whether
built-in exception processing should be used to process exceptions within handlers.
It may be better to catch and process all exceptions produced by the custom code and
leave built-in processing for exceptions that happen while calling the function.
EXCEPTIONS SHOULD BE PROCESSED INSIDE THE HANDLER!!!
"""
def method_handler5():
raise RuntimeError("Function crashed ...")
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2)
pc.add_method(method_handler5, "method5")
pc.start()
request = format_jsonrpc_msg("method5")
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5): # Set timeout large enough
response = conn1.recv()
response = json.loads(response)
assert response["error"]["code"] == -32000, f"Incorrect error reported: {response}"
assert response["error"]["data"]["type"] == "RuntimeError", "Incorrect error type."
assert response["error"]["data"]["message"] == "Function crashed ...", "Incorrect message."
else:
assert False, "Timeout occurred while waiting for response."
pc.stop()
def test_PipeJsonRpcReceive_8_failing():
"""
This is an example of handler to timeout. 'json-rpc' package can not handle timeouts.
Care must be taken to write handles that execute quickly. Timeouts must be handled
explicitly within the handlers.
ONLY INSTANTLY EXECUTED HANDLERS MAY BE USED!!!
"""
def method_handler6():
ttime.sleep(3) # Longer than 'poll' timeout
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2)
pc.add_method(method_handler6, "method6")
pc.start()
# Non-existing method ('Method not found' error)
request = format_jsonrpc_msg("method6")
conn1.send(json.dumps(request))
if conn1.poll(timeout=0.5): # Set timeout large enough
assert False, "The test is expected to timeout."
else:
pass
pc.stop()
# =======================================================================
# Class PipeJsonRpcSendAsync
def test_PipeJsonRpcSendAsync_1():
"""
Create, start and stop `PipeJsonRpcReceive` object
"""
conn1, conn2 = multiprocessing.Pipe()
new_name = "Unusual Thread Name"
async def object_start_stop():
assert count_threads_with_name(new_name) == 0, "No threads are expected to exist"
pc = PipeJsonRpcSendAsync(conn=conn1, name=new_name)
pc.start()
assert count_threads_with_name(new_name) == 2, "One thread is expected to exist"
pc.start() # Expected to do nothing
pc.stop()
ttime.sleep(0.15) # Wait until the thread stops (0.1s polling period)
assert count_threads_with_name(new_name) == 0, "No threads are expected to exist"
pc.start() # Restart
assert count_threads_with_name(new_name) == 2, "One thread is expected to exist"
pc.stop()
asyncio.run(object_start_stop())
@pytest.mark.parametrize(
"method, params, result, notification",
[
("method_handler1", [], 5, False),
("method_handler1", [], 5, True),
("method1", [], 5, False),
("method2", [5], 15, False),
("method2", {"value": 5}, 15, False),
("method2", {}, 12, False),
("method3", {"value": 5}, 20, False),
("method3", {}, 18, False),
("method4", {"value": 5}, 20, False),
("method4", {}, 19, False),
],
)
def test_PipeJsonRpcSendAsync_2(method, params, result, notification):
"""
Test of basic functionality. Here we don't test for timeout case (it raises an exception).
"""
value_nonlocal = None
def method_handler1():
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return 5
def method_handler2(value=2):
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return value + 10
def method_handler3(*, value=3):
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return value + 15
class SomeClass:
def method_handler4(self, *, value=4):
nonlocal value_nonlocal
value_nonlocal = "function_was_called"
return value + 15
some_class = SomeClass()
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2, name="comm-server")
pc.add_method(method_handler1) # No name is specified, default name is "method_handler1"
pc.add_method(method_handler1, "method1")
pc.add_method(method_handler2, "method2")
pc.add_method(method_handler3, "method3")
pc.add_method(some_class.method_handler4, "method4")
pc.start()
async def send_messages():
nonlocal value_nonlocal
p_send = PipeJsonRpcSendAsync(conn=conn1, name="comm-client")
p_send.start()
for n in range(3):
value_nonlocal = None
response = await p_send.send_msg(method, params, notification=notification)
if not notification:
assert response == result, f"Result does not match the expected: {response}"
assert value_nonlocal == "function_was_called", "Non-local variable has incorrect value"
elif response is not None:
assert False, "Response was received for notification."
p_send.stop()
asyncio.run(send_messages())
pc.stop()
def test_PipeJsonRpcSendAsync_3():
"""
Put multiple messages to the loop at once. The should be processed one by one.
"""
n_calls = 0
lock = threading.Lock()
def method_handler1():
nonlocal n_calls
with lock:
n_calls += 1
n_return = n_calls
ttime.sleep(0.1)
return n_return
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2, name="comm-server")
pc.add_method(method_handler1, "method1")
pc.start()
async def send_messages():
p_send = PipeJsonRpcSendAsync(conn=conn1, name="comm-client")
p_send.start()
# Submit multiple messages at once. Messages should stay at the event loop
# and be processed one by one.
futs = []
for n in range(5):
futs.append(asyncio.ensure_future(p_send.send_msg("method1")))
for n, fut in enumerate(futs):
await asyncio.wait_for(fut, timeout=5.0) # Timeout is in case of failure
result = fut.result()
assert result == n + 1, "Incorrect returned value"
p_send.stop()
asyncio.run(send_messages())
pc.stop()
def test_PipeJsonRpcSendAsync_4():
"""
Message timeout.
"""
def method_handler1():
ttime.sleep(1)
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2, name="comm-server")
pc.add_method(method_handler1, "method1")
pc.start()
async def send_messages():
p_send = PipeJsonRpcSendAsync(conn=conn1, name="comm-client")
p_send.start()
# Submit multiple messages at once. Messages should stay at the event loop
# and be processed one by one.
with pytest.raises(CommTimeoutError, match="Timeout while waiting for response to message"):
await p_send.send_msg("method1", timeout=0.5)
p_send.stop()
asyncio.run(send_messages())
pc.stop()
def test_PipeJsonRpcSendAsync_5():
"""
Special test case.
Two messages: the first message times out, the second message is send before the response
from the first message is received. Verify that the result returned in response to the
second message is received. (We discard the result of the message that is timed out.)
"""
def method_handler1():
ttime.sleep(0.7)
return 39
def method_handler2():
ttime.sleep(0.2)
return 56
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2, name="comm-server")
pc.add_method(method_handler1, "method1")
pc.add_method(method_handler2, "method2")
pc.start()
async def send_messages():
p_send = PipeJsonRpcSendAsync(conn=conn1, name="comm-client")
p_send.start()
# Submit multiple messages at once. Messages should stay at the event loop
# and be processed one by one.
with pytest.raises(CommTimeoutError):
await p_send.send_msg("method1", timeout=0.5)
result = await p_send.send_msg("method2", timeout=0.5)
assert result == 56, "Incorrect result received"
p_send.stop()
asyncio.run(send_messages())
pc.stop()
class _PipeJsonRpcReceiveTest(PipeJsonRpcReceive):
"""
Object that responds to a single request with multiple duplicates of the message.
The test emulates possible issues with the process receiving messages.
"""
def _handle_msg(self, msg):
response = JSONRPCResponseManager.handle(msg, self._dispatcher)
if response:
response = response.json
self._conn.send(response) # Send the response 3 times !!!
self._conn.send(response)
self._conn.send(response)
def test_PipeJsonRpcSendAsync_6(caplog):
"""
Special test case.
The receiving process responds with multiple replies (3) to a single request. Check that
only one (the first) message is processed and the following messages are ignored.
"""
caplog.set_level(logging.INFO)
def method_handler1():
return 39
conn1, conn2 = multiprocessing.Pipe()
pc = _PipeJsonRpcReceiveTest(conn=conn2, name="comm-server")
pc.add_method(method_handler1, "method1")
pc.start()
async def send_messages():
p_send = PipeJsonRpcSendAsync(conn=conn1, name="comm-client")
p_send.start()
result = await p_send.send_msg("method1", timeout=0.5)
assert result == 39, "Incorrect result received"
await asyncio.sleep(1) # Wait until additional messages are sent and received
p_send.stop()
asyncio.run(send_messages())
pc.stop()
txt = "Unexpected message received"
assert txt in caplog.text
assert caplog.text.count(txt) == 2, caplog.text
def test_PipeJsonRpcSendAsync_7_fail():
"""
Exception raised inside the method.
"""
def method_handler1():
raise RuntimeError("Function crashed ...")
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2, name="comm-server")
pc.add_method(method_handler1, "method1")
pc.start()
async def send_messages():
p_send = PipeJsonRpcSendAsync(conn=conn1, name="comm-client")
p_send.start()
# Submit multiple messages at once. Messages should stay at the event loop
# and be processed one by one.
with pytest.raises(CommJsonRpcError, match="Function crashed ..."):
await p_send.send_msg("method1", timeout=0.5)
p_send.stop()
asyncio.run(send_messages())
pc.stop()
def test_PipeJsonRpcSendAsync_8_fail():
"""
Method not found (other `json-rpc` errors will raise the same exception).
"""
def method_handler1():
pass
conn1, conn2 = multiprocessing.Pipe()
pc = PipeJsonRpcReceive(conn=conn2, name="comm-server")
pc.add_method(method_handler1, "method1")
pc.start()
async def send_messages():
p_send = PipeJsonRpcSendAsync(conn=conn1, name="comm-client")
p_send.start()
# Submit multiple messages at once. Messages should stay at the event loop
# and be processed one by one.
with pytest.raises(CommJsonRpcError):
await p_send.send_msg("nonexisting_method", timeout=0.5)
p_send.stop()
asyncio.run(send_messages())
pc.stop()
# =======================================================================
# ZMQ keys
def test_generate_zmq_keys():
"""
Functions ``generate_zmq_keys()``, ``generate_zmq_public_key()`` and ``validate_zmq_key()``.
"""
key_public, key_private = generate_zmq_keys()
assert isinstance(key_public, str)
assert len(key_public) == 40
assert isinstance(key_private, str)
assert len(key_private) == 40
validate_zmq_key(key_public)
validate_zmq_key(key_private)
key_public_gen = generate_zmq_public_key(key_private)
assert key_public_gen == key_public
# fmt: off
@pytest.mark.parametrize("key", [
None,
10,
"",
"abc",
"wt8[6a8eoXFRVL<l2JBbOzs(hcI%kRBIr0Do/eL'", # 40 characters, but contains invalid character "'"
])
# fmt: on
def test_validate_zmq_key(key):
"""
Function ``validate_zmq_key()``: cases of failing validation.
"""
with pytest.raises(ValueError, match="the key must be a 40 byte z85 encoded string"):
validate_zmq_key(key)
# =======================================================================
# Class ZMQCommSendThreads
def _gen_server_keys(*, encryption_enabled):
"""Generate server keys and a set of kwargs."""
if encryption_enabled:
public_key, private_key = generate_zmq_keys()
server_kwargs = {"private_key": private_key}
else:
public_key, private_key = None, None
server_kwargs = {}
return public_key, private_key, server_kwargs
def _zmq_server_1msg(*, private_key=None):
"""
ZMQ server that provides single response.
``private_key`` - server private key (for tests with enabled encryption)
"""
ctx = zmq.Context()
zmq_socket = ctx.socket(zmq.REP)
if private_key is not None:
zmq_socket.set(zmq.CURVE_SERVER, 1)
zmq_socket.set(zmq.CURVE_SECRETKEY, private_key.encode("utf-8"))
zmq_socket.bind("tcp://*:60615")
msg_in = zmq_socket.recv_json()
msg_out = {"success": True, "some_data": 10, "msg_in": msg_in}
zmq_socket.send_json(msg_out)
zmq_socket.close(linger=10)
# fmt: off
@pytest.mark.parametrize("encryption_enabled", [False, True])
@pytest.mark.parametrize("is_blocking", [True, False])
# fmt: on
def test_ZMQCommSendThreads_1(is_blocking, encryption_enabled):
"""
Basic test of ZMQCommSendThreads class: single communication with the
server both in blocking and non-blocking mode.
"""
public_key, _, server_kwargs = _gen_server_keys(encryption_enabled=encryption_enabled)
thread = threading.Thread(target=_zmq_server_1msg, kwargs=server_kwargs)
thread.start()
zmq_comm = ZMQCommSendThreads(server_public_key=public_key if encryption_enabled else None)
method, params = "testing", {"p1": 10, "p2": "abc"}
msg_recv, msg_recv_err = {}, ""
if is_blocking:
msg_recv = zmq_comm.send_message(method=method, params=params)
else:
done = False
def cb(msg, msg_err):
nonlocal msg_recv, msg_recv_err, done
msg_recv = msg
msg_recv_err = msg_err
done = True
zmq_comm.send_message(method=method, params=params, cb=cb)
# Implement primitive polling of 'done' flag
while not done:
ttime.sleep(0.1)
assert msg_recv["success"] is True, str(msg_recv)
assert msg_recv["some_data"] == 10, str(msg_recv)
assert msg_recv["msg_in"] == {"method": method, "params": params}, str(msg_recv)
assert msg_recv_err == ""
thread.join()
def _zmq_server_2msg(*, private_key=None):
"""
ZMQ server: provides ability to communicate twice.
``private_key`` - server private key (for tests with enabled encryption)
"""
ctx = zmq.Context()
zmq_socket = ctx.socket(zmq.REP)
if private_key is not None:
zmq_socket.set(zmq.CURVE_SERVER, 1)
zmq_socket.set(zmq.CURVE_SECRETKEY, private_key.encode("utf-8"))
zmq_socket.bind("tcp://*:60615")
msg_in = zmq_socket.recv_json()
msg_out = {"success": True, "some_data": 10, "msg_in": msg_in}
zmq_socket.send_json(msg_out)
msg_in = zmq_socket.recv_json()
msg_out = {"success": True, "some_data": 20, "msg_in": msg_in}
zmq_socket.send_json(msg_out)
zmq_socket.close(linger=10)
# fmt: off
@pytest.mark.parametrize("encryption_enabled", [False, True])
@pytest.mark.parametrize("is_blocking", [True, False])
# fmt: on
def test_ZMQCommSendThreads_2(is_blocking, encryption_enabled):
"""
Basic test of ZMQCommSendThreads class: two consecutive communications with the
server both in blocking and non-blocking mode.
"""
public_key, _, server_kwargs = _gen_server_keys(encryption_enabled=encryption_enabled)
thread = threading.Thread(target=_zmq_server_2msg, kwargs=server_kwargs)
thread.start()
zmq_comm = ZMQCommSendThreads(server_public_key=public_key if encryption_enabled else None)
method, params = "testing", {"p1": 10, "p2": "abc"}
msg_recv, msg_recv_err = {}, ""
for val in (10, 20):
if is_blocking:
msg_recv = zmq_comm.send_message(method=method, params=params)
else:
done = False
def cb(msg, msg_err):
nonlocal msg_recv, msg_recv_err, done
msg_recv = msg
msg_recv_err = msg_err
done = True
zmq_comm.send_message(method=method, params=params, cb=cb)
# Implement primitive polling of 'done' flag
while not done:
ttime.sleep(0.1)
assert msg_recv["success"] is True, str(msg_recv)
assert msg_recv["some_data"] == val, str(msg_recv)
assert msg_recv["msg_in"] == {"method": method, "params": params}, str(msg_recv)
assert msg_recv_err == ""
thread.join()
def _zmq_server_2msg_delay1(*, private_key=None):
"""
ZMQ server: provides ability to communicate twice, short delay before sending the 1st response
``private_key`` - server private key (for tests with enabled encryption)
"""
ctx = zmq.Context()
zmq_socket = ctx.socket(zmq.REP)
if private_key is not None:
zmq_socket.set(zmq.CURVE_SERVER, 1)
zmq_socket.set(zmq.CURVE_SECRETKEY, private_key.encode("utf-8"))
zmq_socket.bind("tcp://*:60615")
msg_in = zmq_socket.recv_json()
ttime.sleep(0.1) # Delay before the 1st response
msg_out = {"success": True, "some_data": 10, "msg_in": msg_in}
zmq_socket.send_json(msg_out)
msg_in = zmq_socket.recv_json()
msg_out = {"success": True, "some_data": 20, "msg_in": msg_in}
zmq_socket.send_json(msg_out)
zmq_socket.close(linger=10)
# fmt: off
@pytest.mark.parametrize("encryption_enabled", [False, True])
@pytest.mark.parametrize("is_blocking", [True, False])
# fmt: on
def test_ZMQCommSendThreads_3(is_blocking, encryption_enabled):
"""
Testing protection of '_zmq_communicate` with lock. In this test the function
``send_message` is called twice so that the second call is submitted before
the response to the first message is received. The server waits for 0.1 seconds
before responding to the 1st message to emulate delay in processing. Since
``_zmq_communicate`` is protected by a lock, the second request will not
be sent until the first message is processed.
"""
public_key, _, server_kwargs = _gen_server_keys(encryption_enabled=encryption_enabled)
thread = threading.Thread(target=_zmq_server_2msg_delay1, kwargs=server_kwargs)
thread.start()
zmq_comm = ZMQCommSendThreads(server_public_key=public_key if encryption_enabled else None)
method, params = "testing", {"p1": 10, "p2": "abc"}