-
Notifications
You must be signed in to change notification settings - Fork 10
/
pub_test.c
595 lines (494 loc) · 17.9 KB
/
pub_test.c
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
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#include "rmc_pub.h"
#include <stdio.h>
#include <stdlib.h>
static uint8_t _test_print_pending(pub_packet_node_t* node, void* dt)
{
pub_packet_t* pack = (pub_packet_t*) node->data;
int indent = *((int*) dt);
printf("%*cPacket %p\n", indent*2, ' ', pack);
printf("%*c PID %llu\n", indent*2, ' ', (long long unsigned) pack->pid);
printf("%*c Sent timestamp %lld\n", indent*2, ' ', (long long int) pack->send_ts);
printf("%*c Reference count %d\n", indent*2, ' ', pack->ref_count);
printf("%*c Parent node %p\n", indent*2, ' ', pack->parent_node);
printf("%*c Pa yload Length %d\n", indent*2, ' ', pack->payload_len);
putchar('\n');
return 1;
}
static uint8_t _test_print_subscriber(pub_sub_node_t* node, void* dt)
{
pub_subscriber_t* sub = node->data;
int indent = *((int*) dt);
printf("%*cSubscriber %p\n", indent*2, ' ', sub);
if (pub_packet_list_size(&sub->inflight) > 0) {
printf("%*cInflight packets:\n", indent*3, ' ');
indent +=2;
pub_packet_list_for_each(&sub->inflight, _test_print_pending, (void*) &indent);
indent -=2;
} else
printf("%*cInflight packets: [None]\n", indent*2, ' ');
putchar('\n');
return 1;
}
void test_print_pub_context(pub_context_t* ctx)
{
int ind = 1;
printf("Context %p\n", ctx);
printf("Next PID %llu\n", (long long unsigned) ctx->next_pid);
if (pub_packet_list_size(&ctx->queued) > 0) {
printf("Queued Packets:\n");
pub_packet_list_for_each(&ctx->queued, _test_print_pending, (void*) &ind);
} else
printf("Queued Packets: [None]\n");
if (pub_packet_list_size(&ctx->inflight) > 0) {
printf("\nInflight Packets:\n");
pub_packet_list_for_each(&ctx->inflight, _test_print_pending, (void*) &ind);
} else
printf("Inflight Packets: [None]\n");
if (pub_sub_list_size(&ctx->subscribers) > 0) {
printf("\nSubscribers:\n");
pub_sub_list_for_each(&ctx->subscribers, _test_print_subscriber, (void*) &ind);
} else
printf("Subscribers: [None]\n");
}
void test_pub(void)
{
pub_context_t ctx;
pub_subscriber_t sub1;
pub_subscriber_t sub2;
pub_subscriber_t sub3;
pub_subscriber_t* sptr1 = 0;
pub_subscriber_t* sptr2 = 0;
pub_packet_t* pack;
packet_id_t pid = 0;
pub_sub_list_t sub_lst;
usec_timestamp_t ts = 0;
pub_init_context(&ctx);
pub_init_subscriber(&sub1, &ctx, user_data_nil());
pub_init_subscriber(&sub2, &ctx, user_data_nil());
pub_init_subscriber(&sub3, &ctx, user_data_nil());
pub_queue_packet(&ctx, "1", 2, user_data_nil());
pub_queue_packet(&ctx, "2", 2, user_data_nil());
pub_queue_packet(&ctx, "3", 2, user_data_nil());
pub_queue_packet(&ctx, "4", 2, user_data_nil());
pub_queue_packet(&ctx, "5", 2, user_data_nil());
pub_queue_packet(&ctx, "6", 2, user_data_nil());
pid = pub_queue_packet(&ctx, "7", 2, user_data_nil());
if (pid != 7) {
printf("Failed pub test 1.1. Wanted packet id 7, got %llu\n",
(long long unsigned) pid);
exit(255);
}
if (pub_packet_list_size(&ctx.queued) != 7) {
printf("Failed pub test 1.2. Wanted size 7, got %d\n",
pub_packet_list_size(&ctx.queued));
exit(255);
}
if (pub_packet_list_size(&ctx.inflight) != 0) {
printf("Failed pub test 1.3. Wanted size 0, got %d\n",
pub_packet_list_size(&ctx.inflight));
exit(255);
}
pack = pub_next_queued_packet(&ctx);
if (pack->pid != 1) {
printf("Failed pub test 2.1. Wanted packet id 1, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pub_packet_sent(&ctx, pack, rmc_usec_monotonic_timestamp());
pack = pub_next_queued_packet(&ctx);
if (pack->pid != 2) {
printf("Failed pub test 2.2. Wanted packet id 2, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pub_packet_sent(&ctx, pack, rmc_usec_monotonic_timestamp());
pack = pub_next_queued_packet(&ctx);
if (pack->pid != 3) {
printf("Failed pub test 2.3. Wanted packet id 3, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pub_packet_sent(&ctx, pack, rmc_usec_monotonic_timestamp());
// Check that we have four of original seven packets left in queue.
if (pub_packet_list_size(&ctx.queued) != 4) {
printf("Failed pub test 3.1. Wanted size 4, got %d\n",
pub_packet_list_size(&ctx.queued));
exit(255);
}
// Check that we have three inflight packets
if (pub_packet_list_size(&ctx.inflight) != 3) {
printf("Failed pub test 3.2. Wanted size 3, got %d\n",
pub_packet_list_size(&ctx.inflight));
exit(255);
}
//
// Check that all subscribers have three inflight packets.
//
if (pub_packet_list_size(&sub1.inflight) != 3) {
printf("Failed pub test 4.1. Wanted size 3, got %d\n",
pub_packet_list_size(&sub1.inflight));
exit(255);
}
if (pub_packet_list_size(&sub2.inflight) != 3) {
printf("Failed pub test 4.2. Wanted size 3, got %d\n",
pub_packet_list_size(&sub3.inflight));
exit(255);
}
if (pub_packet_list_size(&sub3.inflight) != 3) {
printf("Failed pub test 4.3. Wanted size 3, got %d\n",
pub_packet_list_size(&sub3.inflight));
exit(255);
}
// Check that sub is in descending order.
pack = (pub_packet_t*) pub_packet_list_head(&sub1.inflight)->data;
if (pack->pid != 3) {
printf("Failed pub test 5.1. Wanted pid 3, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pack = (pub_packet_t*) pub_packet_list_tail(&sub1.inflight)->data;
if (pack->pid != 1) {
printf("Failed pub test 5.2. Wanted pid 1, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
// Ack the first packet.
pub_packet_ack(&sub1, 1, 0);
//
// Do we have two elements left in flight for subscriber.
//
if (pub_packet_list_size(&sub1.inflight) != 2) {
printf("Failed pub test 6.1. Wanted size 2, got %d\n",
pub_packet_list_size(&sub1.inflight));
exit(255);
}
// Inspect he two elements left and ensure that they are correct.
pack = (pub_packet_t*) pub_packet_list_head(&sub1.inflight)->data;
if (pack->pid != 3) {
printf("Failed pub test 6.2. Wanted pid 2, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pack = (pub_packet_t*) pub_packet_list_tail(&sub1.inflight)->data;
if (pack->pid != 2) {
printf("Failed pub test 6.3. Wanted size 3, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
// Inspect the element in the context's inflight queue
// It should be tail in the queue since we sort on descending.
pack = (pub_packet_t*) pub_packet_list_tail(&ctx.inflight)->data;
if (pack->pid != 1) {
printf("Failed pub test 6.4. Wanted pid 1, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
// Is ref count correctly decreased?
if (pack->ref_count != 2) {
printf("Failed pub test 6.5. Wanted ref count 2, got %d\n",
pack->ref_count);
exit(255);
}
// Ack the remaining two subscribers for pid 1.
pub_packet_ack(&sub2, 1, 0);
pub_packet_ack(&sub3, 1, 0);
// Check size of inflight elements, which should have been decreased by one.
if (pub_packet_list_size(&ctx.inflight) != 2) {
printf("Failed pub test 7.1. Wanted size 2, got %d\n",
pub_packet_list_size(&ctx.inflight));
exit(255);
}
// Check that the first in
pack = (pub_packet_t*) pub_packet_list_tail(&ctx.inflight)->data;
if (pack->pid != 2) {
printf("Failed pub test 7.2. Wanted pid 2, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
// Ack the next packet out of order, with pid 3 being acked before pid 2.
pub_packet_ack(&sub1, 3, 0);
pub_packet_ack(&sub2, 3, 0);
pub_packet_ack(&sub3, 3, 0);
// Check size of inflight elements for sub2, which should be 1 (pid 3)
if (pub_packet_list_size(&sub2.inflight) != 1) {
printf("Failed pub test 8.1. Wanted size 1, got %d\n",
pub_packet_list_size(&sub2.inflight));
exit(255);
}
// Inspect sub2 to see that its only remaining inflight packet is pid 2
pack = (pub_packet_t*) pub_packet_list_head(&sub2.inflight)->data;
if (pack->pid != 2) {
printf("Failed pub test 8.2. Wanted size 2, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
// Inspect context's inflight packets and ensure that only pid 3 is left.
// Check size of inflight elements for ctx, which should be 1 (pid 3)
if (pub_packet_list_size(&ctx.inflight) != 1) {
printf("Failed pub test 8.3. Wanted size 1, got %d\n",
pub_packet_list_size(&ctx.inflight));
exit(255);
}
// Inspect ctx inflighjt to see that its only remaining inflight
// packet is pid 2
pack = (pub_packet_t*) pub_packet_list_head(&ctx.inflight)->data;
if (pack->pid != 2) {
printf("Failed pub test 8.3. Wanted size 2, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
//
// Ack packet 2, which is the last one
//
pub_packet_ack(&sub1, 2, 0);
pub_packet_ack(&sub2, 2, 0);
pub_packet_ack(&sub3, 2, 0);
// Check size of inflight elements for sub2, which should be 1 (pid 3)
if (pub_packet_list_size(&sub3.inflight) != 0) {
printf("Failed pub test 9.1. Wanted size 0, got %d\n",
pub_packet_list_size(&sub3.inflight));
exit(255);
}
// Inspect context's inflight packets and ensure that only pid 3 is left.
// Check size of inflight elements for ctx, which should be 1 (pid 3)
if (pub_packet_list_size(&ctx.inflight) != 0) {
printf("Failed pub test 9.2. Wanted size 0, got %d\n",
pub_packet_list_size(&ctx.inflight));
exit(255);
}
// Send the rest of the packags.
pack = pub_next_queued_packet(&ctx);
if (pack->pid != 4) {
printf("Failed pub test 10.1. Wanted packet id 4, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pub_packet_sent(&ctx, pack, rmc_usec_monotonic_timestamp());
pack = pub_next_queued_packet(&ctx);
if (pack->pid != 5) {
printf("Failed pub test 10.2. Wanted packet id 5, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pub_packet_sent(&ctx, pack, rmc_usec_monotonic_timestamp());
pack = pub_next_queued_packet(&ctx);
if (pack->pid != 6) {
printf("Failed pub test 10.3. Wanted packet id 6, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pub_packet_sent(&ctx, pack, rmc_usec_monotonic_timestamp());
pack = pub_next_queued_packet(&ctx);
if (pack->pid != 7) {
printf("Failed pub test 10.4. Wanted packet id 7, got %llu\n",
(long long unsigned) pack->pid);
exit(255);
}
pub_packet_sent(&ctx, pack, rmc_usec_monotonic_timestamp());
// Ack all the packages in a semi-random order
pub_packet_ack(&sub1, 5, 0);
pub_packet_ack(&sub2, 5, 0);
pub_packet_ack(&sub3, 5, 0);
pub_packet_ack(&sub1, 7, 0);
pub_packet_ack(&sub2, 7, 0);
pub_packet_ack(&sub3, 7, 0);
pub_packet_ack(&sub1, 6, 0);
pub_packet_ack(&sub2, 6, 0);
pub_packet_ack(&sub3, 6, 0);
pub_packet_ack(&sub1, 4, 0);
pub_packet_ack(&sub2, 4, 0);
pub_packet_ack(&sub3, 4, 0);
// Check that everything is empty
if (pub_packet_list_size(&ctx.inflight) != 0) {
printf("Failed pub test 11.1. Wanted size 0, got %d\n",
pub_packet_list_size(&ctx.inflight));
exit(255);
}
if (pub_packet_list_size(&ctx.queued) != 0) {
printf("Failed pub test 11.2. Wanted size 0, got %d\n",
pub_packet_list_size(&ctx.queued));
exit(255);
}
if (pub_packet_list_size(&sub1.inflight) != 0) {
printf("Failed pub test 11.3. Wanted size 0, got %d\n",
pub_packet_list_size(&sub1.inflight));
exit(255);
}
if (pub_packet_list_size(&sub2.inflight) != 0) {
printf("Failed pub test 11.4. Wanted size 0, got %d\n",
pub_packet_list_size(&sub2.inflight));
exit(255);
}
if (pub_packet_list_size(&sub3.inflight) != 0) {
printf("Failed pub test 11.5. Wanted size 0, got %d\n",
pub_packet_list_size(&sub3.inflight));
exit(255);
}
// Reset next_pid to make testing easier.
ctx.next_pid = 1;
//
// Check that collection of timed out packages works.
//
if ((pid = pub_queue_packet(&ctx, "1", 2, user_data_nil())) != 1) {
printf("Failed pub test 12.1. Wanted pid 1, got %llu\n",
(long long unsigned) pid);
exit(255);
}
// Queue integrity has already been tested. Trust rest of pids to
// be 2-6.
pub_queue_packet(&ctx, "2", 2, user_data_nil());
pub_queue_packet(&ctx, "3", 2, user_data_nil());
pub_queue_packet(&ctx, "4", 2, user_data_nil());
pub_queue_packet(&ctx, "5", 2, user_data_nil());
pub_queue_packet(&ctx, "6", 2, user_data_nil());
// Send each packet in turn, increasing time stamp
// by one for each packet
pack = pub_next_queued_packet(&ctx);
pub_packet_sent(&ctx, pack, 1);
pack = pub_next_queued_packet(&ctx);
pub_packet_sent(&ctx, pack, 2);
pack = pub_next_queued_packet(&ctx);
pub_packet_sent(&ctx, pack, 3);
pack = pub_next_queued_packet(&ctx);
pub_packet_sent(&ctx, pack, 4);
pack = pub_next_queued_packet(&ctx);
pub_packet_sent(&ctx, pack, 5);
pack = pub_next_queued_packet(&ctx);
pub_packet_sent(&ctx, pack, 6);
// All subs will have p1 acked
//
// Inflight pids after ack:
// sub1: - 2 3 4 5 6
// sub2: - 2 3 4 5 6
// sub3: - 2 3 4 5 6
//
pub_packet_ack(&sub1, 1, 0);
pub_packet_ack(&sub2, 1, 0);
pub_packet_ack(&sub3, 1, 0);
// Sub3 will have all packes acked
//
// After:
// sub1: - 2 3 4 5 6
// sub2: - 2 3 4 5 6
// sub3: - - - - - -
//
pub_packet_ack(&sub3, 2, 0);
pub_packet_ack(&sub3, 3, 0);
pub_packet_ack(&sub3, 4, 0);
pub_packet_ack(&sub3, 5, 0);
pub_packet_ack(&sub3, 6, 0);
// Ack p2 and p3 for sub2
//
// After:
// sub1: - 2 3 4 5 6
// sub2: - - - 4 5 6
// sub3: - - - - - -
pub_packet_ack(&sub2, 2, 0);
pub_packet_ack(&sub2, 3, 0);
// Get all packets sent >= 2 usecs ago.
// Expected hits of subscribers:
// sub1 - 2,3,4
// sub2 - 4
pub_sub_list_init(&sub_lst, 0,0,0);
pub_get_timed_out_subscribers(&ctx, 6, 2, &sub_lst);
// sub1 and sub2 should be in the list
if (pub_sub_list_size(&sub_lst) != 2) {
printf("Failed pub test 12.2. Wanted size 2, got %d\n",
pub_sub_list_size(&sub_lst));
exit(255);
}
//
// Check Sub1
//
sptr1 = pub_sub_list_head(&sub_lst)->data;
if (sptr1 == &sub2) {
printf("Failed pub test 12.3. Wanted sub1, got sub2\n");
exit(255);
}
if (sptr1 == &sub3) {
printf("Failed pub test 12.4. Wanted sub1, got sub3\n");
exit(255);
}
if (sptr1 != &sub1) {
printf("Failed pub test 12.5. Wanted sub1, got weird %p\n",
sptr1);
exit(255);
}
//
// Check Sub2
//
sptr2 = pub_sub_list_tail(&sub_lst)->data;
if (sptr2 == &sub3) {
printf("Failed pub test 13.1. Wanted sub2, got sub3\n");
exit(255);
}
if (sptr2 == &sub1) {
printf("Failed pub test 13.2. Wanted sub2, got sub1\n");
exit(255);
}
if (sptr2 != &sub2) {
printf("Failed pub test 13.3 Wanted sub2, got weird %p\n",
sptr1);
exit(255);
}
// Get all packets sent >= 3 usecs ago.
// Expected hits of subscribers:
// sub1 - 2,3
pub_sub_list_empty(&sub_lst);
pub_get_timed_out_subscribers(&ctx, 6, 3, &sub_lst);
// sub1 and sub2 should be in the list
if (pub_sub_list_size(&sub_lst) != 1) {
printf("Failed pub test 14.1. Wanted size 1, got %d\n",
pub_sub_list_size(&sub_lst));
exit(255);
}
//
// Check Sub1
//
sptr1 = pub_sub_list_head(&sub_lst)->data;
if (sptr1 == &sub2) {
printf("Failed pub test 14.2. Wanted sub1, got sub2\n");
exit(255);
}
if (sptr1 == &sub3) {
printf("Failed pub test 14.3. Wanted sub1, got sub3\n");
exit(255);
}
if (sptr1 != &sub1) {
printf("Failed pub test 14.4. Wanted sub1, got weird %p\n",
sptr1);
exit(255);
}
//
// Test oldest inflight packets
//
pub_get_oldest_unackowledged_packet(&ctx, &ts);
if (ts == -1) {
printf("Failed pub test 15.1. Wanted oldest subscriber sent ts. Got nothing\n");
exit(255);
}
// Oldest pid is 2.
//
// sub1: - 2 3 4 5 6
// sub2: - - - 4 5 6
// sub3: - - - - - -
if (ts != 2) {
printf("Failed pub test 15.5. Wanted ts 2, got %llu\n",
(long long unsigned) ts);
exit(255);
}
pub_packet_ack(&sub1, 2, 0);
pub_packet_ack(&sub1, 3, 0);
pub_packet_ack(&sub1, 4, 0);
pub_packet_ack(&sub1, 5, 0);
pub_packet_ack(&sub1, 6, 0);
pub_packet_ack(&sub2, 4, 0);
pub_packet_ack(&sub2, 5, 0);
pub_packet_ack(&sub2, 6, 0);
}