forked from omg-dds/dds-rtps
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_suite_functions.py
More file actions
1108 lines (949 loc) · 44.5 KB
/
Copy pathtest_suite_functions.py
File metadata and controls
1108 lines (949 loc) · 44.5 KB
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
#################################################################
# Use and redistribution is source and binary forms is permitted
# subject to the OMG-DDS INTEROPERABILITY TESTING LICENSE found
# at the following URL:
#
# https://github.com/omg-dds/dds-rtps/blob/master/LICENSE.md
#
#################################################################
from rtps_test_utilities import ReturnCode, basic_check
import re
import pexpect
import queue
import time
# This constant is used to limit the maximum number of samples that tests that
# check the behavior needs to read. For example, checking that the data
# is received in order, or that OWNERSHIP works properly, etc...
MAX_SAMPLES_READ = 500
def test_size_receivers(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function is used by test cases that have two publishers and one
subscriber. This tests check how many samples are received by the
subscriber application with different sizes.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern.
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
sub_string = re.search(r'\w\s+\w+\s+[0-9]+ [0-9]+ \[([0-9]+)\]',
child_sub.before + child_sub.after)
if sub_string is None:
return ReturnCode.DATA_NOT_RECEIVED
first_sample_size = int(sub_string.group(1))
max_samples_received = MAX_SAMPLES_READ
samples_read = 0
ignore_first_samples = True
retcode = ReturnCode.RECEIVING_FROM_ONE
while sub_string is not None and samples_read < max_samples_received:
current_sample_size = int(sub_string.group(1))
if current_sample_size != first_sample_size:
if ignore_first_samples:
# the first time we receive a different size, ignore it
# For example, if we receive samples of size 20, then only
# samples of size 30, we have to return RECEIVING_FROM_ONE.
# If after receiving samples of size 30, we receive samples of
# size 20 again, then we return RECEIVING_FROM_BOTH.
ignore_first_samples = False
first_sample_size = current_sample_size
else:
retcode = ReturnCode.RECEIVING_FROM_BOTH
break
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 1:
break
elif index == 2:
return ReturnCode.DATA_NOT_RECEIVED
samples_read += 1
sub_string = re.search(r'\w\s+\w+\s+[0-9]+ [0-9]+ \[([0-9]+)\]',
child_sub.before + child_sub.after)
print(f'Samples read: {samples_read}')
return retcode
def test_color_receivers(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function is used by test cases that have two publishers and one
subscriber. This tests how many samples are received by the
subscriber application with different colors.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern.
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
sub_string = re.search(r'\w\s+(\w+)\s+[0-9]+ [0-9]+ \[[0-9]+\]',
child_sub.before + child_sub.after)
first_sample_color = sub_string.group(1)
max_samples_received = MAX_SAMPLES_READ
samples_read = 0
while sub_string is not None and samples_read < max_samples_received:
current_sample_color = sub_string.group(1)
# Check that all received samples have the same color
if current_sample_color != first_sample_color:
return ReturnCode.RECEIVING_FROM_BOTH
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 1:
break
elif index == 2:
return ReturnCode.DATA_NOT_RECEIVED
samples_read += 1
sub_string = re.search(r'\w\s+(\w+)\s+[0-9]+ [0-9]+ \[[0-9]+\]',
child_sub.before + child_sub.after)
print(f'Samples read: {samples_read}')
return ReturnCode.RECEIVING_FROM_ONE
def test_size_less_than_20(child_sub, samples_sent, last_sample_saved, timeout):
"""
Checks that all received samples have size between 1 and 20 (inclusive).
Returns ReturnCode.OK if all samples are in range, otherwise ReturnCode.DATA_NOT_CORRECT.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern.
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
max_samples_received = MAX_SAMPLES_READ / 2
samples_read = 0
return_code = ReturnCode.OK
sub_string = re.search(r'[0-9]+ [0-9]+ \[([0-9]+)\]', child_sub.before + child_sub.after)
while sub_string is not None and samples_read < max_samples_received:
size = int(sub_string.group(1))
if size < 1 or size > 20:
return_code = ReturnCode.DATA_NOT_CORRECT
break
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 1 or index == 2:
return_code = ReturnCode.DATA_NOT_RECEIVED
break
samples_read += 1
sub_string = re.search(r'[0-9]+ [0-9]+ \[([0-9]+)\]', child_sub.before + child_sub.after)
print(f'Samples read: {samples_read}')
return return_code
def test_order_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests that the subscriber receives the samples in order
(for several instances)
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern.
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.DATA_NOT_RECEIVED
instance_color = []
instance_seq_num = []
first_iteration = []
samples_read_per_instance = 0
max_samples_received = MAX_SAMPLES_READ
while samples_read_per_instance < max_samples_received:
sub_string = re.search(r'\w+\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[([0-9]+)\]',
child_sub.before + child_sub.after)
if sub_string is not None:
# add a new instance to instance_color
if sub_string.group(1) not in instance_color:
instance_color.append(sub_string.group(1))
instance_seq_num.append(int(sub_string.group(2)))
first_iteration.append(True)
if sub_string.group(1) in instance_color:
index = instance_color.index(sub_string.group(1))
if first_iteration[index]:
first_iteration[index] = False
else:
# check that the next sequence number is the next value
current_size = int(sub_string.group(2))
if (current_size > instance_seq_num[index]):
instance_seq_num[index] = current_size
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
# Get the next sample the subscriber is receiving
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
r'Reading with ordered access.*?\n', # index = 1
pexpect.TIMEOUT, # index = 2
pexpect.EOF # index = 3
],
timeout
)
if index == 0:
if sub_string is not None and sub_string.group(1) == instance_color[0]:
samples_read_per_instance += 1
elif index == 2:
# no more data to process
break
elif index == 3:
return ReturnCode.DATA_NOT_RECEIVED
if max_samples_received == samples_read_per_instance:
produced_code = ReturnCode.OK
print(f'Samples read per instance: {samples_read_per_instance}, instances: {instance_color}')
return produced_code
def test_reliability_no_losses_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests RELIABLE reliability, it checks whether the subscriber
receives the samples in order and with no losses (for several instances)
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern.
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.DATA_NOT_RECEIVED
instance_color = []
instance_seq_num = []
first_iteration = []
samples_read_per_instance = 0
max_samples_received = MAX_SAMPLES_READ
while samples_read_per_instance < max_samples_received:
sub_string = re.search(r'\w+\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[([0-9]+)\]',
child_sub.before + child_sub.after)
if sub_string is not None:
# add a new instance to instance_color
if sub_string.group(1) not in instance_color:
instance_color.append(sub_string.group(1))
instance_seq_num.append(int(sub_string.group(2)))
first_iteration.append(True)
if sub_string.group(1) in instance_color:
index = instance_color.index(sub_string.group(1))
if first_iteration[index]:
first_iteration[index] = False
else:
# check that the next sequence number is the next value
instance_seq_num[index] += 1
if instance_seq_num[index] != int(sub_string.group(2)):
produced_code = ReturnCode.DATA_NOT_CORRECT
break
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
# Get the next sample the subscriber is receiving
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 0:
if sub_string is not None and sub_string.group(1) == instance_color[0]:
samples_read_per_instance += 1
elif index == 1:
# no more data to process
break
elif index == 2:
return ReturnCode.DATA_NOT_RECEIVED
if max_samples_received == samples_read_per_instance:
produced_code = ReturnCode.OK
print(f'Samples read per instance: {samples_read_per_instance}, instances: {instance_color}')
return produced_code
def test_durability_volatile(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests the volatile durability, it checks that the sample the
subscriber receives is not the first one. The publisher application sends
samples increasing the value of the size, so if the first sample that the
subscriber app doesn't have the size > 5, the test is correct.
Note: size > 5 to avoid checking only the first sample, that may be an edge
case where the DataReader hasn't matched with the DataWriter yet and
the first samples are not received.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: not used
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
# Read the first sample, if it has the size > 5, it is using volatile
# durability correctly
sub_string = re.search(r'[0-9]+ [0-9]+ \[([0-9]+)\]',
child_sub.before + child_sub.after)
# Check if the element received is not the first 5 samples (aka size >= 5)
# which should not be the case because the subscriber application waits some
# seconds after the publisher. Checking 5 samples instead of just one to
# make sure that there is not the case in which the DataReader hasn't
# matched with the DataWriter yet and the first samples may not be received.
# The group(1) contains the matching element for the parameter between
# brackets in the regular expression. In this case is the size as a string.
if int(sub_string.group(1)) >= 5:
produced_code = ReturnCode.OK
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
return produced_code
def test_durability_transient_local(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests the TRANSIENT_LOCAL durability, it checks that the
sample the subscriber receives is the first one. The publisher application
sends samples increasing the value of the size, so if the first sample that
the subscriber app does have the size == 1, the test is correct.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: not used
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
# Read the first sample, if it has the size == 1, it is using transient
# local durability correctly
sub_string = re.search(r'[0-9]+ [0-9]+ \[([0-9]+)\]',
child_sub.before + child_sub.after)
# Check if the element is the first one sent (aka size == 1), which should
# be the case for TRANSIENT_LOCAL durability.
# The group(1) contains the matching element for the parameter between
# brackets in the regular expression. In this case is the size as a string.
if int(sub_string.group(1)) == 1:
produced_code = ReturnCode.OK
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
return produced_code
def test_deadline_missed(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests whether the subscriber application misses the requested
deadline or not. This is needed in case the subscriber application receives
some samples and then missed the requested deadline.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
# At this point, the subscriber app has already received one sample
# Check deadline requested missed
index = child_sub.expect([
'on_requested_deadline_missed()', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout)
if index == 0:
return ReturnCode.DEADLINE_MISSED
elif index == 1:
return ReturnCode.DATA_NOT_RECEIVED
else:
index = child_sub.expect([
r'\[[0-9]+\]', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout)
if index == 0:
return ReturnCode.OK
else:
return ReturnCode.DATA_NOT_RECEIVED
def test_reading_1_sample_every_10_samples_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests whether the subscriber application receives one sample
out of 10 (for each instance). For example, first sample received with size
5, the next one should have size [15,24], then [25,34], etc.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.DATA_NOT_RECEIVED
instance_color = []
instance_seq_num = []
first_iteration = []
ignore_first_sample = []
max_samples_received = MAX_SAMPLES_READ / 20 # 25
samples_read_per_instance = 0
while samples_read_per_instance < max_samples_received:
sub_string = re.search(r'\w+\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[([0-9]+)\]',
child_sub.before + child_sub.after)
if sub_string is not None:
# add a new instance to instance_color
if sub_string.group(1) not in instance_color:
instance_color.append(sub_string.group(1))
instance_seq_num.append(int(sub_string.group(2)))
first_iteration.append(True)
ignore_first_sample.append(True)
if sub_string.group(1) in instance_color:
index = instance_color.index(sub_string.group(1))
if first_iteration[index]:
first_iteration[index] = False
else:
current_seq_num = int(sub_string.group(2))
if ignore_first_sample[index]:
ignore_first_sample[index] = False
else:
# check that the received sample reads only one sample in
# in the period of 10 samples. For example, if the previous
# sample received has size 5, the next one should be
# between [15-24], both included.
# As the write period does not take into account the
# execution overhead, the next valid sample may be
# between [14-24] if the filtering happens in the reader
# side.
if current_seq_num < (instance_seq_num[index] + 9) or current_seq_num > instance_seq_num[index] + 19:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
instance_seq_num[index] = current_seq_num
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
# Get the next sample the subscriber is receiving
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 0:
if sub_string is not None and sub_string.group(1) == instance_color[0]:
# increase samples_read_per_instance only for the first instance
samples_read_per_instance += 1
elif index == 1:
# no more data to process
break
elif index == 2:
return ReturnCode.DATA_NOT_RECEIVED
if max_samples_received == samples_read_per_instance:
produced_code = ReturnCode.OK
print(f'Samples read per instance: {samples_read_per_instance}, instances: {instance_color}')
return produced_code
def test_unregistering_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests whether instances are correctly unregistered
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.OK
instance_color = []
unregistered_instance_color = []
max_samples_received = MAX_SAMPLES_READ
samples_read_per_instance = 0
while samples_read_per_instance < max_samples_received:
sub_string = re.search(r'\w+\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[([0-9]+)\]',
child_sub.before + child_sub.after)
if sub_string is not None:
# add a new instance to instance_color
if sub_string.group(1) not in instance_color:
instance_color.append(sub_string.group(1))
else:
# if no sample is received, it might be a UNREGISTER message
sub_string = re.search(r'\w+\s+(\w+)\s+NOT_ALIVE_NO_WRITERS_INSTANCE_STATE',
child_sub.before + child_sub.after)
if sub_string is not None and sub_string.group(1) not in unregistered_instance_color:
unregistered_instance_color.append(sub_string.group(1))
if len(instance_color) == len(unregistered_instance_color):
break
# Get the next sample the subscriber is receiving or unregister/dispose
index = child_sub.expect(
[
r'\w+\s+\w+\s+.*?\n', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 0:
if sub_string is not None and sub_string.group(1) == instance_color[0]:
samples_read_per_instance += 1
elif index == 1:
# no more data to process
break
elif index == 2:
return ReturnCode.DATA_NOT_RECEIVED
# compare that arrays contain the same elements and are not empty
if len(instance_color) == 0:
produced_code = ReturnCode.DATA_NOT_RECEIVED
elif set(instance_color) == set(unregistered_instance_color):
produced_code = ReturnCode.OK
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
print(f'Unregistered {len(unregistered_instance_color)} elements: {unregistered_instance_color}')
return produced_code
def test_disposing_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests whether instances are correctly disposed
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.OK
instance_color = []
disposed_instance_color = []
max_samples_received = MAX_SAMPLES_READ
samples_read_per_instance = 0
while samples_read_per_instance < max_samples_received:
sub_string = re.search(r'\w+\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[([0-9]+)\]',
child_sub.before + child_sub.after)
if sub_string is not None:
# add a new instance to instance_color
if sub_string.group(1) not in instance_color:
instance_color.append(sub_string.group(1))
else:
# if no sample is received, it might be a DISPOSED message
sub_string = re.search(r'\w+\s+(\w+)\s+NOT_ALIVE_DISPOSED_INSTANCE_STATE',
child_sub.before + child_sub.after)
if sub_string is not None and sub_string.group(1) not in disposed_instance_color:
disposed_instance_color.append(sub_string.group(1))
if len(instance_color) == len(disposed_instance_color):
break
# Get the next sample the subscriber is receiving or unregister/dispose
index = child_sub.expect(
[
r'\w+\s+\w+\s+.*?\n', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 0:
if sub_string is not None and sub_string.group(1) == instance_color[0]:
samples_read_per_instance += 1
elif index == 1:
# no more data to process
break
elif index == 2:
return ReturnCode.DATA_NOT_RECEIVED
# compare that arrays contain the same elements and are not empty
if len(instance_color) == 0:
produced_code = ReturnCode.DATA_NOT_RECEIVED
elif set(instance_color) == set(disposed_instance_color):
produced_code = ReturnCode.OK
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
print(f'Disposed {len(disposed_instance_color)} elements: {disposed_instance_color}')
return produced_code
def test_large_data(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests whether large data is correctly received
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.DATA_NOT_RECEIVED
samples_read = 0
while samples_read < MAX_SAMPLES_READ:
# As the interoperability_report is just looking for the size [<size>],
# this does not count the data after it, we need to read a full sample
index = child_sub.expect(
[
r'\w+\s+\w+\s+[0-9]+\s+[0-9]+\s+\[[0-9]+\]\s+\{[0-9]+\}', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 0:
# Read the sample received, if it prints the additional_bytes == 255,
# it is sending large data correctly
sub_string = re.search(r'\w+\s+\w+\s+[0-9]+\s+[0-9]+\s+\[[0-9]+\]\s+\{([0-9]+)\}',
child_sub.before + child_sub.after)
# Check if the last element of the additional_bytes field element is
# received correctly
if sub_string is not None:
if int(sub_string.group(1)) != 255:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
samples_read += 1
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
elif index == 1 or index == 2:
produced_code = ReturnCode.DATA_NOT_RECEIVED
break
if samples_read == MAX_SAMPLES_READ:
produced_code = ReturnCode.OK
print(f'Samples read: {samples_read}')
return produced_code
def test_lifespan_2_3_consecutive_samples_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests that lifespan works correctly. In the test situation,
only 2 or 3 consecutive samples should be received each time the reader
reads data.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.DATA_NOT_RECEIVED
# as the test is reading in a slower rate, reduce the number of samples read
max_samples_lifespan = MAX_SAMPLES_READ / 10 # 50
instance_color = []
previous_seq_num = []
first_iteration = []
samples_read_per_instance = 0
consecutive_samples = []
ignore_first_sample = []
while samples_read_per_instance < max_samples_lifespan:
sub_string = re.search(r'\w+\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[([0-9]+)\]',
child_sub.before + child_sub.after)
if sub_string is not None:
# add a new instance to instance_color
if sub_string.group(1) not in instance_color:
instance_color.append(sub_string.group(1))
previous_seq_num.append(int(sub_string.group(2)))
first_iteration.append(True)
consecutive_samples.append(1) # take into account the first sample
ignore_first_sample.append(True)
# if the instance exists
if sub_string.group(1) in instance_color:
index = instance_color.index(sub_string.group(1))
# we should receive only 2 or 3 consecutive samples with the
# parameters defined by the test
if first_iteration[index]:
# do nothing for the first sample received
first_iteration[index] = False
else:
# if the sequence number is consecutive, increase the counter
if previous_seq_num[index] + 1 == int(sub_string.group(2)):
consecutive_samples[index] += 1
# if found consecutive samples, do not ignore the first sample
ignore_first_sample[index] = False
else:
# if the sequence number is not consecutive, check that we
# receive only 3 or 2 samples
if consecutive_samples[index] == 3 or consecutive_samples[index] == 2:
# reset value to 1, as this test consider that the first
# sample is consecutive with itself
consecutive_samples[index] = 1
produced_code = ReturnCode.OK
else:
if ignore_first_sample[index]:
# there may be a case in which we receive a sample
# and the next one is not consecutive, if that is the
# case, ignore it
ignore_first_sample[index] = False
else:
# if the amount of samples received is different than 3 or 2
# this is an error
produced_code = ReturnCode.DATA_NOT_CORRECT
break
previous_seq_num[index] = int(sub_string.group(2))
else:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
# Get the next sample the subscriber is receiving
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
pexpect.TIMEOUT, # index = 1
pexpect.EOF # index = 2
],
timeout
)
if index == 0:
if sub_string is not None and sub_string.group(1) == instance_color[0]:
# increase samples_read_per_instance only for the first instance
samples_read_per_instance += 1
elif index == 1:
# no more data to process
break
elif index == 2:
return ReturnCode.DATA_NOT_RECEIVED
if max_samples_lifespan == samples_read_per_instance:
produced_code = ReturnCode.OK
print(f'Samples read per instance: {samples_read_per_instance}, instances: {instance_color}')
return produced_code
def ordered_access_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests that ordered access works correctly. This counts the
samples received in order and detects whether they are from the same instance
as the previously received sample or not.
If the number of consecutive samples from the same instance is greater than
the number of consecutive samples form different instances, this means that
the DW is using INSTANCE_PRESENTATION, if the case is the opposite, it is
using TOPIC_PRESENTATION.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.OK
instance_color = []
samples_read_per_instance = 0
previous_sample_color = None
color_different_count = 0
color_equal_count = 0
samples_printed = False
ordered_access_group_count = 0
while samples_read_per_instance < MAX_SAMPLES_READ:
sub_string = re.search(r'\w+\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[[0-9]+\]',
child_sub.before + child_sub.after)
current_color = None
# if a sample is read
if sub_string is not None:
# samples have been printed at least once
samples_printed = True
current_color = sub_string.group(1)
# add new instance to instance_color
if current_color not in instance_color:
instance_color.append(current_color)
# the instance exists
if current_color in instance_color:
index = instance_color.index(current_color)
# check the previous color and increase the different or equal
# counters
if previous_sample_color is not None:
if current_color != previous_sample_color:
color_different_count += 1
else:
color_equal_count += 1
previous_sample_color = current_color
# different message than a sample
else:
sub_string = re.search(r'Reading with ordered access',
child_sub.before + child_sub.after)
# if 'Reading with ordered access' message, it means that the DataReader
# is reading a new set of data (DataReader reads data slower that a
# DataWriter writes it)
if sub_string is not None:
# if samples have been already received by the DataReader and the
# counter addition (samples read) is greater than 5. It is 5 because
# there are 4 instances and we need to make sure that we receive
# at least 1 sample for every instance
# Note: color_equal_count + color_different_count will be the samples read
if samples_printed and color_equal_count + color_different_count > 5:
# if produced_code is not OK (this will happen in all iterations
# except for the first one). We check that the behavior is the same
# as in previous iterations.
if produced_code != ReturnCode.OK:
current_behavior = None
if color_equal_count > color_different_count:
current_behavior = ReturnCode.ORDERED_ACCESS_INSTANCE
elif color_equal_count < color_different_count:
current_behavior = ReturnCode.ORDERED_ACCESS_TOPIC
# in case of a behavior change, this will be an error
if produced_code != current_behavior:
produced_code = ReturnCode.DATA_NOT_CORRECT
break
# this only happens on the first iteration and then this sets
# the initial ReturnCode
else:
if color_equal_count > color_different_count:
produced_code = ReturnCode.ORDERED_ACCESS_INSTANCE
elif color_equal_count < color_different_count:
produced_code = ReturnCode.ORDERED_ACCESS_TOPIC
# reset counters for the next set of samples read
color_equal_count = 0
color_different_count = 0
# Get the next sample the subscriber is receiving or the next
# 'Reading with ordered access message'
index = child_sub.expect(
[
r'\[[0-9]+\]', # index = 0
r'Reading with ordered access.*?\n', # index = 1
pexpect.TIMEOUT, # index = 2
pexpect.EOF # index = 3
],
timeout
)
if index == 0:
if current_color is not None and current_color == instance_color[0]:
samples_read_per_instance += 1
elif index == 1:
ordered_access_group_count += 1
elif index == 2:
# no more data to process
break
elif index == 3:
produced_code = ReturnCode.DATA_NOT_RECEIVED
break
# Exit condition in case there are no samples being printed
if ordered_access_group_count > MAX_SAMPLES_READ:
# If we have not read enough samples, we consider it a failure
if samples_read_per_instance < MAX_SAMPLES_READ:
produced_code = ReturnCode.DATA_NOT_RECEIVED
break
print(f'Samples read per instance: {samples_read_per_instance}, instances: {instance_color}')
return produced_code
def coherent_sets_w_instances(child_sub, samples_sent, last_sample_saved, timeout):
"""
This function tests that coherent sets works correctly. This counts the
consecutive samples received from the same instance. The value should be 3
as this is the coherent set count that the test is setting.
Note: when using GROUP_PRESENTATION, the first iteration may print more
samples (more coherent sets), the test checks that the samples received per
instance is a multiple of 3, so the coherent sets are received complete.
child_sub: child program generated with pexpect
samples_sent: not used
last_sample_saved: not used
timeout: time pexpect waits until it matches a pattern
"""
basic_check_retcode = basic_check(child_sub, samples_sent, last_sample_saved, timeout)
if basic_check_retcode != ReturnCode.OK:
return basic_check_retcode
produced_code = ReturnCode.DATA_NOT_RECEIVED
topics = {}
samples_read_per_instance = 0
previous_sample_color = None
new_coherent_set_read = False
first_time_reading = True
ignore_firsts_coherent_set = 2
coherent_sets_count = 0
coherent_set_sample_count = 0
coherent_sets_max_count = MAX_SAMPLES_READ / 5 # 100
while samples_read_per_instance < coherent_sets_max_count:
sub_string = re.search(r'(\w+)\s+(\w+)\s+[0-9]+\s+[0-9]+\s+\[[0-9]+\]',
child_sub.before + child_sub.after)
topic_name = None
instance_color = None
# if a sample is read
if sub_string is not None:
# DataReader has received a new coherent set
new_coherent_set_read = True
# add new instances to the corresponding topic