Skip to content
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

Merged
merged 8 commits into from
Feb 24, 2017
104 changes: 41 additions & 63 deletions contrib/endpoints/src/api_manager/service_control/aggregated_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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, &quota_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, &quota_response));

std::string body = allocate_quota_response_.SerializeAsString();
std::string body = quota_response.SerializeAsString();

Copy link
Contributor

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.

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;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we remove these un-necesary blank lines?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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));
Expand All @@ -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) {
Expand Down
18 changes: 0 additions & 18 deletions contrib/endpoints/src/api_manager/service_control/proto_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,24 +219,6 @@ TEST_F(ProtoTest, FillAllocateQuotaRequestNoMethodNameTest) {
ASSERT_EQ(expected_text, text);
}

TEST_F(ProtoTest, FillNoMetricRulesAllocateQuotaRequestTest) {
google::api_manager::service_control::QuotaRequestInfo info;

info.metric_cost_vector = nullptr;

FillOperationInfo(&info);
FillAllocateQuotaRequestInfo(&info);

gasv1::AllocateQuotaRequest request;
ASSERT_TRUE(scp_.FillAllocateQuotaRequest(info, &request).ok());

std::string text = AllocateQuotaRequestToString(&request);
std::string expected_text =
ReadTestBaseline("allocate_quota_request_no_metrics_rule.golden");

ASSERT_EQ(expected_text, text);
}

TEST_F(ProtoTest, FillNoApiKeyCheckRequestTest) {
CheckRequestInfo info;
info.operation_id = "operation_id";
Expand Down