diff options
author | yangg <yangg@google.com> | 2015-01-06 10:16:15 -0800 |
---|---|---|
committer | Nicolas Noble <nnoble@google.com> | 2015-01-06 17:45:46 -0800 |
commit | ed5e7e006b1a60ef01a0ee1db144e959436b53d7 (patch) | |
tree | 4166d8504d804d00ff96e8ce47ae4e6ecf7e2152 /test/cpp | |
parent | c87b1c533f84a9bed94143908b00b0cc498cfda5 (diff) |
Add deadline API on server side.
Change on 2015/01/06 by yangg <yangg@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=83351442
Diffstat (limited to 'test/cpp')
-rw-r--r-- | test/cpp/end2end/end2end_test.cc | 93 | ||||
-rw-r--r-- | test/cpp/util/echo.proto | 13 | ||||
-rw-r--r-- | test/cpp/util/time_test.cc | 6 |
3 files changed, 108 insertions, 4 deletions
diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 4b5a9a8e6a..83037b5ab1 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -31,10 +31,12 @@ * */ +#include <chrono> #include <thread> -#include "src/cpp/server/rpc_service_method.h" + #include "test/cpp/util/echo.pb.h" -#include "net/util/netutil.h" +#include "src/cpp/server/rpc_service_method.h" +#include "src/cpp/util/time.h" #include <grpc++/channel_arguments.h> #include <grpc++/channel_interface.h> #include <grpc++/client_context.h> @@ -44,22 +46,43 @@ #include <grpc++/server_context.h> #include <grpc++/status.h> #include <grpc++/stream.h> +#include "net/util/netutil.h" #include <gtest/gtest.h> #include <grpc/grpc.h> #include <grpc/support/thd.h> +#include <grpc/support/time.h> using grpc::cpp::test::util::EchoRequest; using grpc::cpp::test::util::EchoResponse; using grpc::cpp::test::util::TestService; +using std::chrono::system_clock; namespace grpc { +namespace testing { + +namespace { + +// When echo_deadline is requested, deadline seen in the ServerContext is set in +// the response in seconds. +void MaybeEchoDeadline(ServerContext* context, const EchoRequest* request, + EchoResponse* response) { + if (request->has_param() && request->param().echo_deadline()) { + gpr_timespec deadline = gpr_inf_future; + if (context->absolute_deadline() != system_clock::time_point::max()) { + Timepoint2Timespec(context->absolute_deadline(), &deadline); + } + response->mutable_param()->set_request_deadline(deadline.tv_sec); + } +} +} // namespace class TestServiceImpl : public TestService::Service { public: Status Echo(ServerContext* context, const EchoRequest* request, EchoResponse* response) { response->set_message(request->message()); + MaybeEchoDeadline(context, request, response); return Status::OK; } @@ -179,6 +202,71 @@ TEST_F(End2endTest, RpcDeadlineExpires) { delete stub; } +// Set a long but finite deadline. +TEST_F(End2endTest, RpcLongDeadline) { + std::shared_ptr<ChannelInterface> channel = + CreateChannel(server_address_.str(), ChannelArguments()); + TestService::Stub* stub = TestService::NewStub(channel); + EchoRequest request; + EchoResponse response; + request.set_message("Hello"); + + ClientContext context; + std::chrono::system_clock::time_point deadline = + std::chrono::system_clock::now() + std::chrono::hours(1); + context.set_absolute_deadline(deadline); + Status s = stub->Echo(&context, request, &response); + EXPECT_EQ(response.message(), request.message()); + EXPECT_TRUE(s.IsOk()); + + delete stub; +} + +// Ask server to echo back the deadline it sees. +TEST_F(End2endTest, EchoDeadline) { + std::shared_ptr<ChannelInterface> channel = + CreateChannel(server_address_.str(), ChannelArguments()); + TestService::Stub* stub = TestService::NewStub(channel); + EchoRequest request; + EchoResponse response; + request.set_message("Hello"); + request.mutable_param()->set_echo_deadline(true); + + ClientContext context; + std::chrono::system_clock::time_point deadline = + std::chrono::system_clock::now() + std::chrono::seconds(100); + context.set_absolute_deadline(deadline); + Status s = stub->Echo(&context, request, &response); + EXPECT_EQ(response.message(), request.message()); + EXPECT_TRUE(s.IsOk()); + gpr_timespec sent_deadline; + Timepoint2Timespec(deadline, &sent_deadline); + // Allow 1 second error. + EXPECT_LE(response.param().request_deadline() - sent_deadline.tv_sec, 1); + EXPECT_GE(response.param().request_deadline() - sent_deadline.tv_sec, -1); + + delete stub; +} + +// Ask server to echo back the deadline it sees. The rpc has no deadline. +TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) { + std::shared_ptr<ChannelInterface> channel = + CreateChannel(server_address_.str(), ChannelArguments()); + TestService::Stub* stub = TestService::NewStub(channel); + EchoRequest request; + EchoResponse response; + request.set_message("Hello"); + request.mutable_param()->set_echo_deadline(true); + + ClientContext context; + Status s = stub->Echo(&context, request, &response); + EXPECT_EQ(response.message(), request.message()); + EXPECT_TRUE(s.IsOk()); + EXPECT_EQ(response.param().request_deadline(), gpr_inf_future.tv_sec); + + delete stub; +} + TEST_F(End2endTest, UnimplementedRpc) { std::shared_ptr<ChannelInterface> channel = CreateChannel(server_address_.str(), ChannelArguments()); @@ -300,6 +388,7 @@ TEST_F(End2endTest, BidiStream) { delete stub; } +} // namespace testing } // namespace grpc int main(int argc, char** argv) { diff --git a/test/cpp/util/echo.proto b/test/cpp/util/echo.proto index abce7ad03a..1240399bf8 100644 --- a/test/cpp/util/echo.proto +++ b/test/cpp/util/echo.proto @@ -2,12 +2,22 @@ syntax = "proto2"; package grpc.cpp.test.util; +message RequestParams { + optional bool echo_deadline = 1; +} + message EchoRequest { optional string message = 1; + optional RequestParams param = 2; +} + +message ResponseParams { + optional int64 request_deadline = 1; } message EchoResponse { optional string message = 1; + optional ResponseParams param = 2; } service TestService { @@ -15,6 +25,5 @@ service TestService { rpc RequestStream(stream EchoRequest) returns (EchoResponse); rpc ResponseStream(EchoRequest) returns (stream EchoResponse); rpc BidiStream(stream EchoRequest) returns (stream EchoResponse); - rpc Unimplemented(EchoRequest) returns (EchoResponse) { - } + rpc Unimplemented(EchoRequest) returns (EchoResponse); } diff --git a/test/cpp/util/time_test.cc b/test/cpp/util/time_test.cc index 97499fed28..c571808633 100644 --- a/test/cpp/util/time_test.cc +++ b/test/cpp/util/time_test.cc @@ -62,5 +62,11 @@ TEST_F(TimeTest, AbsolutePointTest) { EXPECT_TRUE(tp == tp_converted_2); } +// gpr_inf_future is treated specially and mapped to time_point::max() +TEST_F(TimeTest, InfFuture) { + EXPECT_EQ(system_clock::time_point::max(), + Timespec2Timepoint(gpr_inf_future)); +} + } // namespace } // namespace grpc |