forked from lizan/service-control-client-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribution_helper_test.cc
886 lines (777 loc) · 26 KB
/
distribution_helper_test.cc
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
/* Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "distribution_helper.h"
#include <limits>
#include "gmock/gmock.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/util/message_differencer.h"
#include "gtest/gtest.h"
using ::google::api::servicecontrol::v1::Distribution;
using ::google::protobuf::TextFormat;
using ::google::protobuf::util::MessageDifferencer;
namespace google {
namespace service_control_client {
namespace {
const char kInitExponentialDistribution[] = R"(
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 0.001
}
bucket_counts: 0
bucket_counts: 0
bucket_counts: 0
bucket_counts: 0
)";
const char kInitLinearDistribution[] = R"(
linear_buckets {
num_finite_buckets: 2
width: 2
offset: 1
}
bucket_counts: 0
bucket_counts: 0
bucket_counts: 0
bucket_counts: 0
)";
// Same bucket range as the linear distribution above.
const char kInitExplicitDistribution[] = R"(
explicit_buckets {
bounds: 1.0
bounds: 3.0
bounds: 5.0
}
bucket_counts: 0
bucket_counts: 0
bucket_counts: 0
bucket_counts: 0
)";
const double kOneValueExponential = 0.001;
const char kOneValueExponentialDistribution[] = R"(
count: 1
mean: 0.001
sum_of_squared_deviation: 0
minimum: 0.001
maximum: 0.001
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 0.001
}
bucket_counts: 0
bucket_counts: 1
bucket_counts: 0
bucket_counts: 0
)";
const double kOneValueLinear = 3.5;
const char kOneValueLinearDistribution[] = R"(
count: 1
mean: 3.5
sum_of_squared_deviation: 0
minimum: 3.5
maximum: 3.5
linear_buckets {
num_finite_buckets: 2
width: 2
offset: 1
}
bucket_counts: 0
bucket_counts: 0
bucket_counts: 1
bucket_counts: 0
)";
const double kOneValueExplicit = 7.5;
const char kOneValueExplicitDistribution[] = R"(
count: 1
mean: 7.5
sum_of_squared_deviation: 0
minimum: 7.5
maximum: 7.5
explicit_buckets {
bounds: 1.0
bounds: 3.0
bounds: 5.0
}
bucket_counts: 0
bucket_counts: 0
bucket_counts: 0
bucket_counts: 1
)";
const double kTwoValuesExponential[] = {0.001, 0.002};
const char kTwoValuesExponentialDistribution[] = R"(
count: 2
mean: 0.0015
sum_of_squared_deviation: 5.0e-07
minimum: 0.001
maximum: 0.002
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 0.001
}
bucket_counts: 0
bucket_counts: 1
bucket_counts: 1
bucket_counts: 0
)";
const double kTwoValuesLinear[] = {1.5, 3.5};
const char kTwoValuesLinearDistribution[] = R"(
count: 2
mean: 2.5
sum_of_squared_deviation: 2.0
minimum: 1.5
maximum: 3.5
linear_buckets {
num_finite_buckets: 2
width: 2
offset: 1
}
bucket_counts: 0
bucket_counts: 1
bucket_counts: 1
bucket_counts: 0
)";
const double kTwoValuesExplicit[] = {0.5, 2.5};
const char kTwoValuesExplicitDistribution[] = R"(
count: 2
mean: 1.5
sum_of_squared_deviation: 2.0
minimum: 0.5
maximum: 2.5
explicit_buckets {
bounds: 1.0
bounds: 3.0
bounds: 5.0
}
bucket_counts: 1
bucket_counts: 1
bucket_counts: 0
bucket_counts: 0
)";
const double kMultipleValuesExponential[] = {-1, 0, 0.001, 0.0015,
0.002, 0.0025, 0.004, 0.005};
const char kMultipleValuesExponentialDistribution[] = R"(
count: 8
mean: -0.123
sum_of_squared_deviation: 0.8790225
minimum: -1
maximum: 0.005
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 0.001
}
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
)";
const double kMultipleValuesLinear[] = {-5, 1.36, 0.69, 2.78,
3.456, 4.809, 5.92, 10.23};
const char kMultipleValuesLinearDistribution[] = R"(
count: 8
mean: 3.030625
sum_of_squared_deviation: 136.346313875
minimum: -5
maximum: 10.23
linear_buckets {
num_finite_buckets: 2
width: 2
offset: 1
}
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
)";
const double kMultipleValuesExplicit[] = {-0.5, 0.5, 1.5, 2.5,
3.5, 4.5, 5.5, 6.5};
const char kMultipleValuesExplicitDistribution[] = R"(
count: 8
mean: 3.0
sum_of_squared_deviation: 42.0
minimum: -0.5
maximum: 6.5
explicit_buckets {
bounds: 1.0
bounds: 3.0
bounds: 5.0
}
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
)";
// Special value Nan comparison is not supported by
// google/protobuf/util/message_differencer.h.
// following tests have to be disabled before it is supported.
#define TREAT_NAN_AS_EQUAL_SUPPORT 0
#if TREAT_NAN_AS_EQUAL_SUPPORT
const double kSpecialValues[] = {
+0,
-0,
std::numeric_limits<double>::infinity(),
-std::numeric_limits<double>::infinity(),
std::numeric_limits<double>::quiet_NaN(),
};
const char kSpecialValuesExponentialDistribution[] = R"(
count: 5
mean: nan
sum_of_squared_deviation: nan
minimum: nan
maximum: nan
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 0.001
}
bucket_counts: 4
bucket_counts: 0
bucket_counts: 0
bucket_counts: 1
)";
const char kSpecialValuesLinearDistribution[] = R"(
count: 5
mean: nan
sum_of_squared_deviation: nan
minimum: nan
maximum: nan
linear_buckets {
num_finite_buckets: 2
width: 2
offset: 1
}
bucket_counts: 4
bucket_counts: 0
bucket_counts: 0
bucket_counts: 1
)";
const char kSpecialValuesExplicitDistribution[] = R"(
count: 5
mean: nan
sum_of_squared_deviation: nan
minimum: nan
maximum: nan
explicit_buckets {
bounds: 1.0
bounds: 3.0
bounds: 5.0
}
bucket_counts: 4
bucket_counts: 0
bucket_counts: 0
bucket_counts: 1
)";
#endif // TREAT_NAN_AS_EQUAL_SUPPORT
// Distribution for [-1, 1, 3, 5]
const char kLinearDistribution[] = R"(
count: 4
mean: 2
sum_of_squared_deviation: 20
minimum: -1
maximum: 5
linear_buckets {
num_finite_buckets: 2
width: 2
offset: 0
}
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
)";
// Combined Distribution for [-1, 1, 3, 5] and [-1, 1, 3, 5].
const char kCombinedLinearDistribution[] = R"(
count: 8
mean: 2
sum_of_squared_deviation: 40
minimum: -1
maximum: 5
linear_buckets {
num_finite_buckets: 2
width: 2
offset: 0
}
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
)";
// Distribution for [-1, 1, 3, 5]
const char kExponentialDistribution[] = R"(
count: 4
mean: 2
sum_of_squared_deviation: 20
minimum: -1
maximum: 5
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 1
}
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
)";
// Combined distribution for [-1, 1, 3, 5] and [-1, 1, 3, 5]
const char kCombinedExponentialDistribution[] = R"(
count: 8
mean: 2
sum_of_squared_deviation: 40
minimum: -1
maximum: 5
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 1
}
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
)";
// Distribution for [-1, 1, 3, 5]
const char kExplicitDistribution[] = R"(
count: 4
mean: 2
sum_of_squared_deviation: 20
minimum: -1
maximum: 5
explicit_buckets {
bounds: 0
bounds: 2
bounds: 4
}
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
)";
// Combined Distribution for [-1, 1, 3, 5] and [-1, 1, 3, 5].
const char kCombinedExplicitDistribution[] = R"(
count: 8
mean: 2
sum_of_squared_deviation: 40
minimum: -1
maximum: 5
explicit_buckets {
bounds: 0
bounds: 2
bounds: 4
}
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
)";
class DistributionHelperTest : public ::testing::Test {
protected:
DistributionHelperTest() {
helper_.InitExponential(2 /* num_finite_buckets */, 2 /* growth_factor */,
0.001 /* scale */, &exponential_distribution_);
other_exponential_distribution_ = exponential_distribution_;
helper_.InitLinear(2 /* num_finite_buckets */, 2 /* width */,
1 /* offset */, &linear_distribution_);
helper_.InitExplicit({1.0, 3.0, 5.0}, &explicit_distribution_);
}
DistributionHelper helper_;
Distribution exponential_distribution_;
Distribution linear_distribution_;
Distribution explicit_distribution_;
Distribution other_exponential_distribution_;
};
TEST_F(DistributionHelperTest, InitializeDistribution_Exponential) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kInitExponentialDistribution, &expected));
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, InitializeDistribution_Linear) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(kInitLinearDistribution, &expected));
EXPECT_TRUE(
MessageDifferencer::ApproximatelyEquals(linear_distribution_, expected));
}
TEST_F(DistributionHelperTest, InitializeDistribution_Explicit) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kInitExplicitDistribution, &expected));
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(explicit_distribution_,
expected));
}
TEST_F(DistributionHelperTest, AddSample_OneValue_Exponential) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kOneValueExponentialDistribution, &expected));
helper_.AddSample(kOneValueExponential, &exponential_distribution_);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, AddSample_OneValue_Linear) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kOneValueLinearDistribution, &expected));
helper_.AddSample(kOneValueLinear, &linear_distribution_);
EXPECT_TRUE(
MessageDifferencer::ApproximatelyEquals(linear_distribution_, expected));
}
TEST_F(DistributionHelperTest, AddSample_OneValue_Explicit) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kOneValueExplicitDistribution, &expected));
helper_.AddSample(kOneValueExplicit, &explicit_distribution_);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(explicit_distribution_,
expected));
}
TEST_F(DistributionHelperTest, AddSample_TwoValues_Exponential) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(kTwoValuesExponentialDistribution,
&expected));
for (double value : kTwoValuesExponential) {
helper_.AddSample(value, &exponential_distribution_);
}
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, AddSample_TwoValues_Linear) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kTwoValuesLinearDistribution, &expected));
for (double value : kTwoValuesLinear) {
helper_.AddSample(value, &linear_distribution_);
}
EXPECT_TRUE(
MessageDifferencer::ApproximatelyEquals(linear_distribution_, expected));
}
TEST_F(DistributionHelperTest, AddSample_TwoValues_Explicit) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kTwoValuesExplicitDistribution, &expected));
for (double value : kTwoValuesExplicit) {
helper_.AddSample(value, &explicit_distribution_);
}
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(explicit_distribution_,
expected));
}
TEST_F(DistributionHelperTest, AddSample_MultipleValues_Exponential) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(
kMultipleValuesExponentialDistribution, &expected));
for (double value : kMultipleValuesExponential) {
helper_.AddSample(value, &exponential_distribution_);
}
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, AddSample_MultipleValues_Linear) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(kMultipleValuesLinearDistribution,
&expected));
for (double value : kMultipleValuesLinear) {
helper_.AddSample(value, &linear_distribution_);
}
EXPECT_TRUE(
MessageDifferencer::ApproximatelyEquals(linear_distribution_, expected));
}
TEST_F(DistributionHelperTest, AddSample_MultipleValues_Explicit) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(kMultipleValuesExplicitDistribution,
&expected));
for (double value : kMultipleValuesExplicit) {
helper_.AddSample(value, &explicit_distribution_);
}
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(explicit_distribution_,
expected));
}
#if TREAT_NAN_AS_EQUAL_SUPPORT
TEST_F(DistributionHelperTest, AddSample_SpecialValues_Exponential) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(kSpecialValuesExponentialDistribution,
&expected));
for (double value : kSpecialValues) {
helper_.AddSample(value, &exponential_distribution_);
}
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, AddSample_SpecialValues_Linear) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kSpecialValuesLinearDistribution, &expected));
for (double value : kSpecialValues) {
helper_.AddSample(value, &linear_distribution_);
}
EXPECT_TRUE(
MessageDifferencer::ApproximatelyEquals(linear_distribution_, expected));
}
TEST_F(DistributionHelperTest, AddSample_SpecialValues_Explicit) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(kSpecialValuesExplicitDistribution,
&expected));
for (double value : kSpecialValues) {
helper_.AddSample(value, &explicit_distribution_);
}
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(explicit_distribution_,
expected));
}
TEST_F(DistributionHelperTest, Merge_TwoDistributions_SpecialValues) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(kSpecialValuesExponentialDistribution,
&expected));
int total_values = sizeof(kSpecialValues) / sizeof(double);
for (int i = 0; i < total_values / 2; ++i) {
helper_.AddSample(kSpecialValues[i], &exponential_distribution_);
}
for (int i = total_values / 2; i < total_values; ++i) {
helper_.AddSample(kSpecialValues[i], &other_exponential_distribution_);
}
helper_.Merge(other_exponential_distribution_, &exponential_distribution_);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
#endif // TREAT_NAN_AS_EQUAL_SUPPORT
TEST_F(DistributionHelperTest, AddSample_UnknownDistribution) {
Distribution distribution;
Distribution expected;
helper_.AddSample(1, &distribution);
EXPECT_TRUE(MessageDifferencer::Equals(distribution, expected));
}
// Merge does not care about bucket_option, so we only use exponential bucket
// in tests.
TEST_F(DistributionHelperTest, Merge_EmptyDistributions) {
Distribution empty;
helper_.InitExponential(2, 2, 0.001, &empty);
Distribution other_empty = empty;
helper_.Merge(other_empty, &empty);
EXPECT_EQ(0, empty.count());
}
TEST_F(DistributionHelperTest, Merge_FromEmptyDistribution) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kOneValueExponentialDistribution, &expected));
helper_.AddSample(kOneValueExponential, &exponential_distribution_);
helper_.Merge(other_exponential_distribution_, &exponential_distribution_);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, Merge_ToEmptyDistribution) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kOneValueExponentialDistribution, &expected));
helper_.AddSample(kOneValueExponential, &exponential_distribution_);
helper_.Merge(other_exponential_distribution_, &exponential_distribution_);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, Merge_TwoDistributions) {
Distribution expected;
ASSERT_TRUE(TextFormat::ParseFromString(
kMultipleValuesExponentialDistribution, &expected));
int total_values = sizeof(kMultipleValuesExponential) / sizeof(double);
for (int i = 0; i < total_values / 2; ++i) {
helper_.AddSample(kMultipleValuesExponential[i],
&exponential_distribution_);
}
for (int i = total_values / 2; i < total_values; ++i) {
helper_.AddSample(kMultipleValuesExponential[i],
&other_exponential_distribution_);
}
helper_.Merge(other_exponential_distribution_, &exponential_distribution_);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential_distribution_,
expected));
}
TEST_F(DistributionHelperTest, Merge_BucketMatch_Linear) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kCombinedLinearDistribution, &expected));
Distribution distribution;
ASSERT_TRUE(TextFormat::ParseFromString(kLinearDistribution, &distribution));
Distribution other_distribution = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest,
Merge_BucketNotMatch_Linear_DifferentNumBuckets) {
Distribution distribution;
ASSERT_TRUE(TextFormat::ParseFromString(kLinearDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_linear_buckets()->set_num_finite_buckets(10);
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_Linear_DifferentWidth) {
Distribution distribution;
ASSERT_TRUE(TextFormat::ParseFromString(kLinearDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_linear_buckets()->set_width(10);
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_Linear_DifferentOffset) {
Distribution distribution;
ASSERT_TRUE(TextFormat::ParseFromString(kLinearDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_linear_buckets()->set_offset(10);
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_Explicit) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kCombinedExplicitDistribution, &expected));
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExplicitDistribution, &distribution));
Distribution other_distribution = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest,
Merge_BucketNotMatch_Explicit_DifferentNumBuckets) {
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExplicitDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_explicit_buckets()->clear_bounds();
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_Explicit_DifferentBounds) {
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExplicitDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_explicit_buckets()->set_bounds(0, 1.5);
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketMatch_Exponential) {
Distribution expected;
ASSERT_TRUE(
TextFormat::ParseFromString(kCombinedExponentialDistribution, &expected));
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExponentialDistribution, &distribution));
Distribution other_distribution = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest,
Merge_BucketNotMatch_Exponential_DifferentNumBuckets) {
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExponentialDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_exponential_buckets()->set_num_finite_buckets(10);
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest,
Merge_BucketNotMatch_Exponential_DifferentGrowthFactor) {
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExponentialDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_exponential_buckets()->set_growth_factor(10);
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest,
Merge_BucketNotMatch_Exponential_DifferentScale) {
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExponentialDistribution, &distribution));
Distribution other_distribution = distribution;
other_distribution.mutable_exponential_buckets()->set_scale(10);
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_Linear_Exponential) {
Distribution linear;
ASSERT_TRUE(TextFormat::ParseFromString(kLinearDistribution, &linear));
Distribution exponential;
ASSERT_TRUE(TextFormat::ParseFromString(kTwoValuesExponentialDistribution,
&exponential));
Distribution expected = exponential;
helper_.Merge(linear, &exponential);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(exponential, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_Explicit_Linear) {
Distribution linear;
ASSERT_TRUE(TextFormat::ParseFromString(kLinearDistribution, &linear));
Distribution explicit_distribution;
ASSERT_TRUE(TextFormat::ParseFromString(kTwoValuesExponentialDistribution,
&explicit_distribution));
Distribution expected = linear;
helper_.Merge(explicit_distribution, &linear);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(linear, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_Exponential_Explicit) {
Distribution exponential;
ASSERT_TRUE(TextFormat::ParseFromString(kLinearDistribution, &exponential));
Distribution explicit_distribution;
ASSERT_TRUE(TextFormat::ParseFromString(kTwoValuesExponentialDistribution,
&explicit_distribution));
Distribution expected = explicit_distribution;
helper_.Merge(exponential, &explicit_distribution);
EXPECT_TRUE(
MessageDifferencer::ApproximatelyEquals(explicit_distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_BucketNotMatch_UnknownBucketOptions) {
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExponentialDistribution, &distribution));
Distribution other_distribution = distribution;
// Both distribution have default BUCKET_TYPE_NOT_SET bucket_option.
distribution.clear_linear_buckets();
distribution.clear_exponential_buckets();
distribution.clear_explicit_buckets();
other_distribution.clear_linear_buckets();
other_distribution.clear_exponential_buckets();
other_distribution.clear_explicit_buckets();
// The Merge function should not break in this situation.
Distribution expected = distribution;
helper_.Merge(other_distribution, &distribution);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(distribution, expected));
}
TEST_F(DistributionHelperTest, Merge_DifferentBucketCountsSize) {
Distribution from = exponential_distribution_;
from.mutable_bucket_counts()->Resize(5, 0);
from.set_bucket_counts(1, 10);
Distribution to = exponential_distribution_;
// The merge won't happen because the bucket size are different.
helper_.Merge(from, &to);
EXPECT_TRUE(
MessageDifferencer::ApproximatelyEquals(to, exponential_distribution_));
}
TEST_F(DistributionHelperTest, Merge_ZeroCount) {
Distribution distribution;
ASSERT_TRUE(
TextFormat::ParseFromString(kExponentialDistribution, &distribution));
Distribution to = distribution;
to.set_count(0);
helper_.Merge(distribution, &to);
EXPECT_TRUE(MessageDifferencer::ApproximatelyEquals(to, distribution));
}
} // namespace
} // namespace service_control_client
} // namespace google