Skip to content
Snippets Groups Projects
Commit 11a4ed80 authored by Eric Gribkoff's avatar Eric Gribkoff
Browse files

refactor client

parent caf7ea53
No related branches found
No related tags found
No related merge requests found
...@@ -65,7 +65,9 @@ Http2Client::ServiceStub::ServiceStub(std::shared_ptr<Channel> channel) ...@@ -65,7 +65,9 @@ Http2Client::ServiceStub::ServiceStub(std::shared_ptr<Channel> channel)
TestService::Stub* Http2Client::ServiceStub::Get() { return stub_.get(); } TestService::Stub* Http2Client::ServiceStub::Get() { return stub_.get(); }
Http2Client::Http2Client(std::shared_ptr<Channel> channel) Http2Client::Http2Client(std::shared_ptr<Channel> channel)
: serviceStub_(channel), channel_(channel) {} : serviceStub_(channel),
channel_(channel),
defaultRequest_(BuildDefaultRequest()) {}
bool Http2Client::AssertStatusCode(const Status& s, StatusCode expected_code) { bool Http2Client::AssertStatusCode(const Status& s, StatusCode expected_code) {
if (s.error_code() == expected_code) { if (s.error_code() == expected_code) {
...@@ -77,18 +79,24 @@ bool Http2Client::AssertStatusCode(const Status& s, StatusCode expected_code) { ...@@ -77,18 +79,24 @@ bool Http2Client::AssertStatusCode(const Status& s, StatusCode expected_code) {
abort(); abort();
} }
bool Http2Client::DoRstAfterHeader() { Status Http2Client::SendUnaryCall(SimpleResponse* response) {
gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after header");
ClientContext context; ClientContext context;
return serviceStub_.Get()->UnaryCall(&context, defaultRequest_, response);
}
SimpleRequest Http2Client::BuildDefaultRequest() {
SimpleRequest request; SimpleRequest request;
SimpleResponse response;
request.set_response_size(kLargeResponseSize); request.set_response_size(kLargeResponseSize);
grpc::string payload(kLargeRequestSize, '\0'); grpc::string payload(kLargeRequestSize, '\0');
request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize); request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
return request;
}
Status s = serviceStub_.Get()->UnaryCall(&context, request, &response); bool Http2Client::DoRstAfterHeader() {
AssertStatusCode(s, grpc::StatusCode::INTERNAL); gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after header");
SimpleResponse response;
AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
GPR_ASSERT(!response.has_payload()); // no data should be received GPR_ASSERT(!response.has_payload()); // no data should be received
gpr_log(GPR_DEBUG, "Done testing reset stream after header"); gpr_log(GPR_DEBUG, "Done testing reset stream after header");
...@@ -98,15 +106,8 @@ bool Http2Client::DoRstAfterHeader() { ...@@ -98,15 +106,8 @@ bool Http2Client::DoRstAfterHeader() {
bool Http2Client::DoRstAfterData() { bool Http2Client::DoRstAfterData() {
gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after data"); gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream after data");
ClientContext context;
SimpleRequest request;
SimpleResponse response; SimpleResponse response;
request.set_response_size(kLargeResponseSize); AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
grpc::string payload(kLargeRequestSize, '\0');
request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
Status s = serviceStub_.Get()->UnaryCall(&context, request, &response);
AssertStatusCode(s, grpc::StatusCode::INTERNAL);
GPR_ASSERT(response.has_payload()); // data should be received GPR_ASSERT(response.has_payload()); // data should be received
gpr_log(GPR_DEBUG, "Done testing reset stream after data"); gpr_log(GPR_DEBUG, "Done testing reset stream after data");
...@@ -116,15 +117,8 @@ bool Http2Client::DoRstAfterData() { ...@@ -116,15 +117,8 @@ bool Http2Client::DoRstAfterData() {
bool Http2Client::DoRstDuringData() { bool Http2Client::DoRstDuringData() {
gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream during data"); gpr_log(GPR_DEBUG, "Sending RPC and expecting reset stream during data");
ClientContext context;
SimpleRequest request;
SimpleResponse response; SimpleResponse response;
request.set_response_size(kLargeResponseSize); AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::INTERNAL);
grpc::string payload(kLargeRequestSize, '\0');
request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
Status s = serviceStub_.Get()->UnaryCall(&context, request, &response);
AssertStatusCode(s, grpc::StatusCode::INTERNAL);
GPR_ASSERT(!response.has_payload()); // no data should be received GPR_ASSERT(!response.has_payload()); // no data should be received
gpr_log(GPR_DEBUG, "Done testing reset stream during data"); gpr_log(GPR_DEBUG, "Done testing reset stream during data");
...@@ -133,65 +127,37 @@ bool Http2Client::DoRstDuringData() { ...@@ -133,65 +127,37 @@ bool Http2Client::DoRstDuringData() {
bool Http2Client::DoGoaway() { bool Http2Client::DoGoaway() {
gpr_log(GPR_DEBUG, "Sending two RPCs and expecting goaway"); gpr_log(GPR_DEBUG, "Sending two RPCs and expecting goaway");
SimpleResponse response;
SimpleRequest request; AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
request.set_response_size(kLargeResponseSize); GPR_ASSERT(response.payload().body() ==
grpc::string payload(kLargeRequestSize, '\0');
request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
ClientContext context1;
SimpleResponse response1;
Status s = serviceStub_.Get()->UnaryCall(&context1, request, &response1);
AssertStatusCode(s, grpc::StatusCode::OK);
GPR_ASSERT(response1.payload().body() ==
grpc::string(kLargeResponseSize, '\0')); grpc::string(kLargeResponseSize, '\0'));
// Sleep for one second to give time for client to receive goaway frame. // Sleep for one second to give time for client to receive goaway frame.
gpr_timespec sleep_time = gpr_time_add( gpr_timespec sleep_time = gpr_time_add(
gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(1000, GPR_TIMESPAN)); gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(1, GPR_TIMESPAN));
gpr_sleep_until(sleep_time); gpr_sleep_until(sleep_time);
ClientContext context2; response.Clear();
SimpleResponse response2; AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
s = serviceStub_.Get()->UnaryCall(&context2, request, &response2); GPR_ASSERT(response.payload().body() ==
AssertStatusCode(s, grpc::StatusCode::OK);
GPR_ASSERT(response2.payload().body() ==
grpc::string(kLargeResponseSize, '\0')); grpc::string(kLargeResponseSize, '\0'));
gpr_log(GPR_DEBUG, "Done testing goaway"); gpr_log(GPR_DEBUG, "Done testing goaway");
return true; return true;
} }
bool Http2Client::DoPing() { bool Http2Client::DoPing() {
gpr_log(GPR_DEBUG, "Sending RPC and expecting ping"); gpr_log(GPR_DEBUG, "Sending RPC and expecting ping");
ClientContext context;
SimpleRequest request;
SimpleResponse response; SimpleResponse response;
request.set_response_size(kLargeResponseSize); AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
grpc::string payload(kLargeRequestSize, '\0');
request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
Status s = serviceStub_.Get()->UnaryCall(&context, request, &response);
AssertStatusCode(s, grpc::StatusCode::OK);
GPR_ASSERT(response.payload().body() == GPR_ASSERT(response.payload().body() ==
grpc::string(kLargeResponseSize, '\0')); grpc::string(kLargeResponseSize, '\0'));
gpr_log(GPR_DEBUG, "Done testing ping"); gpr_log(GPR_DEBUG, "Done testing ping");
return true; return true;
} }
void Http2Client::MaxStreamsWorker(std::shared_ptr<grpc::Channel> channel) { void Http2Client::MaxStreamsWorker(std::shared_ptr<grpc::Channel> channel) {
ClientContext context;
SimpleRequest request;
SimpleResponse response; SimpleResponse response;
request.set_response_size(kLargeResponseSize); AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
grpc::string payload(kLargeRequestSize, '\0');
request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
Status s =
TestService::NewStub(channel)->UnaryCall(&context, request, &response);
AssertStatusCode(s, grpc::StatusCode::OK);
GPR_ASSERT(response.payload().body() == GPR_ASSERT(response.payload().body() ==
grpc::string(kLargeResponseSize, '\0')); grpc::string(kLargeResponseSize, '\0'));
} }
...@@ -201,15 +167,8 @@ bool Http2Client::DoMaxStreams() { ...@@ -201,15 +167,8 @@ bool Http2Client::DoMaxStreams() {
// Make an initial call on the channel to ensure the server's max streams // Make an initial call on the channel to ensure the server's max streams
// setting is received // setting is received
ClientContext context;
SimpleRequest request;
SimpleResponse response; SimpleResponse response;
request.set_response_size(kLargeResponseSize); AssertStatusCode(SendUnaryCall(&response), grpc::StatusCode::OK);
grpc::string payload(kLargeRequestSize, '\0');
request.mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
Status s =
TestService::NewStub(channel_)->UnaryCall(&context, request, &response);
AssertStatusCode(s, grpc::StatusCode::OK);
GPR_ASSERT(response.payload().body() == GPR_ASSERT(response.payload().body() ==
grpc::string(kLargeResponseSize, '\0')); grpc::string(kLargeResponseSize, '\0'));
......
...@@ -70,8 +70,11 @@ class Http2Client { ...@@ -70,8 +70,11 @@ class Http2Client {
void MaxStreamsWorker(std::shared_ptr<grpc::Channel> channel); void MaxStreamsWorker(std::shared_ptr<grpc::Channel> channel);
bool AssertStatusCode(const Status& s, StatusCode expected_code); bool AssertStatusCode(const Status& s, StatusCode expected_code);
Status SendUnaryCall(SimpleResponse* response);
SimpleRequest BuildDefaultRequest();
ServiceStub serviceStub_; ServiceStub serviceStub_;
std::shared_ptr<Channel> channel_; std::shared_ptr<Channel> channel_;
SimpleRequest defaultRequest_;
}; };
} // namespace testing } // namespace testing
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment