Skip to content
Snippets Groups Projects
Commit 2c3be1df authored by Craig Tiller's avatar Craig Tiller
Browse files

Add tests for C++ propagation

parent e40f964e
Branches
Tags
No related merge requests found
...@@ -85,23 +85,23 @@ class PropagationOptions { ...@@ -85,23 +85,23 @@ class PropagationOptions {
return *this; return *this;
} }
PropagationOptions& enable_stats_propagation() { PropagationOptions& enable_census_stats_propagation() {
propagate_ |= GRPC_PROPAGATE_STATS_CONTEXT; propagate_ |= GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
return *this; return *this;
} }
PropagationOptions& disable_stats_propagation() { PropagationOptions& disable_census_stats_propagation() {
propagate_ &= ~GRPC_PROPAGATE_STATS_CONTEXT; propagate_ &= ~GRPC_PROPAGATE_CENSUS_STATS_CONTEXT;
return *this; return *this;
} }
PropagationOptions& enable_tracing_propagation() { PropagationOptions& enable_census_tracing_propagation() {
propagate_ |= GRPC_PROPAGATE_TRACING_CONTEXT; propagate_ |= GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
return *this; return *this;
} }
PropagationOptions& disable_tracing_propagation() { PropagationOptions& disable_census_tracing_propagation() {
propagate_ &= ~GRPC_PROPAGATE_TRACING_CONTEXT; propagate_ &= ~GRPC_PROPAGATE_CENSUS_TRACING_CONTEXT;
return *this; return *this;
} }
...@@ -127,7 +127,7 @@ class ClientContext { ...@@ -127,7 +127,7 @@ class ClientContext {
~ClientContext(); ~ClientContext();
/// Create a new ClientContext that propagates some or all of its attributes /// Create a new ClientContext that propagates some or all of its attributes
static ClientContext FromServerContext( static std::unique_ptr<ClientContext> FromServerContext(
const ServerContext& server_context, const ServerContext& server_context,
PropagationOptions options = PropagationOptions()); PropagationOptions options = PropagationOptions());
......
...@@ -66,11 +66,11 @@ ClientContext::~ClientContext() { ...@@ -66,11 +66,11 @@ ClientContext::~ClientContext() {
} }
} }
ClientContext ClientContext::FromServerContext(const ServerContext& context, std::unique_ptr<ClientContext> ClientContext::FromServerContext(
PropagationOptions options) { const ServerContext& context, PropagationOptions options) {
ClientContext ctx; std::unique_ptr<ClientContext> ctx(new ClientContext);
ctx.propagate_from_call_ = context.call_; ctx->propagate_from_call_ = context.call_;
ctx.propagation_options_ = options; ctx->propagation_options_ = options;
return ctx; return ctx;
} }
......
...@@ -104,6 +104,22 @@ bool CheckIsLocalhost(const grpc::string& addr) { ...@@ -104,6 +104,22 @@ bool CheckIsLocalhost(const grpc::string& addr) {
} // namespace } // namespace
class Proxy : public ::grpc::cpp::test::util::TestService::Service {
public:
Proxy(std::shared_ptr<ChannelInterface> channel)
: stub_(grpc::cpp::test::util::TestService::NewStub(channel)) {}
Status Echo(ServerContext* server_context, const EchoRequest* request,
EchoResponse* response) GRPC_OVERRIDE {
std::unique_ptr<ClientContext> client_context =
ClientContext::FromServerContext(*server_context);
return stub_->Echo(client_context.get(), *request, response);
}
private:
std::unique_ptr<::grpc::cpp::test::util::TestService::Stub> stub_;
};
class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service { class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
public: public:
TestServiceImpl() : signal_client_(false), host_() {} TestServiceImpl() : signal_client_(false), host_() {}
...@@ -241,7 +257,9 @@ class TestServiceImplDupPkg ...@@ -241,7 +257,9 @@ class TestServiceImplDupPkg
} }
}; };
class End2endTest : public ::testing::Test { /* Param is whether or not to use a proxy -- some tests use TEST_F as they don't
need this functionality */
class End2endTest : public ::testing::TestWithParam<bool> {
protected: protected:
End2endTest() End2endTest()
: kMaxMessageSize_(8192), special_service_("special"), thread_pool_(2) {} : kMaxMessageSize_(8192), special_service_("special"), thread_pool_(2) {}
...@@ -267,21 +285,41 @@ class End2endTest : public ::testing::Test { ...@@ -267,21 +285,41 @@ class End2endTest : public ::testing::Test {
server_ = builder.BuildAndStart(); server_ = builder.BuildAndStart();
} }
void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } void TearDown() GRPC_OVERRIDE {
server_->Shutdown();
if (proxy_server_) proxy_server_->Shutdown();
}
void ResetStub() { void ResetStub(bool use_proxy) {
SslCredentialsOptions ssl_opts = {test_root_cert, "", ""}; SslCredentialsOptions ssl_opts = {test_root_cert, "", ""};
ChannelArguments args; ChannelArguments args;
args.SetSslTargetNameOverride("foo.test.google.fr"); args.SetSslTargetNameOverride("foo.test.google.fr");
args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test"); args.SetString(GRPC_ARG_SECONDARY_USER_AGENT_STRING, "end2end_test");
channel_ = CreateChannel(server_address_.str(), SslCredentials(ssl_opts), channel_ = CreateChannel(server_address_.str(), SslCredentials(ssl_opts),
args); args);
if (use_proxy) {
proxy_service_.reset(new Proxy(channel_));
int port = grpc_pick_unused_port_or_die();
std::ostringstream proxyaddr;
proxyaddr << "localhost:" << port;
ServerBuilder builder;
builder.AddListeningPort(proxyaddr.str(), InsecureServerCredentials());
builder.RegisterService(proxy_service_.get());
builder.SetThreadPool(&thread_pool_);
proxy_server_ = builder.BuildAndStart();
channel_ = CreateChannel(proxyaddr.str(), InsecureCredentials(),
ChannelArguments());
}
stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel_)); stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel_));
} }
std::shared_ptr<ChannelInterface> channel_; std::shared_ptr<ChannelInterface> channel_;
std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_; std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
std::unique_ptr<Server> server_; std::unique_ptr<Server> server_;
std::unique_ptr<Server> proxy_server_;
std::unique_ptr<Proxy> proxy_service_;
std::ostringstream server_address_; std::ostringstream server_address_;
const int kMaxMessageSize_; const int kMaxMessageSize_;
TestServiceImpl service_; TestServiceImpl service_;
...@@ -306,7 +344,7 @@ static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub, ...@@ -306,7 +344,7 @@ static void SendRpc(grpc::cpp::test::util::TestService::Stub* stub,
} }
TEST_F(End2endTest, SimpleRpcWithHost) { TEST_F(End2endTest, SimpleRpcWithHost) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
...@@ -321,13 +359,13 @@ TEST_F(End2endTest, SimpleRpcWithHost) { ...@@ -321,13 +359,13 @@ TEST_F(End2endTest, SimpleRpcWithHost) {
EXPECT_TRUE(s.ok()); EXPECT_TRUE(s.ok());
} }
TEST_F(End2endTest, SimpleRpc) { TEST_P(End2endTest, SimpleRpc) {
ResetStub(); ResetStub(GetParam());
SendRpc(stub_.get(), 1); SendRpc(stub_.get(), 1);
} }
TEST_F(End2endTest, MultipleRpcs) { TEST_P(End2endTest, MultipleRpcs) {
ResetStub(); ResetStub(GetParam());
std::vector<std::thread*> threads; std::vector<std::thread*> threads;
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
threads.push_back(new std::thread(SendRpc, stub_.get(), 10)); threads.push_back(new std::thread(SendRpc, stub_.get(), 10));
...@@ -339,8 +377,8 @@ TEST_F(End2endTest, MultipleRpcs) { ...@@ -339,8 +377,8 @@ TEST_F(End2endTest, MultipleRpcs) {
} }
// Set a 10us deadline and make sure proper error is returned. // Set a 10us deadline and make sure proper error is returned.
TEST_F(End2endTest, RpcDeadlineExpires) { TEST_P(End2endTest, RpcDeadlineExpires) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -354,8 +392,8 @@ TEST_F(End2endTest, RpcDeadlineExpires) { ...@@ -354,8 +392,8 @@ TEST_F(End2endTest, RpcDeadlineExpires) {
} }
// Set a long but finite deadline. // Set a long but finite deadline.
TEST_F(End2endTest, RpcLongDeadline) { TEST_P(End2endTest, RpcLongDeadline) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -370,8 +408,8 @@ TEST_F(End2endTest, RpcLongDeadline) { ...@@ -370,8 +408,8 @@ TEST_F(End2endTest, RpcLongDeadline) {
} }
// Ask server to echo back the deadline it sees. // Ask server to echo back the deadline it sees.
TEST_F(End2endTest, EchoDeadline) { TEST_P(End2endTest, EchoDeadline) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -392,8 +430,8 @@ TEST_F(End2endTest, EchoDeadline) { ...@@ -392,8 +430,8 @@ TEST_F(End2endTest, EchoDeadline) {
} }
// Ask server to echo back the deadline it sees. The rpc has no deadline. // Ask server to echo back the deadline it sees. The rpc has no deadline.
TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) { TEST_P(End2endTest, EchoDeadlineForNoDeadlineRpc) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -407,8 +445,8 @@ TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) { ...@@ -407,8 +445,8 @@ TEST_F(End2endTest, EchoDeadlineForNoDeadlineRpc) {
gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec); gpr_inf_future(GPR_CLOCK_REALTIME).tv_sec);
} }
TEST_F(End2endTest, UnimplementedRpc) { TEST_P(End2endTest, UnimplementedRpc) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -422,7 +460,7 @@ TEST_F(End2endTest, UnimplementedRpc) { ...@@ -422,7 +460,7 @@ TEST_F(End2endTest, UnimplementedRpc) {
} }
TEST_F(End2endTest, RequestStreamOneRequest) { TEST_F(End2endTest, RequestStreamOneRequest) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -437,7 +475,7 @@ TEST_F(End2endTest, RequestStreamOneRequest) { ...@@ -437,7 +475,7 @@ TEST_F(End2endTest, RequestStreamOneRequest) {
} }
TEST_F(End2endTest, RequestStreamTwoRequests) { TEST_F(End2endTest, RequestStreamTwoRequests) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -453,7 +491,7 @@ TEST_F(End2endTest, RequestStreamTwoRequests) { ...@@ -453,7 +491,7 @@ TEST_F(End2endTest, RequestStreamTwoRequests) {
} }
TEST_F(End2endTest, ResponseStream) { TEST_F(End2endTest, ResponseStream) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -473,7 +511,7 @@ TEST_F(End2endTest, ResponseStream) { ...@@ -473,7 +511,7 @@ TEST_F(End2endTest, ResponseStream) {
} }
TEST_F(End2endTest, BidiStream) { TEST_F(End2endTest, BidiStream) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -506,7 +544,7 @@ TEST_F(End2endTest, BidiStream) { ...@@ -506,7 +544,7 @@ TEST_F(End2endTest, BidiStream) {
// Talk to the two services with the same name but different package names. // Talk to the two services with the same name but different package names.
// The two stubs are created on the same channel. // The two stubs are created on the same channel.
TEST_F(End2endTest, DiffPackageServices) { TEST_F(End2endTest, DiffPackageServices) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -561,8 +599,8 @@ void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) { ...@@ -561,8 +599,8 @@ void CancelRpc(ClientContext* context, int delay_us, TestServiceImpl* service) {
} }
// Client cancels rpc after 10ms // Client cancels rpc after 10ms
TEST_F(End2endTest, ClientCancelsRpc) { TEST_P(End2endTest, ClientCancelsRpc) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -578,8 +616,8 @@ TEST_F(End2endTest, ClientCancelsRpc) { ...@@ -578,8 +616,8 @@ TEST_F(End2endTest, ClientCancelsRpc) {
} }
// Server cancels rpc after 1ms // Server cancels rpc after 1ms
TEST_F(End2endTest, ServerCancelsRpc) { TEST_P(End2endTest, ServerCancelsRpc) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -593,7 +631,7 @@ TEST_F(End2endTest, ServerCancelsRpc) { ...@@ -593,7 +631,7 @@ TEST_F(End2endTest, ServerCancelsRpc) {
// Client cancels request stream after sending two messages // Client cancels request stream after sending two messages
TEST_F(End2endTest, ClientCancelsRequestStream) { TEST_F(End2endTest, ClientCancelsRequestStream) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -613,7 +651,7 @@ TEST_F(End2endTest, ClientCancelsRequestStream) { ...@@ -613,7 +651,7 @@ TEST_F(End2endTest, ClientCancelsRequestStream) {
// Client cancels server stream after sending some messages // Client cancels server stream after sending some messages
TEST_F(End2endTest, ClientCancelsResponseStream) { TEST_F(End2endTest, ClientCancelsResponseStream) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -645,7 +683,7 @@ TEST_F(End2endTest, ClientCancelsResponseStream) { ...@@ -645,7 +683,7 @@ TEST_F(End2endTest, ClientCancelsResponseStream) {
// Client cancels bidi stream after sending some messages // Client cancels bidi stream after sending some messages
TEST_F(End2endTest, ClientCancelsBidi) { TEST_F(End2endTest, ClientCancelsBidi) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -677,7 +715,7 @@ TEST_F(End2endTest, ClientCancelsBidi) { ...@@ -677,7 +715,7 @@ TEST_F(End2endTest, ClientCancelsBidi) {
} }
TEST_F(End2endTest, RpcMaxMessageSize) { TEST_F(End2endTest, RpcMaxMessageSize) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message(string(kMaxMessageSize_ * 2, 'a')); request.set_message(string(kMaxMessageSize_ * 2, 'a'));
...@@ -702,7 +740,7 @@ bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata, ...@@ -702,7 +740,7 @@ bool MetadataContains(const std::multimap<grpc::string, grpc::string>& metadata,
} }
TEST_F(End2endTest, SetPerCallCredentials) { TEST_F(End2endTest, SetPerCallCredentials) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -724,7 +762,7 @@ TEST_F(End2endTest, SetPerCallCredentials) { ...@@ -724,7 +762,7 @@ TEST_F(End2endTest, SetPerCallCredentials) {
} }
TEST_F(End2endTest, InsecurePerCallCredentials) { TEST_F(End2endTest, InsecurePerCallCredentials) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -739,7 +777,7 @@ TEST_F(End2endTest, InsecurePerCallCredentials) { ...@@ -739,7 +777,7 @@ TEST_F(End2endTest, InsecurePerCallCredentials) {
} }
TEST_F(End2endTest, OverridePerCallCredentials) { TEST_F(End2endTest, OverridePerCallCredentials) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -772,7 +810,7 @@ TEST_F(End2endTest, OverridePerCallCredentials) { ...@@ -772,7 +810,7 @@ TEST_F(End2endTest, OverridePerCallCredentials) {
// Client sends 20 requests and the server returns CANCELLED status after // Client sends 20 requests and the server returns CANCELLED status after
// reading 10 requests. // reading 10 requests.
TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) { TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
ClientContext context; ClientContext context;
...@@ -791,7 +829,7 @@ TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) { ...@@ -791,7 +829,7 @@ TEST_F(End2endTest, RequestStreamServerEarlyCancelTest) {
} }
TEST_F(End2endTest, ClientAuthContext) { TEST_F(End2endTest, ClientAuthContext) {
ResetStub(); ResetStub(false);
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("Hello"); request.set_message("Hello");
...@@ -816,8 +854,8 @@ TEST_F(End2endTest, ClientAuthContext) { ...@@ -816,8 +854,8 @@ TEST_F(End2endTest, ClientAuthContext) {
} }
// Make the response larger than the flow control window. // Make the response larger than the flow control window.
TEST_F(End2endTest, HugeResponse) { TEST_P(End2endTest, HugeResponse) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("huge response"); request.set_message("huge response");
...@@ -842,7 +880,7 @@ void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream, gpr ...@@ -842,7 +880,7 @@ void ReaderThreadFunc(ClientReaderWriter<EchoRequest, EchoResponse>* stream, gpr
// Run a Read and a WritesDone simultaneously. // Run a Read and a WritesDone simultaneously.
TEST_F(End2endTest, SimultaneousReadWritesDone) { TEST_F(End2endTest, SimultaneousReadWritesDone) {
ResetStub(); ResetStub(false);
ClientContext context; ClientContext context;
gpr_event ev; gpr_event ev;
gpr_event_init(&ev); gpr_event_init(&ev);
...@@ -855,8 +893,8 @@ TEST_F(End2endTest, SimultaneousReadWritesDone) { ...@@ -855,8 +893,8 @@ TEST_F(End2endTest, SimultaneousReadWritesDone) {
reader_thread.join(); reader_thread.join();
} }
TEST_F(End2endTest, Peer) { TEST_P(End2endTest, Peer) {
ResetStub(); ResetStub(GetParam());
EchoRequest request; EchoRequest request;
EchoResponse response; EchoResponse response;
request.set_message("hello"); request.set_message("hello");
...@@ -870,6 +908,8 @@ TEST_F(End2endTest, Peer) { ...@@ -870,6 +908,8 @@ TEST_F(End2endTest, Peer) {
EXPECT_TRUE(CheckIsLocalhost(context.peer())); EXPECT_TRUE(CheckIsLocalhost(context.peer()));
} }
INSTANTIATE_TEST_CASE_P(End2end, End2endTest, ::testing::Values(false, true));
} // namespace testing } // namespace testing
} // namespace grpc } // namespace grpc
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment