|
20 | 20 |
|
21 | 21 | def test_ownership_receivers(child_sub, samples_sent, last_sample_saved, timeout): |
22 | 22 |
|
| 23 | + """ |
| 24 | + This function is used by test cases that have several publishers and one |
| 25 | + subscriber. |
| 26 | + This tests that the Ownership QoS works correctly. In order to do that the |
| 27 | + function checks if the subscriber has received samples from one publisher or |
| 28 | + from both. Each publisher should publish data with a different size. |
| 29 | + A subscriber has received from both publishers if there is a sample |
| 30 | + from each publisher interleaved. This is done to make sure the subscriber |
| 31 | + did not started receiving samples from the publisher that was run before, |
| 32 | + and then change to the publisher with the greatest ownership. |
| 33 | +
|
| 34 | + child_sub: child program generated with pexpect |
| 35 | + samples_sent: not used |
| 36 | + last_sample_saved: not used |
| 37 | + timeout: time pexpect waits until it matches a pattern. |
| 38 | +
|
| 39 | + This functions assumes that the subscriber has already received samples |
| 40 | + from, at least, one publisher. |
| 41 | + """ |
| 42 | + ignore_first_samples = True |
| 43 | + max_samples_received = MAX_SAMPLES_READ |
| 44 | + samples_read = 0 |
| 45 | + sizes_received = [] |
| 46 | + last_size_received = 0 |
| 47 | + |
| 48 | + while(samples_read < max_samples_received): |
| 49 | + # take the topic, color, position and size of the ShapeType. |
| 50 | + # child_sub.before contains x and y, and child_sub.after contains |
| 51 | + # [shapesize] |
| 52 | + # Example: child_sub.before contains 'Square BLUE 191 152' |
| 53 | + # child_sub.after contains '[30]' |
| 54 | + sub_string = re.search('[0-9]+ [0-9]+ \[([0-9]+)\]', |
| 55 | + child_sub.before + child_sub.after) |
| 56 | + # sub_string contains 'x y [shapesize]', example: '191 152 [30]' |
| 57 | + |
| 58 | + # Determine from which publisher the current sample belongs to |
| 59 | + # size determines the publisher |
| 60 | + if sub_string is not None: |
| 61 | + if int(sub_string.group(1)) not in sizes_received: |
| 62 | + last_size_received = int(sub_string.group(1)) |
| 63 | + sizes_received.append(last_size_received) |
| 64 | + else: |
| 65 | + return ReturnCode.DATA_NOT_RECEIVED |
| 66 | + |
| 67 | + # A potential case is that the reader gets data from one writer and |
| 68 | + # then start receiving from a different writer with a higher |
| 69 | + # ownership. This avoids returning RECEIVING_FROM_BOTH if this is |
| 70 | + # the case. |
| 71 | + # This if is only run once we process the first sample received by the |
| 72 | + # subscriber application |
| 73 | + if ignore_first_samples == True and len(sizes_received) == 2: |
| 74 | + # if we have received samples from both publishers, then we stop |
| 75 | + # ignoring samples |
| 76 | + ignore_first_samples = False |
| 77 | + # only leave the last received sample in the sizes_received list |
| 78 | + sizes_received.clear() |
| 79 | + sizes_received.append(last_size_received) |
| 80 | + |
| 81 | + # Get the next samples the subscriber is receiving |
| 82 | + index = child_sub.expect( |
| 83 | + [ |
| 84 | + '\[[0-9]+\]', # index = 0 |
| 85 | + pexpect.TIMEOUT, # index = 1 |
| 86 | + ], |
| 87 | + timeout |
| 88 | + ) |
| 89 | + if index == 1: |
| 90 | + break |
| 91 | + |
| 92 | + samples_read += 1 |
| 93 | + |
| 94 | + print(f'Samples read: {samples_read}') |
| 95 | + if len(sizes_received) == 2: |
| 96 | + return ReturnCode.RECEIVING_FROM_BOTH |
| 97 | + elif len(sizes_received) == 1: |
| 98 | + return ReturnCode.RECEIVING_FROM_ONE |
| 99 | + return ReturnCode.DATA_NOT_RECEIVED |
| 100 | + |
| 101 | +def test_ownership_receivers_by_samples_sent(child_sub, samples_sent, last_sample_saved, timeout): |
| 102 | + |
23 | 103 | """ |
24 | 104 | This function is used by test cases that have two publishers and one subscriber. |
25 | 105 | This tests that the Ownership QoS works correctly. In order to do that the |
|
0 commit comments