This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
forked from lizan/service-control-client-cxx
-
Notifications
You must be signed in to change notification settings - Fork 10
/
quota_aggregator_impl_test.cc
805 lines (629 loc) · 26.3 KB
/
quota_aggregator_impl_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
// TODO: Insert description here. (generated by jaebong)
#include "src/quota_aggregator_impl.h"
#include <thread>
#include <unordered_map>
#include "gmock/gmock.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/util/message_differencer.h"
#include "gtest/gtest.h"
#include "src/signature.h"
#include "utils/status_test_util.h"
using ::google::api::servicecontrol::v1::AllocateQuotaRequest;
using ::google::api::servicecontrol::v1::AllocateQuotaResponse;
using ::google::api::servicecontrol::v1::QuotaOperation;
using ::google::api::servicecontrol::v1::QuotaOperation_QuotaMode;
using ::google::protobuf::TextFormat;
using ::google::protobuf::util::MessageDifferencer;
using std::string;
namespace google {
namespace service_control_client {
namespace {
const char kServiceName[] = "library.googleapis.com";
const char kServiceConfigId[] = "2016-09-19r0";
const int kFlushIntervalMs = 100;
const int kExpirationMs = 400;
const char kRequest1[] = R"(
service_name: "library.googleapis.com"
allocate_operation {
operation_id: "operation-1"
method_name: "methodname"
consumer_id: "consumerid"
quota_metrics {
metric_name: "metric_first"
metric_values {
int64_value: 1
}
}
quota_metrics {
metric_name: "metric_second"
metric_values {
int64_value: 1
}
}
quota_mode: NORMAL
}
service_config_id: "2016-09-19r0"
)";
const char kRequest2[] = R"(
service_name: "library.googleapis.com"
allocate_operation {
operation_id: "operation-2"
method_name: "methodname2"
consumer_id: "consumerid2"
quota_metrics {
metric_name: "metric_first"
metric_values {
int64_value: 2
}
}
quota_metrics {
metric_name: "metric_second"
metric_values {
int64_value: 3
}
}
quota_mode: NORMAL
}
service_config_id: "2016-09-19r0"
)";
const char kRequest3[] = R"(
service_name: "library.googleapis.com"
allocate_operation {
operation_id: "operation-3"
method_name: "methodname3"
consumer_id: "consumerid3"
quota_metrics {
metric_name: "metric_first"
metric_values {
int64_value: 1
}
}
quota_metrics {
metric_name: "metric_second"
metric_values {
int64_value: 1
}
}
quota_mode: NORMAL
}
service_config_id: "2016-09-19r0"
)";
const char kSuccessResponse1[] = R"(
operation_id: "operation-1"
quota_metrics {
metric_name: "serviceruntime.googleapis.com/api/consumer/quota_used_count"
metric_values {
labels {
key: "/quota_name"
value: "metric_first"
}
int64_value: 1
}
metric_values {
labels {
key: "/quota_name"
value: "metric_first"
}
int64_value: 1
}
}
service_config_id: "2016-09-19r0"
)";
const char kErrorResponse1[] = R"(
operation_id: "a197c6f2-aecc-4a31-9744-b1d5aea4e4b4"
allocate_errors {
code: RESOURCE_EXHAUSTED
subject: "user:integration_test_user"
}
)";
const char kSuccessResponse2[] = R"(
operation_id: "550e8400-e29b-41d4-a716-446655440000"
quota_metrics {
metric_name: "serviceruntime.googleapis.com/api/consumer/quota_used_count"
metric_values {
labels {
key: "/quota_name"
value: "metric_first"
}
int64_value: 1
}
metric_values {
labels {
key: "/quota_name"
value: "metric_second"
}
int64_value: 1
}
}
service_config_id: "2017-02-08r5"
)";
const char kErrorResponse2[] = R"(
operation_id: "a197c6f2-aecc-4a31-9744-b1d5aea4e4b4"
allocate_errors {
code: RESOURCE_EXHAUSTED
subject: "user:integration_test_user"
}
)";
const char kEmptyResponse[] = R"(
)";
const std::set<std::pair<std::string, int>> ExtractMetricSets(
const ::google::api::servicecontrol::v1::QuotaOperation& operation) {
std::set<std::pair<std::string, int>> sets;
for (auto quota_metric : operation.quota_metrics()) {
sets.insert(std::make_pair(quota_metric.metric_name(),
quota_metric.metric_values(0).int64_value()));
}
return sets;
}
} // namespace
class QuotaAggregatorImplTest : public testing::Test {
public:
void SetUp() {
ASSERT_TRUE(TextFormat::ParseFromString(kRequest1, &request1_));
ASSERT_TRUE(TextFormat::ParseFromString(kRequest2, &request2_));
ASSERT_TRUE(TextFormat::ParseFromString(kRequest3, &request3_));
request_signature1_ = GenerateAllocateQuotaRequestSignature(request1_);
request_signature2_ = GenerateAllocateQuotaRequestSignature(request2_);
request_signature3_ = GenerateAllocateQuotaRequestSignature(request3_);
ASSERT_TRUE(
TextFormat::ParseFromString(kSuccessResponse1, &pass_response1_));
ASSERT_TRUE(
TextFormat::ParseFromString(kSuccessResponse2, &pass_response2_));
ASSERT_TRUE(
TextFormat::ParseFromString(kErrorResponse1, &error_response1_));
ASSERT_TRUE(
TextFormat::ParseFromString(kErrorResponse2, &error_response2_));
ASSERT_TRUE(TextFormat::ParseFromString(kEmptyResponse, &empty_response_));
QuotaAggregationOptions options(10, kFlushIntervalMs, kExpirationMs);
aggregator_ =
CreateAllocateQuotaAggregator(kServiceName, kServiceConfigId, options);
ASSERT_TRUE((bool)(aggregator_));
aggregator_->SetFlushCallback(std::bind(
&QuotaAggregatorImplTest::FlushCallback, this, std::placeholders::_1));
}
void FlushCallback(const AllocateQuotaRequest& request) {
flushed_.push_back(request);
}
void FlushCallbackCallingBackToAggregator(
const AllocateQuotaRequest& request) {
flushed_.push_back(request);
(void)aggregator_->CacheResponse(
GenerateAllocateQuotaRequestSignature(request), pass_response1_);
}
AllocateQuotaRequest request1_;
AllocateQuotaRequest request2_;
AllocateQuotaRequest request3_;
string request_signature1_;
string request_signature2_;
string request_signature3_;
AllocateQuotaResponse pass_response1_;
AllocateQuotaResponse pass_response2_;
AllocateQuotaResponse error_response1_;
AllocateQuotaResponse error_response2_;
AllocateQuotaResponse empty_response_;
std::unique_ptr<QuotaAggregator> aggregator_;
std::vector<AllocateQuotaRequest> flushed_;
};
TEST_F(QuotaAggregatorImplTest, TestRequestAndCache) {
AllocateQuotaResponse response;
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
}
TEST_F(QuotaAggregatorImplTest, TestCacheElementStayByAggregate) {
AllocateQuotaResponse response;
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// Use cached result, and add the cost to aggregator
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
// When refresh comes, the cache item has aggregated cost. it is not dropped.
std::this_thread::sleep_for(std::chrono::milliseconds(kFlushIntervalMs + 10));
EXPECT_OK(aggregator_->Flush());
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
}
TEST_F(QuotaAggregatorImplTest, TestCacheElementStayByNegative) {
AllocateQuotaResponse response;
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, error_response1_));
// Use the cached negative result.
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, error_response1_));
// When refresh comes, the cache item is an negative result. it is not dropped
std::this_thread::sleep_for(std::chrono::milliseconds(kFlushIntervalMs + 10));
EXPECT_OK(aggregator_->Flush());
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, error_response1_));
}
TEST_F(QuotaAggregatorImplTest, TestCacheElementStayWithNonAggregated) {
AllocateQuotaResponse response;
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// When refresh comes, the cache item is a non-aggregated positive item.
// It is still kept in the cache.
std::this_thread::sleep_for(std::chrono::milliseconds(kFlushIntervalMs + 10));
EXPECT_OK(aggregator_->Flush());
// Get the cached response.
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
}
TEST_F(QuotaAggregatorImplTest, TestCacheElementDrop) {
AllocateQuotaResponse response;
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
std::this_thread::sleep_for(std::chrono::milliseconds(kExpirationMs + 10));
EXPECT_OK(aggregator_->Flush());
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
}
TEST_F(QuotaAggregatorImplTest, TestNotMatchingServiceName) {
*(request1_.mutable_service_name()) = "some-other-service-name";
AllocateQuotaResponse response;
EXPECT_EQ(absl::StatusCode::kInvalidArgument,
aggregator_->Quota(request1_, &response).code());
}
TEST_F(QuotaAggregatorImplTest, TestNoOperation) {
request1_.clear_allocate_operation();
AllocateQuotaResponse response;
EXPECT_EQ(absl::StatusCode::kInvalidArgument,
aggregator_->Quota(request1_, &response).code());
}
TEST_F(QuotaAggregatorImplTest, TestFlushAggregatedRecord) {
AllocateQuotaResponse response;
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response).code());
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
EXPECT_EQ(flushed_.size(), 1);
// simulate refresh timeout
std::this_thread::sleep_for(std::chrono::milliseconds(110));
EXPECT_OK(aggregator_->Flush());
EXPECT_EQ(flushed_.size(), 2);
}
TEST_F(QuotaAggregatorImplTest, TestFlushedBeforeRefreshTimeout) {
AllocateQuotaResponse response;
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response).code());
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
EXPECT_OK(aggregator_->Flush());
// The request 1 remaining in the cache is not yet flushed.
EXPECT_EQ(flushed_.size(), 1);
}
TEST_F(QuotaAggregatorImplTest, TestInflightCache) {
AllocateQuotaResponse response;
// temporary cached with in_flight flag on
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response).code());
// hit cached response and aggregate
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response).code());
std::this_thread::sleep_for(std::chrono::milliseconds(110));
EXPECT_OK(aggregator_->Flush());
// hit cached response and aggregate
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response).code());
std::this_thread::sleep_for(std::chrono::milliseconds(110));
EXPECT_OK(aggregator_->Flush());
// nothing refreshed
EXPECT_EQ(flushed_.size(), 1);
// update the response in the cache and set in_flight flag off
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// hit cached response and aggregate
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response).code());
std::this_thread::sleep_for(std::chrono::milliseconds(110));
EXPECT_OK(aggregator_->Flush());
// one element added to the refresh list
EXPECT_EQ(flushed_.size(), 2);
}
TEST_F(QuotaAggregatorImplTest, TestCacheAggregateAfterRefreshAndCacheUpdate) {
std::set<std::pair<std::string, int>> quota_metrics;
std::set<std::pair<std::string, int>> expected_costs;
AllocateQuotaResponse response1;
AllocateQuotaResponse response2;
// insert request1
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response1).code());
EXPECT_EQ(flushed_.size(), 1);
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// insert request2
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request2_, &response2).code());
EXPECT_OK(aggregator_->CacheResponse(request_signature2_, pass_response2_));
// aggregate request1
EXPECT_OK(aggregator_->Quota(request1_, &response1));
// aggregate request2
EXPECT_OK(aggregator_->Quota(request2_, &response2));
// verify responses from cache
EXPECT_TRUE(MessageDifferencer::Equals(response1, pass_response1_));
EXPECT_TRUE(MessageDifferencer::Equals(response2, pass_response2_));
// check flushed out list is empty
EXPECT_EQ(flushed_.size(), 2);
std::this_thread::sleep_for(std::chrono::milliseconds(60));
// expire request1
EXPECT_OK(aggregator_->Flush());
EXPECT_EQ(flushed_.size(), 3);
EXPECT_TRUE(flushed_[0].has_allocate_operation());
quota_metrics = ExtractMetricSets(flushed_[0].allocate_operation());
expected_costs = {{"metric_first", 1}, {"metric_second", 1}};
ASSERT_EQ(expected_costs, quota_metrics);
std::this_thread::sleep_for(std::chrono::milliseconds(60));
// expire request2
EXPECT_OK(aggregator_->Flush());
EXPECT_EQ(flushed_.size(), 4);
EXPECT_TRUE(flushed_[1].has_allocate_operation());
expected_costs = {{"metric_first", 2}, {"metric_second", 3}};
quota_metrics = ExtractMetricSets(flushed_[1].allocate_operation());
ASSERT_EQ(quota_metrics, expected_costs);
// reset in_flight flag for request1
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response2_));
// expire temporary elements for refreshment from the cache
std::this_thread::sleep_for(std::chrono::milliseconds(110));
EXPECT_OK(aggregator_->Flush());
EXPECT_EQ(flushed_.size(), 4);
// lookup request should be StatusCode::kOk. Element will not be expired.
AllocateQuotaResponse response3;
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response3).code());
}
TEST_F(QuotaAggregatorImplTest,
TestCacheAggregateAfterRefreshBeforeCacheUpdate) {
std::set<std::pair<std::string, int>> quota_metrics;
std::set<std::pair<std::string, int>> expected_costs;
AllocateQuotaResponse response1;
// insert request1 to cache and removed item
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response1).code());
EXPECT_EQ(flushed_.size(), 1);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
// response has arrived
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
std::this_thread::sleep_for(std::chrono::milliseconds(30));
// aggregated to request1
EXPECT_OK(aggregator_->Quota(request1_, &response1));
EXPECT_EQ(flushed_.size(), 1);
std::this_thread::sleep_for(std::chrono::milliseconds(60));
// expire request1, temporary 1 inserted
EXPECT_OK(aggregator_->Flush());
EXPECT_EQ(flushed_.size(), 2);
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// aggregated to temporary three times
EXPECT_OK(aggregator_->Quota(request1_, &response1));
EXPECT_OK(aggregator_->Quota(request1_, &response1));
EXPECT_OK(aggregator_->Quota(request1_, &response1));
// elapse time to trigger refresh by Quota
std::this_thread::sleep_for(std::chrono::milliseconds(110));
EXPECT_OK(aggregator_->Quota(request1_, &response1));
EXPECT_EQ(flushed_.size(), 3);
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
std::this_thread::sleep_for(std::chrono::milliseconds(110));
// send refresh for expired item
EXPECT_OK(aggregator_->Flush());
// check
EXPECT_EQ(flushed_.size(), 4);
// response has arrived, set in_flight flag off
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// temporary 1 will be flushed
EXPECT_EQ(flushed_.size(), 4);
// check first flushed
EXPECT_TRUE(flushed_[1].has_allocate_operation());
expected_costs = {{"metric_first", 1}, {"metric_second", 1}};
quota_metrics = ExtractMetricSets(flushed_[1].allocate_operation());
ASSERT_EQ(quota_metrics, expected_costs);
// check second flushed
EXPECT_TRUE(flushed_[2].has_allocate_operation());
expected_costs = {{"metric_first", 3}, {"metric_second", 3}};
quota_metrics = ExtractMetricSets(flushed_[2].allocate_operation());
ASSERT_EQ(quota_metrics, expected_costs);
// check second flushed
EXPECT_TRUE(flushed_[3].has_allocate_operation());
expected_costs = {{"metric_first", 1}, {"metric_second", 1}};
quota_metrics = ExtractMetricSets(flushed_[3].allocate_operation());
ASSERT_EQ(quota_metrics, expected_costs);
}
TEST_F(QuotaAggregatorImplTest, TestCacheRefreshOneAggregated) {
std::set<std::pair<std::string, int>> quota_metrics;
std::set<std::pair<std::string, int>> expected_costs;
AllocateQuotaResponse response1;
AllocateQuotaResponse response2;
// insert request 1
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response1).code());
EXPECT_EQ(flushed_.size(), 1);
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// insert request 2
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request2_, &response2).code());
EXPECT_EQ(flushed_.size(), 2);
EXPECT_OK(aggregator_->CacheResponse(request_signature2_, pass_response2_));
// aggregate request 1
EXPECT_OK(aggregator_->Quota(request1_, &response1));
// check responses from cache
EXPECT_TRUE(MessageDifferencer::Equals(response1, pass_response1_));
EXPECT_TRUE(MessageDifferencer::Equals(response2, empty_response_));
std::this_thread::sleep_for(std::chrono::milliseconds(60));
// expire request1
EXPECT_OK(aggregator_->Flush());
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// check request1 has flushed out
EXPECT_EQ(flushed_.size(), 3);
// aggregate request 2
EXPECT_OK(aggregator_->Quota(request2_, &response2));
EXPECT_OK(aggregator_->Quota(request2_, &response2));
std::this_thread::sleep_for(std::chrono::milliseconds(60));
// expire request2
EXPECT_OK(aggregator_->Flush());
EXPECT_OK(aggregator_->CacheResponse(request_signature2_, pass_response2_));
// the second cached item did not have aggregated quota, when it is flushed,
// it was dropped.
EXPECT_EQ(flushed_.size(), 4);
// verify flushed out 1 again
expected_costs = {{"metric_first", 1}, {"metric_second", 1}};
quota_metrics = ExtractMetricSets(flushed_[2].allocate_operation());
ASSERT_EQ(quota_metrics, expected_costs);
// verify flushed out 1 again
expected_costs = {{"metric_first", 4}, {"metric_second", 6}};
quota_metrics = ExtractMetricSets(flushed_[3].allocate_operation());
ASSERT_EQ(quota_metrics, expected_costs);
}
TEST_F(QuotaAggregatorImplTest, TestCacheRefreshQuotaModeBestEffort) {
AllocateQuotaResponse response;
// trigger initial allocation
EXPECT_EQ(absl::StatusCode::kOk,
aggregator_->Quota(request1_, &response).code());
// check refresh
EXPECT_EQ(flushed_.size(), 1);
// cache positive response
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// aggregate
EXPECT_OK(aggregator_->Quota(request1_, &response));
// trigger refresh request by timeout
std::this_thread::sleep_for(std::chrono::milliseconds(60));
EXPECT_OK(aggregator_->Flush());
// check refresh request
EXPECT_EQ(flushed_.size(), 2);
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
EXPECT_EQ(flushed_[0].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
EXPECT_EQ(flushed_[1].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
}
TEST_F(QuotaAggregatorImplTest, TestCacheRefreshQuotaCallForNegative) {
AllocateQuotaResponse response;
// trigger initial allocation
EXPECT_OK(aggregator_->Quota(request1_, &response));
// quota_call count should be 1
EXPECT_EQ(flushed_.size(), 1);
// First cache miss always get empty response.
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
// cache negative response
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, error_response1_));
// Make a second call, should use cached result.
EXPECT_OK(aggregator_->Quota(request1_, &response));
// quota_call count should still be 1 since cached result is used.
EXPECT_EQ(flushed_.size(), 1);
// Get the cached response.
EXPECT_TRUE(MessageDifferencer::Equals(response, error_response1_));
// After the first refresh.
std::this_thread::sleep_for(std::chrono::milliseconds(kFlushIntervalMs + 10));
EXPECT_OK(aggregator_->Flush());
// Negative result caused a CHECK_ONLY call.
// quota_call count should be 2 now
EXPECT_EQ(flushed_.size(), 2);
EXPECT_EQ(flushed_[0].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
EXPECT_EQ(flushed_[1].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_CHECK_ONLY);
// Set CHECK_ONLY call response as positive.
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// Next call should be positive
EXPECT_OK(aggregator_->Quota(request1_, &response));
// Get the cached response.
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
}
TEST_F(QuotaAggregatorImplTest,
TestCacheRefreshQuotaCallForNegativeBestEffort) {
AllocateQuotaResponse response;
// trigger initial allocation
EXPECT_OK(aggregator_->Quota(request1_, &response));
// quota_call count should be 1
EXPECT_EQ(flushed_.size(), 1);
// First cache miss always get empty response.
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
// cache negative response
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, error_response1_));
// The second call has BEST_EFFORT mode, should use cached result.
request1_.mutable_allocate_operation()->set_quota_mode(
QuotaOperation::BEST_EFFORT);
EXPECT_OK(aggregator_->Quota(request1_, &response));
// quota_call count should still be 1 since cached result is used.
EXPECT_EQ(flushed_.size(), 1);
// Get the cached response.
EXPECT_TRUE(MessageDifferencer::Equals(response, error_response1_));
// After the first refresh.
std::this_thread::sleep_for(std::chrono::milliseconds(kFlushIntervalMs + 10));
EXPECT_OK(aggregator_->Flush());
// Negative result caused a CHECK_ONLY call.
// quota_call count should be 2 now
EXPECT_EQ(flushed_.size(), 2);
EXPECT_EQ(flushed_[0].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
EXPECT_EQ(flushed_[1].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
// Set CHECK_ONLY call response as positive.
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// Next call should be positive
EXPECT_OK(aggregator_->Quota(request1_, &response));
// Get the cached response.
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
}
TEST_F(QuotaAggregatorImplTest, TestCacheRefreshQuotaCallForAggregated) {
AllocateQuotaResponse response;
// trigger initial allocation
EXPECT_OK(aggregator_->Quota(request1_, &response));
// quota_call count should be 1
EXPECT_EQ(flushed_.size(), 1);
// First cache miss always get empty response.
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
// cache negative response
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// Make a second call, should use cached result, aggregate the cost.
EXPECT_OK(aggregator_->Quota(request1_, &response));
// quota_call count should still be 1 since cached result is used.
EXPECT_EQ(flushed_.size(), 1);
// Get the cached response.
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
// After the first refresh.
std::this_thread::sleep_for(std::chrono::milliseconds(kFlushIntervalMs + 10));
EXPECT_OK(aggregator_->Flush());
// Aggregated cache item will be send to the server.
// quota_call count should be 2 now
EXPECT_EQ(flushed_.size(), 2);
EXPECT_EQ(flushed_[0].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
EXPECT_EQ(flushed_[1].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
// Set response as positive.
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response2_));
// Next call should be positive
EXPECT_OK(aggregator_->Quota(request1_, &response));
// Get the cached response.
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response2_));
}
TEST_F(QuotaAggregatorImplTest, TestCacheRefreshQuotaCallForNonAggregated) {
AllocateQuotaResponse response;
// trigger initial allocation
EXPECT_OK(aggregator_->Quota(request1_, &response));
// quota_call count should be 1
EXPECT_EQ(flushed_.size(), 1);
// First cache miss always get empty response.
EXPECT_TRUE(MessageDifferencer::Equals(response, empty_response_));
// cache negative response
EXPECT_OK(aggregator_->CacheResponse(request_signature1_, pass_response1_));
// After the first refresh.
std::this_thread::sleep_for(std::chrono::milliseconds(kFlushIntervalMs + 10));
EXPECT_OK(aggregator_->Flush());
// Non-aggregated cache item will be kept in the cache in the refresh.
// quota_call count should still be 1
EXPECT_EQ(flushed_.size(), 1);
EXPECT_EQ(flushed_[0].allocate_operation().quota_mode(),
QuotaOperation_QuotaMode::QuotaOperation_QuotaMode_BEST_EFFORT);
// Next call should be a cache hit
EXPECT_OK(aggregator_->Quota(request1_, &response));
EXPECT_TRUE(MessageDifferencer::Equals(response, pass_response1_));
}
} // namespace service_control_client
} // namespace google