-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added unit test cases for rate limiting #124
Changes from 1 commit
9d96d7e
a7098ad
99af110
9dc18d7
267d0bb
021c942
f84d7d8
4c9dbd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,7 +231,7 @@ TEST_F(AggregatedTestWithRealClient, CheckOKTest) { | |
EXPECT_EQ(stat.send_report_operations, 0); | ||
} | ||
|
||
class QuotaAllocationFailedTestWithRealClient : public ::testing::Test { | ||
class QuotaAllocationTestWithRealClient : public ::testing::Test { | ||
public: | ||
void SetUp() { | ||
service_.set_name("test_service"); | ||
|
@@ -242,98 +242,75 @@ class QuotaAllocationFailedTestWithRealClient : public ::testing::Test { | |
ASSERT_TRUE((bool)(sc_lib_)); | ||
// This is the call actually creating the client. | ||
sc_lib_->Init(); | ||
|
||
metric_cost_vector_ = {{"metric_first", 1}, {"metric_second", 2}}; | ||
} | ||
|
||
void DoRunHTTPRequest(HTTPRequest* request) { | ||
std::map<std::string, std::string> headers; | ||
AllocateQuotaResponse allocate_quota_response_; | ||
|
||
ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( | ||
kAllocateQuotaResponseErrorExhausted, &allocate_quota_response_)); | ||
AllocateQuotaRequest quota_request; | ||
AllocateQuotaResponse quota_response; | ||
|
||
std::string body = allocate_quota_response_.SerializeAsString(); | ||
ASSERT_TRUE(quota_request.ParseFromString(request->body())); | ||
ASSERT_EQ(quota_request.allocate_operation().quota_metrics_size(), 2); | ||
|
||
request->OnComplete(Status::OK, std::move(headers), std::move(body)); | ||
} | ||
std::set<std::pair<std::string, int>> expected_costs = { | ||
{"metric_first", 1}, {"metric_second", 2}}; | ||
std::set<std::pair<std::string, int>> actual_costs; | ||
|
||
::google::api::Service service_; | ||
std::unique_ptr<MockApiManagerEnvironment> env_; | ||
std::unique_ptr<Interface> sc_lib_; | ||
}; | ||
|
||
TEST_F(QuotaAllocationFailedTestWithRealClient, AllocateQuotaTest) { | ||
EXPECT_CALL(*env_, DoRunHTTPRequest(_)) | ||
.WillOnce(Invoke( | ||
this, &QuotaAllocationFailedTestWithRealClient::DoRunHTTPRequest)); | ||
|
||
std::vector<std::pair<std::string, int>> metric_cost_vector; | ||
metric_cost_vector.push_back( | ||
std::make_pair<std::string, int>("metric_first", 1)); | ||
metric_cost_vector.push_back( | ||
std::make_pair<std::string, int>("metric_second", 2)); | ||
for (auto rule : quota_request.allocate_operation().quota_metrics()) { | ||
actual_costs.insert(std::make_pair(rule.metric_name(), | ||
rule.metric_values(0).int64_value())); | ||
} | ||
|
||
QuotaRequestInfo info; | ||
ASSERT_EQ(actual_costs, expected_costs); | ||
|
||
info.metric_cost_vector = &metric_cost_vector; | ||
ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( | ||
kAllocateQuotaResponse, "a_response)); | ||
|
||
FillOperationInfo(&info); | ||
sc_lib_->Quota(info, nullptr, [](Status status) { | ||
ASSERT_TRUE(status.code() == Code::RESOURCE_EXHAUSTED); | ||
}); | ||
} | ||
std::string body = quota_response.SerializeAsString(); | ||
|
||
class QuotaAllocationTestWithRealClient : public ::testing::Test { | ||
public: | ||
void SetUp() { | ||
service_.set_name("test_service"); | ||
service_.mutable_control()->set_environment( | ||
"servicecontrol.googleapis.com"); | ||
env_.reset(new ::testing::NiceMock<MockApiManagerEnvironment>); | ||
sc_lib_.reset(Aggregated::Create(service_, nullptr, env_.get(), nullptr)); | ||
ASSERT_TRUE((bool)(sc_lib_)); | ||
// This is the call actually creating the client. | ||
sc_lib_->Init(); | ||
request->OnComplete(Status::OK, std::move(headers), std::move(body)); | ||
} | ||
|
||
void DoRunHTTPRequest(HTTPRequest* request) { | ||
void DoRunHTTPRequestAllocationFailed(HTTPRequest* request) { | ||
std::map<std::string, std::string> headers; | ||
AllocateQuotaRequest allocate_quota_request_; | ||
AllocateQuotaResponse allocate_quota_response_; | ||
|
||
ASSERT_TRUE(allocate_quota_request_.ParseFromString(request->body())); | ||
ASSERT_EQ(allocate_quota_request_.allocate_operation().quota_metrics_size(), | ||
2); | ||
|
||
std::unordered_map<std::string, int> metric_rules; | ||
for (auto rule : | ||
allocate_quota_request_.allocate_operation().quota_metrics()) { | ||
metric_rules[rule.metric_name()] = rule.metric_values(0).int64_value(); | ||
} | ||
ASSERT_EQ(metric_rules.size(), 2); | ||
|
||
ASSERT_NE(metric_rules.find("metric_first"), metric_rules.end()); | ||
ASSERT_NE(metric_rules.find("metric_second"), metric_rules.end()); | ||
ASSERT_EQ(metric_rules["metric_first"], 1); | ||
ASSERT_EQ(metric_rules["metric_second"], 2); | ||
AllocateQuotaResponse quota_response; | ||
|
||
ASSERT_TRUE(::google::protobuf::TextFormat::ParseFromString( | ||
kAllocateQuotaResponse, &allocate_quota_response_)); | ||
kAllocateQuotaResponseErrorExhausted, "a_response)); | ||
|
||
std::string body = allocate_quota_response_.SerializeAsString(); | ||
std::string body = quota_response.SerializeAsString(); | ||
|
||
request->OnComplete(Status::OK, std::move(headers), std::move(body)); | ||
} | ||
|
||
::google::api::Service service_; | ||
std::unique_ptr<MockApiManagerEnvironment> env_; | ||
std::unique_ptr<Interface> sc_lib_; | ||
std::vector<std::pair<std::string, int>> metric_cost_vector_; | ||
}; | ||
|
||
TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaTest) { | ||
EXPECT_CALL(*env_, DoRunHTTPRequest(_)) | ||
.WillOnce( | ||
Invoke(this, &QuotaAllocationTestWithRealClient::DoRunHTTPRequest)); | ||
|
||
QuotaRequestInfo info; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we remove these un-necesary blank lines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is ok to have line 303, but I think line 301 is not needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
info.metric_cost_vector = &metric_cost_vector_; | ||
|
||
FillOperationInfo(&info); | ||
sc_lib_->Quota(info, nullptr, | ||
[](Status status) { ASSERT_TRUE(status.ok()); }); | ||
} | ||
|
||
TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaFailedTest) { | ||
EXPECT_CALL(*env_, DoRunHTTPRequest(_)) | ||
.WillOnce(Invoke(this, &QuotaAllocationTestWithRealClient:: | ||
DoRunHTTPRequestAllocationFailed)); | ||
|
||
std::vector<std::pair<std::string, int>> metric_cost_vector; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we try this: metric_cost_ve = { {metric1, cost1}, {metric2, cost2} } to use cxx11 initialization feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beside, this can be in test class variable and setup in SetUp() fucntion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
metric_cost_vector.push_back( | ||
std::make_pair<std::string, int>("metric_first", 1)); | ||
|
@@ -345,8 +322,9 @@ TEST_F(QuotaAllocationTestWithRealClient, AllocateQuotaTest) { | |
info.metric_cost_vector = &metric_cost_vector; | ||
|
||
FillOperationInfo(&info); | ||
sc_lib_->Quota(info, nullptr, | ||
[](Status status) { ASSERT_TRUE(status.ok()); }); | ||
sc_lib_->Quota(info, nullptr, [](Status status) { | ||
ASSERT_TRUE(status.code() == Code::RESOURCE_EXHAUSTED); | ||
}); | ||
} | ||
|
||
TEST(AggregatedServiceControlTest, Create) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems that line 279-284 can be a small function so it can be called by line 269 too.