-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtest_segment.c
3219 lines (2764 loc) · 119 KB
/
test_segment.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
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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <stddef.h>
#include <stdio.h>
#include "nr_segment_private.h"
#include "nr_segment.h"
#include "nr_span_event.h"
#include "nr_span_event_private.h"
#include "test_segment_helpers.h"
#include "util_memory.h"
#include "util_slab_private.h"
#include "tlib_main.h"
/* A simple list type to affirm that traversing the tree of segments in
* prefix order is correct. This type is for test purposes only.
*/
#define NR_TEST_LIST_CAPACITY 10
typedef struct _nr_test_list_t {
size_t capacity;
int used;
nr_segment_t* elements[NR_TEST_LIST_CAPACITY];
nr_segment_post_iter_t post_callback;
int post_used;
nr_segment_t* post_elements[NR_TEST_LIST_CAPACITY];
} nr_test_list_t;
static void test_iterator_post_callback(nr_segment_t* segment,
nr_test_list_t* list) {
tlib_pass_if_not_null("post iterator must receive a valid segment", segment);
tlib_pass_if_not_null("post iterator must receive a valid userdata", list);
list->post_elements[list->post_used] = segment;
list->post_used += 1;
}
static nr_segment_iter_return_t test_iterator_callback(nr_segment_t* segment,
void* userdata) {
nr_test_list_t* list;
if (nrunlikely(NULL == segment || NULL == userdata)) {
return NR_SEGMENT_NO_POST_ITERATION_CALLBACK;
}
list = (nr_test_list_t*)userdata;
list->elements[list->used] = segment;
list->used = list->used + 1;
if (list->post_callback) {
return ((nr_segment_iter_return_t){.post_callback = list->post_callback,
.userdata = userdata});
}
return NR_SEGMENT_NO_POST_ITERATION_CALLBACK;
}
static int test_segment_priority_comparator(const void* ptr1,
const void* ptr2,
void* userdata NRUNUSED) {
const nr_segment_t* seg1 = (const nr_segment_t*)ptr1;
const nr_segment_t* seg2 = (const nr_segment_t*)ptr2;
if (seg1->priority > seg2->priority) {
return 1;
} else if (seg1->priority < seg2->priority) {
return -1;
} else {
return 0;
}
}
static void test_segment_new_txn_with_segment_root(void) {
nrtxn_t* txn = new_txn(0);
/*
* Test : Normal operation. When a new transaction is started, affirm
* that it has all the necessary initialization for maintaining
* a tree of segments.
*/
tlib_pass_if_not_null("A new transaction must have a segment root",
txn->segment_root);
tlib_pass_if_size_t_equal(
"A new transaction's segment root must initialise its children", 0,
nr_segment_children_size(&txn->segment_root->children));
tlib_pass_if_size_t_equal("A new transaction must have a segment count of 0",
txn->segment_count, 0);
tlib_pass_if_ptr_equal(
"A new transaction's current parent must be initialized to its segment "
"root",
txn->segment_root, nr_txn_get_current_segment(txn, NULL));
tlib_pass_if_true(
"A new transaction's segment root must have its start time initialized",
0 != txn->abs_start_time, "Expected true");
/* Force the call to nr_txn_end() to be successful */
txn->status.path_is_frozen = 1;
nr_txn_end(txn);
tlib_pass_if_true(
"An ended transaction's segment root must have its stop time initialized",
0 != txn->segment_root->stop_time, "Expected true");
tlib_pass_if_str_equal(
"An ended transaction's segment root must have the same name as the root "
"node",
txn->name, nr_string_get(txn->trace_strings, txn->segment_root->name));
nr_txn_destroy(&txn);
}
static void test_segment_start(void) {
nr_segment_t* s = NULL;
nr_segment_t* t = NULL;
nr_segment_t* u = NULL;
nr_segment_t* prev_parent = NULL;
/* Use the helper function to leverage nr_txn_begin(), install a segment_root
* in the transaction and set a start time */
nrtxn_t* txn = new_txn(0);
txn->status.recording = 1;
tlib_pass_if_size_t_equal("A root segment is created when the txn is started",
1, nr_vector_size(&txn->default_parent_stack));
/*
* Test : Bad parameters.
*/
s = nr_segment_start(NULL, NULL, NULL);
tlib_pass_if_null("Starting a segment on a NULL txn must not succeed", s);
tlib_pass_if_size_t_equal("Only root segment should be allocated", 1,
nr_txn_allocated_segment_count(txn));
tlib_pass_if_size_t_equal("a segment should NOT be added to the parent stack",
1, nr_vector_size(&txn->default_parent_stack));
/*
* Test : Normal operation.
*
* Starting a segment with a NULL, or implicit, parent. The parenting
* information for the segment must be supplied by the parent_stack on the
* transaction. Let's start and end three segments and make sure that the
* family of segments is well-formed. Affirm that the parent, child, and
* sibling relationships are all as expected.
*/
/* Start a segment and affirm that it is well-formed */
s = nr_segment_start(txn, NULL, NULL);
tlib_pass_if_not_null("Starting a segment on a valid txn must succeed", s);
tlib_pass_if_size_t_equal("There should be 2 segments", 2,
nr_vector_size(&txn->default_parent_stack));
tlib_pass_if_ptr_equal(
"The most-recently started segment must be the transaction's current "
"segment",
nr_txn_get_current_segment(txn, NULL), s);
tlib_pass_if_not_null(
"Starting a segment on a valid txn must allocate space for children",
&s->children);
tlib_pass_if_uint64_t_equal("A started segment has default color WHITE",
s->color, NR_SEGMENT_WHITE);
tlib_pass_if_uint64_t_equal("A started segment has default type CUSTOM",
s->type, NR_SEGMENT_CUSTOM);
tlib_pass_if_ptr_equal("A started segment must save its transaction", s->txn,
txn);
tlib_fail_if_uint64_t_equal("A started segment has an initialized start time",
s->start_time, 0);
tlib_pass_if_null("A started segment has a NULL hash for user attributes",
s->attributes);
tlib_pass_if_ptr_equal(
"A segment started with an implicit parent must have the transaction's"
" segment_root as parent",
s->parent, txn->segment_root);
tlib_pass_if_size_t_equal("Two segments were allocated", 2,
nr_txn_allocated_segment_count(txn));
/* Start and end a second segment, t */
prev_parent = nr_txn_get_current_segment(txn, NULL);
t = nr_segment_start(txn, NULL, NULL);
tlib_pass_if_size_t_equal("There should be 3 segments", 3,
nr_vector_size(&txn->default_parent_stack));
tlib_pass_if_not_null("Starting a segment on a valid txn must succeed", t);
tlib_pass_if_ptr_equal(
"The most recently started segment must be the transaction's current "
"segment",
nr_txn_get_current_segment(txn, NULL), t);
tlib_pass_if_ptr_equal(
"A segment started with an implicit parent must have the previously "
"current segment as parent",
t->parent, prev_parent);
tlib_pass_if_true("Ending a well-formed segment must succeed",
test_segment_end_and_keep(&t), "Expected true");
tlib_pass_if_size_t_equal("The segment should be retired", 2,
nr_vector_size(&txn->default_parent_stack));
tlib_pass_if_ptr_equal(
"The most recently started segment has ended; the current segment must "
"be its parent",
nr_txn_get_current_segment(txn, NULL), s);
/* Start a third segment. Its sibling should be the second segment, t */
prev_parent = nr_txn_get_current_segment(txn, NULL);
u = nr_segment_start(txn, NULL, NULL);
tlib_pass_if_not_null("Starting a segment on a valid txn must succeed", u);
tlib_pass_if_size_t_equal("4 started 1 ended", 3,
nr_vector_size(&txn->default_parent_stack));
tlib_pass_if_ptr_equal(
"The most recently started segment must be the transaction's current "
"segment",
nr_txn_get_current_segment(txn, NULL), u);
tlib_pass_if_ptr_equal(
"A segment started with an implicit parent must have the previously "
"current segment as parent",
u->parent, prev_parent);
tlib_pass_if_ptr_equal(
"A segment started with a NULL parent must have the expected "
"previous siblings",
nr_segment_children_get_prev(&s->children, u), t);
tlib_pass_if_null(
"A segment started with a NULL parent must have the expected "
"next siblings",
nr_segment_children_get_next(&s->children, u));
tlib_pass_if_true("Ending a well-formed segment must succeed",
nr_segment_end(&u), "Expected true");
tlib_pass_if_size_t_equal("a fourth segment was allocated", 4,
nr_txn_allocated_segment_count(txn));
tlib_pass_if_size_t_equal("4 started 2 ended", 2,
nr_vector_size(&txn->default_parent_stack));
tlib_pass_if_size_t_equal(
"The slab should be empty, we haven't discarded yet", 0,
nr_vector_size(&txn->segment_slab->free_list));
/* Remove them from the stack. */
tlib_pass_if_true("good night matriarch", nr_segment_discard(&s),
"Expected true");
tlib_pass_if_size_t_equal("The slab should have 1 item", 1,
nr_vector_size(&txn->segment_slab->free_list));
nr_segment_discard(&t);
tlib_pass_if_size_t_equal("The slab should have 2 item", 2,
nr_vector_size(&txn->segment_slab->free_list));
/* Clean up */
nr_txn_destroy(&txn);
}
static void test_segment_start_async(void) {
nr_segment_t* mother;
nr_segment_t* first_born;
nr_segment_t* third_born;
nr_segment_t* first_stepchild;
nr_segment_t* first_grandchild;
nr_segment_t* great_grandchild;
/* Use the helper function to leverage nr_txn_begin(), install a segment_root
* in the transaction and set a start time */
nrtxn_t* txn = new_txn(0);
txn->status.recording = 1;
/* Build out a small tree of segments to test upon */
mother = nr_segment_start(txn, NULL, NULL);
first_born = nr_segment_start(txn, NULL, NULL);
/*
* Test : Bad parameters.
*/
tlib_pass_if_null("Starting a segment on a NULL txn must not succeed",
nr_segment_start(NULL, mother, "async_context"));
/*
* Test : Async operation. Starting a segment with an explicit parent,
* supplied as a parameter to nr_segment_start() has the expected
* impact on parent and sibling relationships.
*/
first_stepchild = nr_segment_start(txn, mother, "async_context");
tlib_pass_if_not_null(
"Starting a segment on a valid txn and an explicit parent must succeed",
first_stepchild);
tlib_pass_if_ptr_equal(
"The most recently started, explicitly-parented segment must not alter "
"the NULL context's current segment",
nr_txn_get_current_segment(txn, NULL), first_born);
tlib_pass_if_not_null(
"Starting a segment on a valid txn must allocate space for children",
&first_stepchild->children);
tlib_pass_if_uint64_t_equal("A started segment has default type CUSTOM",
first_stepchild->type, NR_SEGMENT_CUSTOM);
tlib_pass_if_ptr_equal("A started segment must save its transaction",
first_stepchild->txn, txn);
tlib_fail_if_uint64_t_equal("A started segment has an initialized start time",
first_stepchild->start_time, 0);
tlib_pass_if_null("A started segment has a NULL hash for user attributes",
first_stepchild->attributes);
tlib_pass_if_int_equal(
"A started segment has an initialized async context",
first_stepchild->async_context,
nr_string_find(first_stepchild->txn->trace_strings, "async_context"));
tlib_pass_if_ptr_equal(
"A segment started with an explicit parent must have the explicit "
"parent",
first_stepchild->parent, mother);
tlib_pass_if_ptr_equal(
"A segment started with an explicit parent must have the explicit "
"previous siblings",
nr_segment_children_get_prev(&(mother->children), first_stepchild),
first_born);
/*
* Test : Async operation. Starting a segment with no parent and a new
* context supplied as a parameter to nr_segment_start() has the
* expected impact on parent and sibling relationships.
*/
first_grandchild = nr_segment_start(txn, NULL, "another_async");
tlib_pass_if_not_null(
"Starting a segment on a valid txn and an implicit parent must succeed",
first_grandchild);
tlib_pass_if_ptr_equal(
"The most recently started, implicitly-parented segment must not alter "
"the NULL context's current segment",
nr_txn_get_current_segment(txn, NULL), first_born);
tlib_pass_if_ptr_equal(
"The most recently started, implicitly-parented segment must set the "
"current segment for the new context",
nr_txn_get_current_segment(txn, "another_async"), first_grandchild);
tlib_pass_if_uint64_t_equal("A started segment has default type CUSTOM",
first_grandchild->type, NR_SEGMENT_CUSTOM);
tlib_pass_if_ptr_equal("A started segment must save its transaction",
first_grandchild->txn, txn);
tlib_fail_if_uint64_t_equal("A started segment has an initialized start time",
first_grandchild->start_time, 0);
tlib_pass_if_null("A started segment has a NULL hash for user attributes",
first_grandchild->attributes);
tlib_pass_if_int_equal(
"A started segment has an initialized async context",
first_grandchild->async_context,
nr_string_find(first_grandchild->txn->trace_strings, "another_async"));
tlib_pass_if_ptr_equal(
"A segment started with an implicit parent must have the implied parent "
"on the main context",
first_grandchild->parent, first_born);
tlib_pass_if_ptr_equal(
"A segment started with an implicit parent must be a child of that "
"parent",
nr_segment_children_get(&first_born->children, 0), first_grandchild);
/*
* Test : Async operation. Starting a segment with no parent on the same
* context as first_grandchild should make it a child of that segment.
*/
great_grandchild = nr_segment_start(txn, NULL, "another_async");
tlib_pass_if_not_null(
"Starting a segment on a valid txn and an implicit parent must succeed",
great_grandchild);
tlib_pass_if_ptr_equal(
"The most recently started, implicitly-parented segment must not alter "
"the NULL context's current segment",
nr_txn_get_current_segment(txn, NULL), first_born);
tlib_pass_if_ptr_equal(
"The most recently started, implicitly-parented segment must set the "
"current segment for the new context",
nr_txn_get_current_segment(txn, "another_async"), great_grandchild);
tlib_pass_if_uint64_t_equal("A started segment has default type CUSTOM",
great_grandchild->type, NR_SEGMENT_CUSTOM);
tlib_pass_if_ptr_equal("A started segment must save its transaction",
great_grandchild->txn, txn);
tlib_fail_if_uint64_t_equal("A started segment has an initialized start time",
great_grandchild->start_time, 0);
tlib_pass_if_null("A started segment has a NULL hash for user attributes",
great_grandchild->attributes);
tlib_pass_if_int_equal(
"A started segment has an initialized async context",
great_grandchild->async_context,
nr_string_find(great_grandchild->txn->trace_strings, "another_async"));
tlib_pass_if_ptr_equal(
"A segment started with an implicit parent must have the implied parent "
"on the same async context",
great_grandchild->parent, first_grandchild);
tlib_pass_if_ptr_equal(
"A segment started with an implicit parent must be a child of that "
"parent",
nr_segment_children_get(&first_grandchild->children, 0),
great_grandchild);
/*
* Test : Async operation. Starting a segment with an explicit parent,
* supplied as a parameter to nr_segment_start() has the expected
* impact on subsequent sibling relationships.
*/
nr_segment_end(&first_born);
third_born = nr_segment_start(txn, NULL, NULL);
tlib_pass_if_ptr_equal(
"A segment started with an explicit parent must have the explicit "
"next siblings",
nr_segment_children_get_next(&(mother->children), first_stepchild),
third_born);
/* Clean up */
nr_txn_destroy(&txn);
}
static void test_set_name(void) {
nrtxn_t txnv = {0};
nr_segment_t segment
= {.type = NR_SEGMENT_CUSTOM, .txn = &txnv, .parent = NULL};
/* Mock up transaction */
txnv.status.recording = 1;
txnv.trace_strings = nr_string_pool_create();
/*
* Test : Bad parameters.
*/
tlib_pass_if_false("Setting a name on a NULL segment must not succeed",
nr_segment_set_name(NULL, "name"), "Expected false");
tlib_pass_if_false("Setting a NULL name on a segment must not succeed",
nr_segment_set_name(&segment, NULL), "Expected false");
/*
* Test : Normal operation.
*/
tlib_pass_if_true("Setting a name on a segment must succeed",
nr_segment_set_name(&segment, "name"), "Expected true");
tlib_pass_if_int_equal("A named segment has the expected name", segment.name,
nr_string_find(segment.txn->trace_strings, "name"));
/* Clean up */
nr_string_pool_destroy(&txnv.trace_strings);
}
static void test_add_child(void) {
nr_segment_t mother = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
nr_segment_t segment = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
/*
* Test : Bad parameters.
*/
tlib_pass_if_false("Adding a NULL child to a parent must not succeed",
nr_segment_add_child(&mother, NULL), "Expected false");
tlib_pass_if_false("Adding a child to a NULL parent must not succeed",
nr_segment_add_child(NULL, &segment), "Expected false");
}
static void test_add_metric(void) {
nr_segment_t segment
= {.type = NR_SEGMENT_CUSTOM, .parent = NULL, .metrics = NULL};
nr_vector_t* vec;
/*
* Test : Bad parameters.
*/
tlib_pass_if_bool_equal("Adding a metric to a NULL segment must not succeed",
false,
nr_segment_add_metric(NULL, "Dead Disco", false));
tlib_pass_if_bool_equal(
"Adding a NULL metric name to a segment must not succeed", false,
nr_segment_add_metric(&segment, NULL, false));
/*
* Test : Normal operation.
*/
tlib_pass_if_bool_equal(
"Adding a scoped metric to a segment must succeed", true,
nr_segment_add_metric(&segment, "Help I'm Alive", true));
tlib_pass_if_not_null(
"Adding a metric to a segment without an initialised segment vector must "
"create a vector to store the segments",
segment.metrics);
tlib_pass_if_size_t_equal("Adding a metric to a segment must save the metric",
1, nr_vector_size(segment.metrics));
tlib_pass_if_str_equal(
"Adding a metric to a segment must save the name", "Help I'm Alive",
((nr_segment_metric_t*)nr_vector_get(segment.metrics, 0))->name);
tlib_pass_if_bool_equal(
"Adding a metric to a segment must save the scoping flag", true,
((nr_segment_metric_t*)nr_vector_get(segment.metrics, 0))->scoped);
vec = segment.metrics;
tlib_pass_if_bool_equal(
"Adding an unscoped metric to a segment must succeed", true,
nr_segment_add_metric(&segment, "Gimme Sympathy", false));
tlib_pass_if_ptr_equal(
"Adding a metric to a segment with an initialised segment vector must "
"use the same vector",
vec, segment.metrics);
tlib_pass_if_size_t_equal("Adding a metric to a segment must save the metric",
2, nr_vector_size(segment.metrics));
tlib_pass_if_str_equal(
"Adding a metric to a segment must save the name", "Gimme Sympathy",
((nr_segment_metric_t*)nr_vector_get(segment.metrics, 1))->name);
tlib_pass_if_bool_equal(
"Adding a metric to a segment must save the scoping flag", false,
((nr_segment_metric_t*)nr_vector_get(segment.metrics, 1))->scoped);
nr_vector_destroy(&segment.metrics);
}
static void test_set_parent_to_same(void) {
nr_segment_t mother = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
/*
* Test : Bad parameters.
*/
tlib_pass_if_false("Setting a parent on a NULL segment must not succeed",
nr_segment_set_parent(NULL, &mother), "Expected false");
/*
* Test : Normal operation.
*/
tlib_pass_if_true(
"Setting a well-formed segment with the same parent must succeed",
nr_segment_set_parent(&mother, NULL), "Expected true");
tlib_pass_if_null(
"Setting a well-formed segment with a NULL parent means the segment must "
"have a NULL parent",
mother.parent);
}
static void test_set_null_parent(void) {
nr_segment_t thing_one = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
nr_segment_t thing_two = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
nr_segment_t mother = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
nr_segment_t segment = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
/* Build mock segments, each with an array of children */
nr_segment_children_init(&mother.children);
nr_segment_add_child(&mother, &thing_one);
nr_segment_children_init(&segment.children);
nr_segment_add_child(&segment, &thing_two);
tlib_pass_if_ptr_equal("Affirm my nuclear family is well-formed",
thing_one.parent, &mother);
tlib_pass_if_ptr_equal("Affirm my nuclear family is well-formed",
thing_two.parent, &segment);
/*
* Test : Normal operation. Reparent a segment with a NULL parent.
*/
tlib_pass_if_true(
"Setting a well-formed segment with a new parent must succeed",
nr_segment_set_parent(&segment, &mother), "Expected true");
tlib_pass_if_ptr_equal(
"Setting a well-formed segment with a new parent means the segment must "
"have that new parent",
segment.parent, &mother);
tlib_pass_if_ptr_equal(
"Setting a well-formed segment with a new parent means the segment must "
"have expected prev siblings",
nr_segment_children_get_prev(&mother.children, &segment), &thing_one);
tlib_pass_if_null(
"Setting a well-formed segment with a new parent means the segment must "
"have expected next siblings",
nr_segment_children_get_next(&mother.children, &segment));
/* Clean up */
nr_segment_children_deinit(&mother.children);
nr_segment_destroy_fields(&mother);
nr_segment_children_deinit(&segment.children);
nr_segment_destroy_fields(&segment);
}
static void test_set_non_null_parent(void) {
nr_segment_t thing_one = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
nr_segment_t thing_two = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
nr_segment_t mother = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
nr_segment_t segment = {.type = NR_SEGMENT_CUSTOM, .parent = NULL};
/* Build mock segments, each with an array of children */
nr_segment_children_init(&segment.children);
nr_segment_add_child(&segment, &thing_two);
nr_segment_children_init(&mother.children);
nr_segment_add_child(&mother, &thing_one);
nr_segment_add_child(&mother, &segment);
tlib_pass_if_ptr_equal("Affirm my nuclear family is well-formed",
thing_one.parent, &mother);
tlib_pass_if_ptr_equal("Affirm my nuclear family is well-formed",
segment.parent, &mother);
/*
* Test : Normal operation. Re-parent a segment with a non-NULL parent.
*/
tlib_pass_if_true(
"Setting a well-formed segment with a new parent must succeed",
nr_segment_set_parent(&thing_one, &segment), "Expected true");
tlib_pass_if_ptr_equal(
"Setting a well-formed segment with a new parent means the segment must "
"have that new parent",
thing_one.parent, &segment);
tlib_pass_if_ptr_equal(
"Setting a well-formed segment with a new parent means the segment must "
"have expected prev siblings",
nr_segment_children_get_prev(&segment.children, &thing_one), &thing_two);
tlib_pass_if_null(
"Setting a well-formed segment with a new parent means the segment must "
"have expected next siblings",
nr_segment_children_get_next(&segment.children, &thing_one));
tlib_pass_if_ptr_equal(
"Setting a well-formed segment with a new parent means the old parent "
"must "
"have a new first child",
nr_segment_children_get(&mother.children, 0), &segment);
tlib_fail_if_ptr_equal(
"Setting a well-formed segment with a new parent means the segment must "
"not be a child of its old parent",
nr_segment_children_get(&mother.children, 0), &thing_one);
/* Clean up */
nr_segment_children_deinit(&mother.children);
nr_segment_destroy_fields(&mother);
nr_segment_children_deinit(&segment.children);
nr_segment_destroy_fields(&segment);
}
static void test_set_parent_different_txn(void) {
nrtxn_t txn_one, txn_two;
nr_segment_t thing_one = {.type = NR_SEGMENT_CUSTOM, .txn = &txn_one};
nr_segment_t thing_two = {.type = NR_SEGMENT_CUSTOM, .txn = &txn_two};
tlib_pass_if_bool_equal(
"A segment cannot be parented to a segment on a different transaction",
false, nr_segment_set_parent(&thing_one, &thing_two));
tlib_pass_if_bool_equal(
"A segment cannot be parented to a segment on a different transaction",
false, nr_segment_set_parent(&thing_two, &thing_one));
tlib_pass_if_ptr_equal(
"A failed nr_segment_set_parent() call must not change the parent",
&txn_one, thing_one.txn);
tlib_pass_if_ptr_equal(
"A failed nr_segment_set_parent() call must not change the parent",
&txn_two, thing_two.txn);
}
static void test_set_timing(void) {
nr_segment_t segment = {.start_time = 1234, .stop_time = 5678};
/*
* Test : Bad parameters.
*/
tlib_pass_if_false("Setting timing on a NULL segment must not succeed",
nr_segment_set_timing(NULL, 0, 0), "Expected false");
/*
* Test : Normal operation.
*/
tlib_pass_if_true("Setting timing on a well-formed segment must succeed",
nr_segment_set_timing(&segment, 2000, 5), "Expected false");
tlib_pass_if_true(
"Setting timing on a well-formed segment must alter the start time",
2000 == segment.start_time, "Expected true");
tlib_pass_if_true(
"Setting timing on a well-formed segment must alter the stop time",
2005 == segment.stop_time, "Expected true");
}
static void test_end_segment(void) {
nrtxn_t txnv = {.segment_count = 0, .parent_stacks = nr_hashmap_create(NULL)};
nr_segment_t S = {.start_time = 1234, .stop_time = 0, .txn = &txnv};
nr_segment_t T = {.start_time = 1234, .stop_time = 5678, .txn = &txnv};
nr_segment_t U = {.txn = 0};
nr_segment_t* s = &S;
nr_segment_t* t = &T;
nr_segment_t* u = &U;
/* Mock up the parent stacks used by the txn */
nr_stack_t parent_stack;
nr_stack_init(&parent_stack, 32);
nr_hashmap_index_set(txnv.parent_stacks, 0, (void*)&parent_stack);
/*
* Test : Bad parameters.
*/
tlib_pass_if_false("Ending a NULL segment must not succeed",
nr_segment_end(NULL), "Expected false");
tlib_pass_if_false("Ending a segment with a NULL txn must not succeed",
nr_segment_end(&u), "Expected false");
tlib_pass_if_true(
"Ending ill-formed segments must not alter the transaction's segment "
"count",
0 == txnv.segment_count, "Expected true");
/*
* Test : Normal operation. Ending a segment with
* stop_time = 0.
*/
tlib_pass_if_true("Ending a well-formed segment must succeed",
nr_segment_end(&s), "Expected true");
tlib_pass_if_true(
"Ending a well-formed segment with a zero-value stop "
"time must alter the stop time",
0 != T.stop_time, "Expected true");
tlib_pass_if_true(
"Ending a well-formed segment must increment the "
"transaction's segment count by 1",
1 == txnv.segment_count, "Expected true");
/*
* Test : Normal operation. Ending a segment with stop_time != 0.
*/
tlib_pass_if_true("Ending a well-formed segment must succeed",
nr_segment_end(&t), "Expected true");
tlib_pass_if_true(
"Ending a well-formed segment with a non-zero stop "
"time must not alter the stop time",
5678 == T.stop_time, "Expected true");
tlib_pass_if_true(
"Ending a well-formed segment must increment the transaction's segment "
"count by 1",
2 == txnv.segment_count, "Expected true");
/* Clean up */
nr_hashmap_destroy(&txnv.parent_stacks);
nr_stack_destroy_fields(&parent_stack);
}
static void test_end_segment_async(void) {
nr_segment_t* aa;
nr_segment_t* ab;
nr_segment_t* ba;
nr_segment_t* bb;
nrtxn_t* txn = new_txn(0);
txn->status.recording = 1;
/*
* Test : Ending a segment on an async context should only affect that stack.
*/
aa = nr_segment_start(txn, NULL, "a");
tlib_pass_if_ptr_equal(
"Segment aa should have the transaction's segment root as its parent",
txn->segment_root, aa->parent);
tlib_pass_if_size_t_equal(
"Context a should have exactly one element in its parent stack", 1,
nr_vector_size((nr_stack_t*)nr_hashmap_index_get(
txn->parent_stacks, (uint64_t)aa->async_context)));
tlib_pass_if_ptr_equal(
"Context a should have aa as the only element in its parent stack", aa,
nr_txn_get_current_segment(txn, "a"));
tlib_pass_if_ptr_equal(
"The main context should have the transaction's segment root as its "
"current segment",
txn->segment_root, nr_txn_get_current_segment(txn, NULL));
ab = nr_segment_start(txn, NULL, "a");
tlib_pass_if_ptr_equal("Segment ab should have aa as its parent", aa,
ab->parent);
tlib_pass_if_size_t_equal(
"Context a should have exactly two elements in its parent stack", 2,
nr_vector_size((nr_stack_t*)nr_hashmap_index_get(
txn->parent_stacks, (uint64_t)aa->async_context)));
tlib_pass_if_ptr_equal(
"Context a should have ab as the current element in its parent stack", ab,
nr_txn_get_current_segment(txn, "a"));
tlib_pass_if_ptr_equal(
"The main context should have the transaction's segment root as its "
"current segment",
txn->segment_root, nr_txn_get_current_segment(txn, NULL));
nr_segment_end(&ab);
tlib_pass_if_size_t_equal(
"Context a should have exactly one element in its parent stack", 1,
nr_vector_size((nr_stack_t*)nr_hashmap_index_get(
txn->parent_stacks, (uint64_t)aa->async_context)));
tlib_pass_if_ptr_equal(
"Context a should have aa as the only element in its parent stack", aa,
nr_txn_get_current_segment(txn, "a"));
tlib_pass_if_ptr_equal(
"The main context should have the transaction's segment root as its "
"current segment",
txn->segment_root, nr_txn_get_current_segment(txn, NULL));
/*
* Test : As above, but when the parent segment is ended first, only the child
* should remain in the stack.
*/
ba = nr_segment_start(txn, NULL, "b");
tlib_pass_if_ptr_equal(
"Segment ba should have the transaction's segment root as its parent",
txn->segment_root, ba->parent);
tlib_pass_if_size_t_equal(
"Context b should have exactly one element in its parent stack", 1,
nr_vector_size((nr_stack_t*)nr_hashmap_index_get(
txn->parent_stacks, (uint64_t)ba->async_context)));
tlib_pass_if_ptr_equal(
"Context b should have ba as the only element in its parent stack", ba,
nr_txn_get_current_segment(txn, "b"));
tlib_pass_if_ptr_equal(
"The main context should have the transaction's segment root as its "
"current segment",
txn->segment_root, nr_txn_get_current_segment(txn, NULL));
bb = nr_segment_start(txn, NULL, "b");
tlib_pass_if_ptr_equal("Segment bb should have ba as its parent", ba,
bb->parent);
tlib_pass_if_size_t_equal(
"Context b should have exactly two elements in its parent stack", 2,
nr_vector_size((nr_stack_t*)nr_hashmap_index_get(
txn->parent_stacks, (uint64_t)bb->async_context)));
tlib_pass_if_ptr_equal(
"Context b should have bb as the current element in its parent stack", bb,
nr_txn_get_current_segment(txn, "b"));
tlib_pass_if_ptr_equal(
"The main context should have the transaction's segment root as its "
"current segment",
txn->segment_root, nr_txn_get_current_segment(txn, NULL));
nr_segment_end(&ba);
tlib_pass_if_size_t_equal(
"Context b should have exactly one element in its parent stack", 1,
nr_vector_size((nr_stack_t*)nr_hashmap_index_get(
txn->parent_stacks, (uint64_t)bb->async_context)));
tlib_pass_if_ptr_equal(
"Context b should have bb as the only element in its parent stack", bb,
nr_txn_get_current_segment(txn, "b"));
tlib_pass_if_ptr_equal(
"The main context should have the transaction's segment root as its "
"current segment",
txn->segment_root, nr_txn_get_current_segment(txn, NULL));
nr_txn_destroy(&txn);
}
static void test_segment_iterate_nulls(void) {
nr_segment_t segment = {.type = NR_SEGMENT_CUSTOM, .name = 1};
nr_test_list_t list = {.capacity = NR_TEST_LIST_CAPACITY, .used = 0};
/*
* Test : Bad parameters.
*/
nr_segment_iterate(NULL, (nr_segment_iter_t)test_iterator_callback, &list);
nr_segment_iterate(&segment, (nr_segment_iter_t)NULL, &list);
nr_segment_iterate(&segment, (nr_segment_iter_t)test_iterator_callback, NULL);
tlib_pass_if_int_equal(
"Traversing with bad parameters must result in an empty list", list.used,
0);
}
static void test_segment_iterate_bachelor(void) {
nr_segment_t bachelor_1 = {.type = NR_SEGMENT_CUSTOM, .name = 1};
nr_segment_t bachelor_2 = {.type = NR_SEGMENT_CUSTOM, .name = 2};
nr_test_list_t list_1 = {.capacity = NR_TEST_LIST_CAPACITY, .used = 0};
nr_test_list_t list_2 = {.capacity = NR_TEST_LIST_CAPACITY, .used = 0};
/* Bachelor 1 has no room for children; Bachelor 2 does. Each
* bachelor needs be regarded as a leaf node.
*/
nr_segment_children_init(&bachelor_2.children);
/*
* Test : Normal operation. Traversing a tree of 1.
*/
nr_segment_iterate(&bachelor_1, (nr_segment_iter_t)test_iterator_callback,
&list_1);
tlib_pass_if_int_equal(
"Traversing a tree of one node must create a one-node list",
list_1.elements[0]->name, bachelor_1.name);
/*
* Test : Normal operation. Traversing a tree of 1, where
* the node has allocated room for children.
*/
nr_segment_iterate(&bachelor_2, (nr_segment_iter_t)test_iterator_callback,
&list_2);
tlib_pass_if_int_equal(
"Traversing a tree of one node must create a one-node list",
list_2.elements[0]->name, bachelor_2.name);
/* Clean up */
nr_segment_children_deinit(&bachelor_2.children);
}
static void test_segment_iterate(void) {
int i;
nr_test_list_t list = {.capacity = NR_TEST_LIST_CAPACITY, .used = 0};
/* Declare eight segments; give them .name values in pre-order */
nr_segment_t grandmother = {.type = NR_SEGMENT_CUSTOM, .name = 0};
nr_segment_t grown_child_1 = {.type = NR_SEGMENT_CUSTOM, .name = 1};
nr_segment_t grown_child_2 = {.type = NR_SEGMENT_CUSTOM, .name = 3};
nr_segment_t grown_child_3 = {.type = NR_SEGMENT_CUSTOM, .name = 7};
nr_segment_t child_1 = {.type = NR_SEGMENT_CUSTOM, .name = 2};
nr_segment_t child_2 = {.type = NR_SEGMENT_CUSTOM, .name = 4};
nr_segment_t child_3 = {.type = NR_SEGMENT_CUSTOM, .name = 5};
nr_segment_t child_4 = {.type = NR_SEGMENT_CUSTOM, .name = 6};
/* Build a mock tree of segments */
nr_segment_children_init(&grandmother.children);
nr_segment_add_child(&grandmother, &grown_child_1);
nr_segment_add_child(&grandmother, &grown_child_2);
nr_segment_add_child(&grandmother, &grown_child_3);
nr_segment_children_init(&grown_child_1.children);
nr_segment_add_child(&grown_child_1, &child_1);
nr_segment_children_init(&grown_child_2.children);
nr_segment_add_child(&grown_child_2, &child_2);
nr_segment_add_child(&grown_child_2, &child_3);
nr_segment_add_child(&grown_child_2, &child_4);
/*
* The mock tree looks like this:
*
* --------(0)grandmother---------
* / | \
* (1)grown_child_1 (3)grown_child_2 (7)grown_child_3
* / / | \
* (2)child_1 (4)child_2 (5)child_3 (6)child_4
*
*