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
/
service_control_client_impl_test.cc
1137 lines (991 loc) · 45.6 KB
/
service_control_client_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
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 2017 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 <vector>
#include "gmock/gmock.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/util/message_differencer.h"
#include "gtest/gtest.h"
#include "include/service_control_client.h"
#include "src/mock_transport.h"
#include "src/service_control_client_factory_impl.h"
#include "utils/status_test_util.h"
#include "utils/thread.h"
using ::google::api::servicecontrol::v1::CheckRequest;
using ::google::api::servicecontrol::v1::CheckResponse;
using ::google::api::servicecontrol::v1::Operation;
using ::google::api::servicecontrol::v1::ReportRequest;
using ::google::api::servicecontrol::v1::ReportResponse;
using ::google::protobuf::TextFormat;
using ::google::protobuf::util::MessageDifferencer;
using std::string;
using ::absl::UnknownError;
using ::testing::_;
using ::testing::Invoke;
using ::testing::Mock;
namespace google {
namespace service_control_client {
namespace {
const char kServiceName[] = "library.googleapis.com";
const char kServiceConfigId[] = "2016-09-19r0";
const char kCheckRequest1[] = R"(
service_name: "library.googleapis.com"
service_config_id: "2016-09-19r0"
operation {
consumer_id: "project:some-consumer"
start_time {
seconds: 1000
nanos: 2000
}
operation_id: "operation-1"
operation_name: "check-quota"
metric_value_sets {
metric_name: "serviceruntime.googleapis.com/api/consumer/quota_used_count"
metric_values {
labels {
key: "/quota_group_name"
value: "ReadGroup"
}
int64_value: 1000
}
}
}
)";
const char kSuccessCheckResponse1[] = R"(
operation_id: "operation-1"
)";
const char kErrorCheckResponse1[] = R"(
operation_id: "operation-1"
check_errors {
code: PERMISSION_DENIED
detail: "permission denied"
}
)";
const char kCheckRequest2[] = R"(
service_name: "library.googleapis.com"
service_config_id: "2016-09-19r0"
operation {
consumer_id: "project:some-consumer"
operation_id: "operation-2"
operation_name: "check-quota-2"
start_time {
seconds: 1000
nanos: 2000
}
metric_value_sets {
metric_name: "serviceruntime.googleapis.com/api/consumer/quota_used_count"
metric_values {
labels {
key: "/quota_group_name"
value: "ReadGroup"
}
int64_value: 2000
}
}
}
)";
const char kSuccessCheckResponse2[] = R"(
operation_id: "operation-2"
)";
const char kErrorCheckResponse2[] = R"(
operation_id: "operation-2"
check_errors {
code: PERMISSION_DENIED
detail: "permission denied"
}
)";
const char kReportRequest1[] = R"(
service_name: "library.googleapis.com"
service_config_id: "2016-09-19r0"
operations: {
operation_id: "operation-1"
consumer_id: "project:some-consumer"
start_time {
seconds: 1000
nanos: 2000
}
end_time {
seconds: 3000
nanos: 4000
}
log_entries {
timestamp {
seconds: 700
nanos: 600
}
severity: INFO
name: "system_event"
text_payload: "Sample text log message 0"
}
metric_value_sets {
metric_name: "library.googleapis.com/rpc/client/count"
metric_values {
start_time {
seconds: 100
}
end_time {
seconds: 300
}
int64_value: 1000
}
}
}
)";
const char kReportRequest2[] = R"(
service_name: "library.googleapis.com"
service_config_id: "2016-09-19r0"
operations: {
operation_id: "operation-2"
consumer_id: "project:some-consumer"
start_time {
seconds: 1000
nanos: 2000
}
end_time {
seconds: 3000
nanos: 4000
}
log_entries {
timestamp {
seconds: 700
nanos: 600
}
severity: INFO
name: "system_event"
text_payload: "Sample text log message 1"
}
metric_value_sets {
metric_name: "library.googleapis.com/rpc/client/count"
metric_values {
start_time {
seconds: 200
}
end_time {
seconds: 400
}
int64_value: 2000
}
}
}
)";
// Result of Merging request 1 into request 2, assuming they have delta metrics.
const char kReportDeltaMerged12[] = R"(
service_name: "library.googleapis.com"
service_config_id: "2016-09-19r0"
operations: {
operation_id: "operation-1"
consumer_id: "project:some-consumer"
start_time {
seconds: 1000
nanos: 2000
}
end_time {
seconds: 3000
nanos: 4000
}
metric_value_sets {
metric_name: "library.googleapis.com/rpc/client/count"
metric_values {
start_time {
seconds: 100
}
end_time {
seconds: 400
}
int64_value: 3000
}
}
log_entries {
severity: INFO
timestamp {
seconds: 700
nanos: 600
}
text_payload: "Sample text log message 0"
name: "system_event"
}
log_entries {
severity: INFO
timestamp {
seconds: 700
nanos: 600
}
text_payload: "Sample text log message 1"
name: "system_event"
}
}
)";
} // namespace
class ServiceControlClientImplTest : public ::testing::Test {
public:
void SetUp() {
ASSERT_TRUE(TextFormat::ParseFromString(kCheckRequest1, &check_request1_));
ASSERT_TRUE(TextFormat::ParseFromString(kSuccessCheckResponse1,
&pass_check_response1_));
ASSERT_TRUE(TextFormat::ParseFromString(kErrorCheckResponse1,
&error_check_response1_));
ASSERT_TRUE(TextFormat::ParseFromString(kCheckRequest2, &check_request2_));
ASSERT_TRUE(TextFormat::ParseFromString(kSuccessCheckResponse2,
&pass_check_response2_));
ASSERT_TRUE(TextFormat::ParseFromString(kErrorCheckResponse2,
&error_check_response2_));
ASSERT_TRUE(
TextFormat::ParseFromString(kReportRequest1, &report_request1_));
ASSERT_TRUE(
TextFormat::ParseFromString(kReportRequest2, &report_request2_));
ASSERT_TRUE(TextFormat::ParseFromString(kReportDeltaMerged12,
&merged_report_request_));
ServiceControlClientOptions options(
CheckAggregationOptions(1 /*entries */, 500 /* refresh_interval_ms */,
1000 /* expiration_ms */),
QuotaAggregationOptions(1 /*entries */, 500 /* refresh_interval_ms */),
ReportAggregationOptions(1 /* entries */, 500 /*flush_interval_ms*/));
options.check_transport = mock_check_transport_.GetFunc();
options.report_transport = mock_report_transport_.GetFunc();
ServiceControlClientFactoryImpl factory;
client_ = factory.CreateClient(kServiceName, kServiceConfigId, options);
}
// Tests non cached check request. Mocked transport::Check() is storing
// on_done() callback and call it in a delayed fashion within the same thread.
// 1) Call a Client::Check(), the request is not in the cache.
// 2) Transport::Check() is called. Mocked transport::Check() stores
// the on_done callback.
// 3) Client::Check() returns. Client::on_check_done() is not called yet.
// 4) Transport::on_done() is called in the same thread.
// 5) Client::on_check_done() is called.
void InternalTestNonCachedCheckWithStoredCallback(
const CheckRequest& request, absl::Status transport_status,
CheckResponse* transport_response) {
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithStoredCallback));
// Set the check response.
mock_check_transport_.check_response_ = transport_response;
size_t saved_done_vector_size =
mock_check_transport_.on_done_vector_.size();
CheckResponse check_response;
absl::Status done_status = absl::UnknownError("");
client_->Check(
request, &check_response,
[&done_status](absl::Status status) { done_status = status; });
// on_check_done is not called yet. waiting for transport one_check_done.
EXPECT_EQ(done_status, absl::UnknownError(""));
// Since it is not cached, transport should be called.
EXPECT_EQ(mock_check_transport_.on_done_vector_.size(),
saved_done_vector_size + 1);
EXPECT_TRUE(MessageDifferencer::Equals(mock_check_transport_.check_request_,
request));
// Calls the on_check_done() to send status.
mock_check_transport_.on_done_vector_[saved_done_vector_size](
transport_status);
// on_check_done is called with right status.
EXPECT_EQ(done_status, transport_status);
if (done_status.ok()) {
EXPECT_TRUE(
MessageDifferencer::Equals(*transport_response, check_response));
}
// Verifies call expections and clear it before other test.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_check_transport_));
}
// Tests non cached check request. Mocked transport::Check() is called
// right away (in place).
// 1) Call a Client::Check(), the request is not in the cache.
// 2) Transport::Check() is called. on_done callback is called inside
// Transport::Check().
void InternalTestNonCachedCheckWithInplaceCallback(
const CheckRequest& request, absl::Status transport_status,
CheckResponse* transport_response) {
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithInplaceCallback));
// Set the check status and response to be used in the on_check_done
mock_check_transport_.done_status_ = transport_status;
mock_check_transport_.check_response_ = transport_response;
CheckResponse check_response;
absl::Status done_status = absl::UnknownError("");
client_->Check(
request, &check_response,
[&done_status](absl::Status status) { done_status = status; });
// on_check_done should be called.
EXPECT_EQ(done_status, transport_status);
EXPECT_TRUE(MessageDifferencer::Equals(mock_check_transport_.check_request_,
request));
if (transport_status.ok()) {
EXPECT_TRUE(
MessageDifferencer::Equals(*transport_response, check_response));
}
// Verifies call expections and clear it before other test.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_check_transport_));
}
// Before this call, cache should have request1. This test will call Check
// with request2, and it calls Transport::Check() and get a good
// response2 and set it to cache. This will evict the request1. The
// evicted request1 will be called Transport::Check() again, and its response
// is dropped. The cache will have request2.
void InternalTestReplacedGoodCheckWithStoredCallback(
const CheckRequest& request2, absl::Status transport_status2,
CheckResponse* transport_response2, const CheckRequest& request1,
absl::Status transport_status1, CheckResponse* transport_response1) {
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithStoredCallback));
// Set the check response.
mock_check_transport_.check_response_ = transport_response2;
size_t saved_done_vector_size =
mock_check_transport_.on_done_vector_.size();
CheckResponse check_response2;
absl::Status done_status2 = absl::UnknownError("");
client_->Check(
request2, &check_response2,
[&done_status2](absl::Status status) { done_status2 = status; });
// on_check_done is not called yet. waiting for transport one_check_done.
EXPECT_EQ(done_status2, absl::UnknownError(""));
// Since it is not cached, transport should be called.
EXPECT_EQ(mock_check_transport_.on_done_vector_.size(),
saved_done_vector_size + 1);
EXPECT_TRUE(MessageDifferencer::Equals(mock_check_transport_.check_request_,
request2));
// Verifies call expections and clear it before other test.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_check_transport_));
// Once on_done_ is called, it will call CacheResponse
// which evicts out the old item. The evicted item will call
// Transport::Check.
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithStoredCallback));
// Set the check response for the next request
mock_check_transport_.check_response_ = transport_response1;
// Calls the on_check_done() to send status.
mock_check_transport_.on_done_vector_[saved_done_vector_size](
transport_status2);
// on_check_done is called with right status.
EXPECT_EQ(done_status2, transport_status2);
EXPECT_TRUE(
MessageDifferencer::Equals(*transport_response2, check_response2));
// request1 should be evited out, and called Transport.
EXPECT_EQ(mock_check_transport_.on_done_vector_.size(),
saved_done_vector_size + 2);
EXPECT_TRUE(MessageDifferencer::Equals(mock_check_transport_.check_request_,
request1));
// Calls the on_check_done() to send status.
mock_check_transport_.on_done_vector_[saved_done_vector_size + 1](
transport_status1);
// Verifies call expections and clear it before other test.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_check_transport_));
}
// Before this call, cache should have request1. This test will call Check
// with request2, and it calls Transport::Check() and get a good
// response2 and set it to cache. This will evict the request1. The
// evicted request1 will be called Transport::Check() again, and its response
// is dropped. The cache will have request2.
void InternalTestReplacedGoodCheckWithInplaceCallback(
const CheckRequest& request2, absl::Status transport_status2,
CheckResponse* transport_response2) {
// Transport::Check() will be called twice. First one is for request2
// The second one is for evicted request1.
ON_CALL(mock_check_transport_, Check(_, _, _))
.WillByDefault(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithInplaceCallback));
EXPECT_CALL(mock_check_transport_, Check(_, _, _)).Times(2);
// Both requests will use the same status and response.
mock_check_transport_.done_status_ = transport_status2;
mock_check_transport_.check_response_ = transport_response2;
CheckResponse check_response;
absl::Status done_status = absl::UnknownError("");
client_->Check(
request2, &check_response,
[&done_status](absl::Status status) { done_status = status; });
EXPECT_EQ(transport_status2, done_status);
if (transport_status2.ok()) {
EXPECT_TRUE(
MessageDifferencer::Equals(*transport_response2, check_response));
}
// Verifies call expections and clear it before other test.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_check_transport_));
}
// Tests a cached check request.
// 1) Calls a Client::Check(), its request is in the cache.
// 2) Client::on_check_done() is called right away.
// 3) Transport::Check() is not called.
void InternalTestCachedCheck(const CheckRequest& request,
const CheckResponse& expected_response) {
// Check should not be called with cached entry
EXPECT_CALL(mock_check_transport_, Check(_, _, _)).Times(0);
CheckResponse cached_response;
absl::Status cached_done_status = absl::UnknownError("");
client_->Check(request, &cached_response,
[&cached_done_status](absl::Status status) {
cached_done_status = status;
});
// on_check_done is called inplace with a cached entry.
EXPECT_OK(cached_done_status);
EXPECT_TRUE(MessageDifferencer::Equals(expected_response, cached_response));
// Verifies call expections and clear it before other test.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_check_transport_));
}
// Adds a label to the given operation.
void AddLabel(const string& key, const string& value, Operation* operation) {
(*operation->mutable_labels())[key] = value;
}
CheckRequest check_request1_;
CheckResponse pass_check_response1_;
CheckResponse error_check_response1_;
CheckRequest check_request2_;
CheckResponse pass_check_response2_;
CheckResponse error_check_response2_;
ReportRequest report_request1_;
ReportRequest report_request2_;
ReportRequest merged_report_request_;
MockCheckTransport mock_check_transport_;
MockReportTransport mock_report_transport_;
std::unique_ptr<ServiceControlClient> client_;
};
TEST_F(ServiceControlClientImplTest, TestNonCachedCheckWithStoredCallback) {
// Calls a Client::Check, the request is not in the cache
// Transport::Check() is called. It will send a successful check response
// The response should be stored in the cache.
// Client::Check is called with the same check request. It should use the one
// in the cache. Such call did not change the cache state, it can be called
// repeatly.
InternalTestNonCachedCheckWithStoredCallback(
check_request1_, absl::OkStatus(), &pass_check_response1_);
// For a cached request, it can be called repeatedly.
for (int i = 0; i < 10; i++) {
InternalTestCachedCheck(check_request1_, pass_check_response1_);
}
Statistics stat;
absl::Status stat_status = client_->GetStatistics(&stat);
EXPECT_EQ(stat_status, absl::OkStatus());
EXPECT_EQ(stat.total_called_checks, 11);
EXPECT_EQ(stat.send_checks_by_flush, 0);
EXPECT_EQ(stat.send_checks_in_flight, 1);
EXPECT_EQ(stat.send_report_operations, 0);
// There is a cached check request in the cache. When client is destroyed,
// it will call Transport Check.
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithInplaceCallback));
}
TEST_F(ServiceControlClientImplTest, TestReplacedGoodCheckWithStoredCallback) {
// Send request1 and a pass response to cache,
// then replace it with request2. request1 will be evited, it will be send
// to server again.
InternalTestNonCachedCheckWithStoredCallback(
check_request1_, absl::OkStatus(), &pass_check_response1_);
InternalTestCachedCheck(check_request1_, pass_check_response1_);
Statistics stat;
absl::Status stat_status = client_->GetStatistics(&stat);
EXPECT_EQ(stat_status, absl::OkStatus());
EXPECT_EQ(stat.total_called_checks, 2);
EXPECT_EQ(stat.send_checks_by_flush, 0);
EXPECT_EQ(stat.send_checks_in_flight, 1);
EXPECT_EQ(stat.send_report_operations, 0);
InternalTestReplacedGoodCheckWithStoredCallback(
check_request2_, absl::OkStatus(), &pass_check_response2_,
check_request1_, absl::OkStatus(), &pass_check_response1_);
InternalTestCachedCheck(check_request2_, pass_check_response2_);
stat_status = client_->GetStatistics(&stat);
EXPECT_EQ(stat_status, absl::OkStatus());
EXPECT_EQ(stat.total_called_checks, 4);
EXPECT_EQ(stat.send_checks_by_flush, 1);
EXPECT_EQ(stat.send_checks_in_flight, 2);
EXPECT_EQ(stat.send_report_operations, 0);
// There is a cached check request in the cache. When client is destroyed,
// it will call Transport Check.
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithInplaceCallback));
}
TEST_F(ServiceControlClientImplTest, TestReplacedBadCheckWithStoredCallback) {
// Send request1 and a error response to cache,
// then replace it with request2. request1 will be evited. Since it only
// has an error response, it will not need to sent to server
InternalTestNonCachedCheckWithStoredCallback(
check_request1_, absl::OkStatus(), &error_check_response1_);
InternalTestCachedCheck(check_request1_, error_check_response1_);
InternalTestNonCachedCheckWithStoredCallback(
check_request2_, absl::OkStatus(), &error_check_response2_);
InternalTestCachedCheck(check_request2_, error_check_response2_);
}
TEST_F(ServiceControlClientImplTest,
TestFailedNonCachedCheckWithStoredCallback) {
// Calls a Client::Check, the request is not in the cache
// Transport::Check() is called, but it failed with PERMISSION_DENIED error.
// The response is not cached.
// Such call did not change cache state, it can be called repeatly.
// For a failed Check calls, it can be called repeatly.
for (int i = 0; i < 10; i++) {
InternalTestNonCachedCheckWithStoredCallback(
check_request1_, absl::Status(absl::StatusCode::kPermissionDenied, ""),
&pass_check_response1_);
}
}
TEST_F(ServiceControlClientImplTest,
TestNonCachedCheckWithStoredCallbackWithPerRequestTransport) {
MockCheckTransport stack_mock_check_transport;
EXPECT_CALL(stack_mock_check_transport, Check(_, _, _))
.WillOnce(Invoke(&stack_mock_check_transport,
&MockCheckTransport::CheckWithStoredCallback));
stack_mock_check_transport.check_response_ = &pass_check_response1_;
CheckResponse check_response;
absl::Status done_status = absl::UnknownError("");
client_->Check(
check_request1_, &check_response,
[&done_status](absl::Status status) { done_status = status; },
stack_mock_check_transport.GetFunc());
// on_check_done is not called yet. waiting for transport one_check_done.
EXPECT_EQ(done_status, absl::UnknownError(""));
// Since it is not cached, transport should be called.
EXPECT_EQ(stack_mock_check_transport.on_done_vector_.size(), 1);
EXPECT_TRUE(MessageDifferencer::Equals(
stack_mock_check_transport.check_request_, check_request1_));
// Calls the on_check_done() to send status.
stack_mock_check_transport.on_done_vector_[0](absl::OkStatus());
// on_check_done is called with right status.
EXPECT_TRUE(done_status.ok());
EXPECT_TRUE(
MessageDifferencer::Equals(check_response, pass_check_response1_));
// Verifies call expections and clear it before other test.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&stack_mock_check_transport));
// For a cached request, it can be called repeatedly.
for (int i = 0; i < 10; i++) {
InternalTestCachedCheck(check_request1_, pass_check_response1_);
}
// There is a cached check request in the cache. When client is destroyed,
// it will call Transport Check.
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithInplaceCallback));
}
TEST_F(ServiceControlClientImplTest, TestNonCachedCheckWithInplaceCallback) {
// Calls a Client::Check, the request is not in the cache
// Transport::Check() is called. It will send a successful check response
// The response should be stored in the cache.
// Client::Check is called with the same check request. It should use the one
// in the cache. Such call did not change the cache state, it can be called
// repeatly.
InternalTestNonCachedCheckWithInplaceCallback(
check_request1_, absl::OkStatus(), &pass_check_response1_);
// For a cached request, it can be called repeatly.
for (int i = 0; i < 10; i++) {
InternalTestCachedCheck(check_request1_, pass_check_response1_);
}
// There is a cached check request in the cache. When client is destroyed,
// it will call Transport Check.
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithInplaceCallback));
}
TEST_F(ServiceControlClientImplTest, TestReplacedGoodCheckWithInplaceCallback) {
// Send request1 and a pass response to cache,
// then replace it with request2. request1 will be evited, it will be send
// to server again.
InternalTestNonCachedCheckWithInplaceCallback(
check_request1_, absl::OkStatus(), &pass_check_response1_);
InternalTestCachedCheck(check_request1_, pass_check_response1_);
InternalTestReplacedGoodCheckWithInplaceCallback(
check_request2_, absl::OkStatus(), &pass_check_response2_);
InternalTestCachedCheck(check_request2_, pass_check_response2_);
// There is a cached check request in the cache. When client is destroyed,
// it will call Transport Check.
EXPECT_CALL(mock_check_transport_, Check(_, _, _))
.WillOnce(Invoke(&mock_check_transport_,
&MockCheckTransport::CheckWithInplaceCallback));
}
TEST_F(ServiceControlClientImplTest, TestReplacedBadCheckWithInplaceCallback) {
// Send request1 and a error response to cache,
// then replace it with request2. request1 will be evited. Since it only
// has an error response, it will not need to sent to server
InternalTestNonCachedCheckWithInplaceCallback(
check_request1_, absl::OkStatus(), &error_check_response1_);
InternalTestCachedCheck(check_request1_, error_check_response1_);
InternalTestNonCachedCheckWithInplaceCallback(
check_request2_, absl::OkStatus(), &error_check_response2_);
InternalTestCachedCheck(check_request2_, error_check_response2_);
}
TEST_F(ServiceControlClientImplTest,
TestFailedNonCachedCheckWithInplaceCallback) {
// Calls a Client::Check, the request is not in the cache
// Transport::Check() is called, but it failed with PERMISSION_DENIED error.
// The response is not cached.
// Such call did not change cache state, it can be called repeatly.
// For a failed Check calls, it can be called repeatly.
for (int i = 0; i < 10; i++) {
InternalTestNonCachedCheckWithInplaceCallback(
check_request1_, absl::Status(absl::StatusCode::kPermissionDenied, ""),
&pass_check_response1_);
}
}
TEST_F(ServiceControlClientImplTest, TestCachedReportWithStoredCallback) {
// Calls Client::Report() with request1, it should be cached.
// Calls Client::Report() with request2, it should be cached.
// Transport::Report() should not be called.
// After client is destroyed, Transport::Report() should be called
// to send a merged_request.
ReportResponse report_response;
absl::Status done_status1 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request1_, &report_response,
[&done_status1](absl::Status status) { done_status1 = status; });
EXPECT_OK(done_status1);
absl::Status done_status2 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request2_, &report_response,
[&done_status2](absl::Status status) { done_status2 = status; });
EXPECT_OK(done_status2);
// Verifies that mock_report_transport_::Report() is NOT called.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithStoredCallback));
// Only after client is destroyed, mock_report_transport_::Report() is called.
client_.reset();
EXPECT_TRUE(mock_report_transport_.on_done_vector_.size() == 1);
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
merged_report_request_));
// Call the on_check_done() to complete the data flow.
mock_report_transport_.on_done_vector_[0](absl::OkStatus());
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
}
TEST_F(ServiceControlClientImplTest, TestCachedReportWithInplaceCallback) {
// Calls Client::Report() with request1, it should be cached.
// Calls Client::Report() with request2, it should be cached.
// Transport::Report() should not be called.
// After client destroyed, Transport::Report() should be called
// to send a merged_request.
ReportResponse report_response;
absl::Status done_status1 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request1_, &report_response,
[&done_status1](absl::Status status) { done_status1 = status; });
EXPECT_OK(done_status1);
absl::Status done_status2 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request2_, &report_response,
[&done_status2](absl::Status status) { done_status2 = status; });
EXPECT_OK(done_status2);
// Verifies that mock_report_transport_::Report() is NOT called.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithInplaceCallback));
// Only after client destroyed, mock_report_transport_::Report() is called.
client_.reset();
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
merged_report_request_));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
}
TEST_F(ServiceControlClientImplTest, TestReplacedReportWithStoredCallback) {
// Calls Client::Report() with request1, it should be cached.
// Calls Client::Report() with request2 with different labels,
// It should be cached with a new key. Since cache size is 1, reqeust1
// should be cleared./ Transport::Report() should be called for request1.
// After client destroyed, Transport::Report() should be called
// to send request2.
EXPECT_CALL(mock_report_transport_, Report(_, _, _)).Times(0);
ReportResponse report_response;
absl::Status done_status1 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request1_, &report_response,
[&done_status1](absl::Status status) { done_status1 = status; });
EXPECT_OK(done_status1);
// Verifies that mock_report_transport_::Report() is NOT called.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
// request2_ has different operation signature. Constrained by capacity 1,
// request1 will be evicted from cache.
AddLabel("key1", "value1", report_request2_.mutable_operations(0));
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithStoredCallback));
absl::Status done_status2 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request2_, &report_response,
[&done_status2](absl::Status status) { done_status2 = status; });
EXPECT_OK(done_status2);
EXPECT_TRUE(mock_report_transport_.on_done_vector_.size() == 1);
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
report_request1_));
mock_report_transport_.on_done_vector_[0](absl::OkStatus());
// Verifies that mock_report_transport_::Report() is NOT called.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithStoredCallback));
// Only after client destroyed, mock_report_transport_::Report() is called.
client_.reset();
EXPECT_TRUE(mock_report_transport_.on_done_vector_.size() == 2);
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
report_request2_));
// Call the on_check_done() to complete the data flow.
mock_report_transport_.on_done_vector_[1](absl::OkStatus());
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
}
TEST_F(ServiceControlClientImplTest, TestReplacedReportWithInplaceCallback) {
// Calls Client::Report() with request1, it should be cached.
// Calls Client::Report() with request2 with different labels,
// It should be cached with a new key. Since cache size is 1, reqeust1
// should be cleared./ Transport::Report() should be called for request1.
// After client destroyed, Transport::Report() should be called
// to send request2.
EXPECT_CALL(mock_report_transport_, Report(_, _, _)).Times(0);
ReportResponse report_response;
absl::Status done_status1 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request1_, &report_response,
[&done_status1](absl::Status status) { done_status1 = status; });
EXPECT_OK(done_status1);
// Verifies that mock_report_transport_::Report() is NOT called.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
// request2_ has different operation signature. Constrained by capacity 1,
// request1 will be evicted from cache.
AddLabel("key1", "value1", report_request2_.mutable_operations(0));
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithInplaceCallback));
absl::Status done_status2 = absl::UnknownError("");
// this report should be cached, one_done() should be called right away
client_->Report(
report_request2_, &report_response,
[&done_status2](absl::Status status) { done_status2 = status; });
EXPECT_OK(done_status2);
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
report_request1_));
// Verifies that mock_report_transport_::Report() is NOT called.
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithInplaceCallback));
// Only after client destroyed, mock_report_transport_::Report() is called.
client_.reset();
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
report_request2_));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_report_transport_));
}
TEST_F(ServiceControlClientImplTest, TestNonCachedReportWithStoredCallback) {
// Calls Client::Report with a high important request, it will not be cached.
// Transport::Report() should be called.
// Transport::on_done() is called in the same thread with PERMISSION_DENIED
// The Client::done_done() is called with the same error.
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithStoredCallback));
ReportResponse report_response;
absl::Status done_status = absl::UnknownError("");
// This request is high important, so it will not be cached.
// client->Report() will call Transport::Report() right away.
report_request1_.mutable_operations(0)->set_importance(Operation::HIGH);
client_->Report(
report_request1_, &report_response,
[&done_status](absl::Status status) { done_status = status; });
// on_report_done is not called yet. waiting for transport one_report_done.
EXPECT_EQ(done_status, absl::UnknownError(""));
// Since it is not cached, transport should be called.
EXPECT_TRUE(mock_report_transport_.on_done_vector_.size() == 1);
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
report_request1_));
// Calls the on_check_done() to send status.
mock_report_transport_.on_done_vector_[0](
absl::Status(absl::StatusCode::kPermissionDenied, ""));
// on_report_done is called with right status.
EXPECT_EQ(absl::StatusCode::kPermissionDenied, done_status.code());
}
TEST_F(ServiceControlClientImplTest,
TestNonCachedReportWithStoredCallbackWithPerRequestTransport) {
// Calls Client::Report with a high important request, it will not be cached.
// Transport::Report() should be called.
// Transport::on_done() is called in the same thread with PERMISSION_DENIED
// The Client::done_done() is called with the same error.
MockReportTransport stack_mock_report_transport;
EXPECT_CALL(stack_mock_report_transport, Report(_, _, _))
.WillOnce(Invoke(&stack_mock_report_transport,
&MockReportTransport::ReportWithStoredCallback));
ReportResponse report_response;
absl::Status done_status = absl::UnknownError("");
// This request is high important, so it will not be cached.
// client->Report() will call Transport::Report() right away.
report_request1_.mutable_operations(0)->set_importance(Operation::HIGH);
client_->Report(
report_request1_, &report_response,
[&done_status](absl::Status status) { done_status = status; },
stack_mock_report_transport.GetFunc());
// on_report_done is not called yet. waiting for transport one_report_done.
EXPECT_EQ(done_status, absl::UnknownError(""));
// Since it is not cached, transport should be called.
EXPECT_TRUE(stack_mock_report_transport.on_done_vector_.size() == 1);
EXPECT_TRUE(MessageDifferencer::Equals(
stack_mock_report_transport.report_request_, report_request1_));
// Calls the on_check_done() to send status.
stack_mock_report_transport.on_done_vector_[0](
absl::Status(absl::StatusCode::kPermissionDenied, ""));
// on_report_done is called with right status.
EXPECT_EQ(absl::StatusCode::kPermissionDenied, done_status.code());
}
TEST_F(ServiceControlClientImplTest, TestNonCachedReportWithInplaceCallback) {
// Calls Client::Report with a high important request, it will not be cached.
// Transport::Report() should be called.
// Transport::on_done() is called inside Transport::Report() with error
// PERMISSION_DENIED. The Client::done_done() is called with the same error.
EXPECT_CALL(mock_report_transport_, Report(_, _, _))
.WillOnce(Invoke(&mock_report_transport_,
&MockReportTransport::ReportWithInplaceCallback));
// Set the report status to be used in the on_report_done
mock_report_transport_.done_status_ =
absl::Status(absl::StatusCode::kPermissionDenied, "");
ReportResponse report_response;
absl::Status done_status = absl::UnknownError("");
// This request is high important, so it will not be cached.
// client->Report() will call Transport::Report() right away.
report_request1_.mutable_operations(0)->set_importance(Operation::HIGH);
client_->Report(
report_request1_, &report_response,
[&done_status](absl::Status status) { done_status = status; });
Statistics stat;
absl::Status stat_status = client_->GetStatistics(&stat);
EXPECT_EQ(stat_status, absl::OkStatus());
EXPECT_EQ(stat.total_called_reports, 1);
EXPECT_EQ(stat.send_reports_by_flush, 0);
EXPECT_EQ(stat.send_reports_in_flight, 1);
EXPECT_EQ(stat.send_report_operations, 1);
// one_done should be called for now.
EXPECT_EQ(absl::StatusCode::kPermissionDenied, done_status.code());
// Since it is not cached, transport should be called.
EXPECT_TRUE(MessageDifferencer::Equals(mock_report_transport_.report_request_,
report_request1_));
}
TEST_F(ServiceControlClientImplTest, TestFlushIntervalReportNeverFlush) {
// With periodic_timer, report flush interval is -1, Check flush interval is
// 1000, so the overall flush interval is 1000
ServiceControlClientOptions options(
CheckAggregationOptions(1 /*entries */, 500 /* refresh_interval_ms */,
1000 /* expiration_ms */),