aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/cpp')
-rw-r--r--test/cpp/end2end/async_test_server.cc155
-rw-r--r--test/cpp/end2end/async_test_server.h75
-rw-r--r--test/cpp/end2end/end2end_test.cc125
-rw-r--r--test/cpp/end2end/sync_client_async_server_test.cc237
-rw-r--r--test/cpp/server/thread_pool_test.cc77
-rw-r--r--test/cpp/util/echo.proto15
-rw-r--r--test/cpp/util/status_test.cc77
-rw-r--r--test/cpp/util/test_ssl_channel.cc59
-rw-r--r--test/cpp/util/test_ssl_channel.h55
-rw-r--r--test/cpp/util/time_test.cc68
10 files changed, 943 insertions, 0 deletions
diff --git a/test/cpp/end2end/async_test_server.cc b/test/cpp/end2end/async_test_server.cc
new file mode 100644
index 0000000000..0a40dbbbd9
--- /dev/null
+++ b/test/cpp/end2end/async_test_server.cc
@@ -0,0 +1,155 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "test/cpp/end2end/async_test_server.h"
+
+#include <chrono>
+
+#include <grpc/support/log.h>
+#include "src/cpp/proto/proto_utils.h"
+#include "test/cpp/util/echo.pb.h"
+#include <grpc++/async_server.h>
+#include <grpc++/async_server_context.h>
+#include <grpc++/completion_queue.h>
+#include <grpc++/status.h>
+#include <gtest/gtest.h>
+
+using grpc::cpp::test::util::EchoRequest;
+using grpc::cpp::test::util::EchoResponse;
+
+using std::chrono::duration_cast;
+using std::chrono::microseconds;
+using std::chrono::seconds;
+using std::chrono::system_clock;
+
+namespace grpc {
+namespace testing {
+
+AsyncTestServer::AsyncTestServer() : server_(&cq_), cq_drained_(false) {}
+
+AsyncTestServer::~AsyncTestServer() {}
+
+void AsyncTestServer::AddPort(const grpc::string& addr) {
+ server_.AddPort(addr);
+}
+
+void AsyncTestServer::Start() { server_.Start(); }
+
+// Return true if deadline actual is within 0.5s from expected.
+bool DeadlineMatched(const system_clock::time_point& actual,
+ const system_clock::time_point& expected) {
+ microseconds diff_usecs = duration_cast<microseconds>(expected - actual);
+ gpr_log(GPR_INFO, "diff_usecs= %d", diff_usecs.count());
+ return diff_usecs.count() < 500000 && diff_usecs.count() > -500000;
+}
+
+void AsyncTestServer::RequestOneRpc() { server_.RequestOneRpc(); }
+
+void AsyncTestServer::MainLoop() {
+ EchoRequest request;
+ EchoResponse response;
+ void* tag = nullptr;
+
+ RequestOneRpc();
+
+ while (true) {
+ CompletionQueue::CompletionType t = cq_.Next(&tag);
+ AsyncServerContext* server_context = static_cast<AsyncServerContext*>(tag);
+ switch (t) {
+ case CompletionQueue::SERVER_RPC_NEW:
+ gpr_log(GPR_INFO, "SERVER_RPC_NEW %p", server_context);
+ if (server_context) {
+ EXPECT_EQ(server_context->method(), "/foo");
+ EXPECT_EQ(server_context->host(), "localhost");
+ // TODO(ctiller): verify deadline
+ server_context->Accept(cq_.cq());
+ // Handle only one rpc at a time.
+ RequestOneRpc();
+ server_context->StartRead(&request);
+ }
+ break;
+ case CompletionQueue::RPC_END:
+ gpr_log(GPR_INFO, "RPC_END %p", server_context);
+ delete server_context;
+ break;
+ case CompletionQueue::SERVER_READ_OK:
+ gpr_log(GPR_INFO, "SERVER_READ_OK %p", server_context);
+ response.set_message(request.message());
+ server_context->StartWrite(response, 0);
+ break;
+ case CompletionQueue::SERVER_READ_ERROR:
+ gpr_log(GPR_INFO, "SERVER_READ_ERROR %p", server_context);
+ server_context->StartWriteStatus(Status::OK);
+ break;
+ case CompletionQueue::HALFCLOSE_OK:
+ gpr_log(GPR_INFO, "HALFCLOSE_OK %p", server_context);
+ // Do nothing, just wait for RPC_END.
+ break;
+ case CompletionQueue::SERVER_WRITE_OK:
+ gpr_log(GPR_INFO, "SERVER_WRITE_OK %p", server_context);
+ server_context->StartRead(&request);
+ break;
+ case CompletionQueue::SERVER_WRITE_ERROR:
+ EXPECT_TRUE(0);
+ break;
+ case CompletionQueue::QUEUE_CLOSED: {
+ gpr_log(GPR_INFO, "QUEUE_CLOSED");
+ HandleQueueClosed();
+ return;
+ }
+ default:
+ EXPECT_TRUE(0);
+ break;
+ }
+ }
+}
+
+void AsyncTestServer::HandleQueueClosed() {
+ std::unique_lock<std::mutex> lock(cq_drained_mu_);
+ cq_drained_ = true;
+ cq_drained_cv_.notify_all();
+}
+
+void AsyncTestServer::Shutdown() {
+ // The server need to be shut down before cq_ as grpc_server flushes all
+ // pending requested calls to the completion queue at shutdown.
+ server_.Shutdown();
+ cq_.Shutdown();
+ std::unique_lock<std::mutex> lock(cq_drained_mu_);
+ while (!cq_drained_) {
+ cq_drained_cv_.wait(lock);
+ }
+}
+
+} // namespace testing
+} // namespace grpc
diff --git a/test/cpp/end2end/async_test_server.h b/test/cpp/end2end/async_test_server.h
new file mode 100644
index 0000000000..a277061ace
--- /dev/null
+++ b/test/cpp/end2end/async_test_server.h
@@ -0,0 +1,75 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef __GRPCPP_TEST_END2END_ASYNC_TEST_SERVER_H__
+#define __GRPCPP_TEST_END2END_ASYNC_TEST_SERVER_H__
+
+#include <condition_variable>
+#include <mutex>
+#include <string>
+
+#include <grpc++/async_server.h>
+#include <grpc++/completion_queue.h>
+
+namespace grpc {
+
+namespace testing {
+
+class AsyncTestServer {
+ public:
+ AsyncTestServer();
+ virtual ~AsyncTestServer();
+
+ void AddPort(const grpc::string& addr);
+ void Start();
+ void RequestOneRpc();
+ virtual void MainLoop();
+ void Shutdown();
+
+ CompletionQueue* completion_queue() { return &cq_; }
+
+ protected:
+ void HandleQueueClosed();
+
+ private:
+ CompletionQueue cq_;
+ AsyncServer server_;
+ bool cq_drained_;
+ std::mutex cq_drained_mu_;
+ std::condition_variable cq_drained_cv_;
+};
+
+} // namespace testing
+} // namespace grpc
+
+#endif // __GRPCPP_TEST_END2END_ASYNC_TEST_SERVER_H__
diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc
new file mode 100644
index 0000000000..255e29e409
--- /dev/null
+++ b/test/cpp/end2end/end2end_test.cc
@@ -0,0 +1,125 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <thread>
+#include "src/cpp/server/rpc_service_method.h"
+#include "test/cpp/util/echo.pb.h"
+#include "net/util/netutil.h"
+#include <grpc++/channel_interface.h>
+#include <grpc++/client_context.h>
+#include <grpc++/create_channel.h>
+#include <grpc++/server.h>
+#include <grpc++/server_builder.h>
+#include <grpc++/status.h>
+#include <gtest/gtest.h>
+
+#include <grpc/grpc.h>
+#include <grpc/support/thd.h>
+
+using grpc::cpp::test::util::EchoRequest;
+using grpc::cpp::test::util::EchoResponse;
+using grpc::cpp::test::util::TestService;
+
+namespace grpc {
+
+class TestServiceImpl : public TestService::Service {
+ public:
+ Status Echo(const EchoRequest* request, EchoResponse* response) {
+ response->set_message(request->message());
+ return Status::OK;
+ }
+};
+
+class End2endTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ int port = PickUnusedPortOrDie();
+ server_address_ << "localhost:" << port;
+ // Setup server
+ ServerBuilder builder;
+ builder.AddPort(server_address_.str());
+ builder.RegisterService(service.service());
+ server_ = builder.BuildAndStart();
+ }
+
+ void TearDown() override {
+ server_->Shutdown();
+ }
+
+ std::unique_ptr<Server> server_;
+ std::ostringstream server_address_;
+ TestServiceImpl service;
+};
+
+static void SendRpc(const grpc::string& server_address, int num_rpcs) {
+ std::shared_ptr<ChannelInterface> channel =
+ CreateChannel(server_address);
+ TestService::Stub* stub = TestService::NewStub(channel);
+ EchoRequest request;
+ EchoResponse response;
+ request.set_message("Hello");
+
+ for (int i = 0; i < num_rpcs; ++i) {
+ ClientContext context;
+ Status s = stub->Echo(&context, request, &response);
+ EXPECT_EQ(response.message(), request.message());
+ EXPECT_TRUE(s.IsOk());
+ }
+
+ delete stub;
+}
+
+TEST_F(End2endTest, SimpleRpc) {
+ SendRpc(server_address_.str(), 1);
+}
+
+TEST_F(End2endTest, MultipleRpcs) {
+ vector<std::thread*> threads;
+ for (int i = 0; i < 10; ++i) {
+ threads.push_back(new std::thread(SendRpc, server_address_.str(), 10));
+ }
+ for (int i = 0; i < 10; ++i) {
+ threads[i]->join();
+ delete threads[i];
+ }
+}
+
+} // namespace grpc
+
+int main(int argc, char** argv) {
+ grpc_init();
+ ::testing::InitGoogleTest(&argc, argv);
+ int result = RUN_ALL_TESTS();
+ grpc_shutdown();
+ return result;
+}
diff --git a/test/cpp/end2end/sync_client_async_server_test.cc b/test/cpp/end2end/sync_client_async_server_test.cc
new file mode 100644
index 0000000000..f9ac6f2ea5
--- /dev/null
+++ b/test/cpp/end2end/sync_client_async_server_test.cc
@@ -0,0 +1,237 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#include <chrono>
+#include <memory>
+#include <sstream>
+#include <string>
+
+#include <grpc/grpc.h>
+#include <grpc/support/thd.h>
+#include "src/cpp/client/internal_stub.h"
+#include "src/cpp/rpc_method.h"
+#include "test/cpp/util/echo.pb.h"
+#include "net/util/netutil.h"
+#include <grpc++/channel_interface.h>
+#include <grpc++/client_context.h>
+#include <grpc++/create_channel.h>
+#include <grpc++/status.h>
+#include <grpc++/stream.h>
+#include "test/cpp/end2end/async_test_server.h"
+#include <gtest/gtest.h>
+
+using grpc::cpp::test::util::EchoRequest;
+using grpc::cpp::test::util::EchoResponse;
+
+using std::chrono::duration_cast;
+using std::chrono::microseconds;
+using std::chrono::seconds;
+using std::chrono::system_clock;
+
+using grpc::testing::AsyncTestServer;
+
+namespace grpc {
+namespace {
+
+void ServerLoop(void* s) {
+ AsyncTestServer* server = static_cast<AsyncTestServer*>(s);
+ server->MainLoop();
+}
+
+class End2endTest : public ::testing::Test {
+ protected:
+ void SetUp() override {
+ int port = PickUnusedPortOrDie();
+ // TODO(yangg) protobuf has a StringPrintf, maybe use that
+ std::ostringstream oss;
+ oss << "[::]:" << port;
+ // Setup server
+ server_.reset(new AsyncTestServer());
+ server_->AddPort(oss.str());
+ server_->Start();
+
+ RunServerThread();
+
+ // Setup client
+ oss.str("");
+ oss << "127.0.0.1:" << port;
+ std::shared_ptr<ChannelInterface> channel = CreateChannel(oss.str());
+ stub_.set_channel(channel);
+ }
+
+ void RunServerThread() {
+ gpr_thd_id id;
+ EXPECT_TRUE(gpr_thd_new(&id, ServerLoop, server_.get(), NULL));
+ }
+
+ void TearDown() override {
+ server_->Shutdown();
+ }
+
+ std::unique_ptr<AsyncTestServer> server_;
+ InternalStub stub_;
+};
+
+TEST_F(End2endTest, NoOpTest) { EXPECT_TRUE(stub_.channel() != nullptr); }
+
+TEST_F(End2endTest, SimpleRpc) {
+ EchoRequest request;
+ request.set_message("hello");
+ EchoResponse result;
+ ClientContext context;
+ RpcMethod method("/foo");
+ std::chrono::system_clock::time_point deadline =
+ std::chrono::system_clock::now() + std::chrono::seconds(10);
+ context.set_absolute_deadline(deadline);
+ Status s =
+ stub_.channel()->StartBlockingRpc(method, &context, request, &result);
+ EXPECT_EQ(result.message(), request.message());
+ EXPECT_TRUE(s.IsOk());
+}
+
+TEST_F(End2endTest, KSequentialSimpleRpcs) {
+ int k = 3;
+ for (int i = 0; i < k; i++) {
+ EchoRequest request;
+ request.set_message("hello");
+ EchoResponse result;
+ ClientContext context;
+ RpcMethod method("/foo");
+ std::chrono::system_clock::time_point deadline =
+ std::chrono::system_clock::now() + std::chrono::seconds(10);
+ context.set_absolute_deadline(deadline);
+ Status s =
+ stub_.channel()->StartBlockingRpc(method, &context, request, &result);
+ EXPECT_EQ(result.message(), request.message());
+ EXPECT_TRUE(s.IsOk());
+ }
+}
+
+TEST_F(End2endTest, OnePingpongBidiStream) {
+ EchoRequest request;
+ request.set_message("hello");
+ EchoResponse result;
+ ClientContext context;
+ RpcMethod method("/foo", RpcMethod::RpcType::BIDI_STREAMING);
+ std::chrono::system_clock::time_point deadline =
+ std::chrono::system_clock::now() + std::chrono::seconds(10);
+ context.set_absolute_deadline(deadline);
+ StreamContextInterface* stream_interface =
+ stub_.channel()->CreateStream(method, &context, nullptr, nullptr);
+ std::unique_ptr<ClientReaderWriter<EchoRequest, EchoResponse>> stream(
+ new ClientReaderWriter<EchoRequest, EchoResponse>(stream_interface));
+ EXPECT_TRUE(stream->Write(request));
+ EXPECT_TRUE(stream->Read(&result));
+ stream->WritesDone();
+ EXPECT_FALSE(stream->Read(&result));
+ Status s = stream->Wait();
+ EXPECT_EQ(result.message(), request.message());
+ EXPECT_TRUE(s.IsOk());
+}
+
+TEST_F(End2endTest, TwoPingpongBidiStream) {
+ EchoRequest request;
+ request.set_message("hello");
+ EchoResponse result;
+ ClientContext context;
+ RpcMethod method("/foo", RpcMethod::RpcType::BIDI_STREAMING);
+ std::chrono::system_clock::time_point deadline =
+ std::chrono::system_clock::now() + std::chrono::seconds(10);
+ context.set_absolute_deadline(deadline);
+ StreamContextInterface* stream_interface =
+ stub_.channel()->CreateStream(method, &context, nullptr, nullptr);
+ std::unique_ptr<ClientReaderWriter<EchoRequest, EchoResponse>> stream(
+ new ClientReaderWriter<EchoRequest, EchoResponse>(stream_interface));
+ EXPECT_TRUE(stream->Write(request));
+ EXPECT_TRUE(stream->Read(&result));
+ EXPECT_EQ(result.message(), request.message());
+ EXPECT_TRUE(stream->Write(request));
+ EXPECT_TRUE(stream->Read(&result));
+ EXPECT_EQ(result.message(), request.message());
+ stream->WritesDone();
+ EXPECT_FALSE(stream->Read(&result));
+ Status s = stream->Wait();
+ EXPECT_TRUE(s.IsOk());
+}
+
+TEST_F(End2endTest, OnePingpongClientStream) {
+ EchoRequest request;
+ request.set_message("hello");
+ EchoResponse result;
+ ClientContext context;
+ RpcMethod method("/foo", RpcMethod::RpcType::CLIENT_STREAMING);
+ std::chrono::system_clock::time_point deadline =
+ std::chrono::system_clock::now() + std::chrono::seconds(10);
+ context.set_absolute_deadline(deadline);
+ StreamContextInterface* stream_interface =
+ stub_.channel()->CreateStream(method, &context, nullptr, &result);
+ std::unique_ptr<ClientWriter<EchoRequest>> stream(
+ new ClientWriter<EchoRequest>(stream_interface));
+ EXPECT_TRUE(stream->Write(request));
+ stream->WritesDone();
+ Status s = stream->Wait();
+ EXPECT_EQ(result.message(), request.message());
+ EXPECT_TRUE(s.IsOk());
+}
+
+TEST_F(End2endTest, OnePingpongServerStream) {
+ EchoRequest request;
+ request.set_message("hello");
+ EchoResponse result;
+ ClientContext context;
+ RpcMethod method("/foo", RpcMethod::RpcType::SERVER_STREAMING);
+ std::chrono::system_clock::time_point deadline =
+ std::chrono::system_clock::now() + std::chrono::seconds(10);
+ context.set_absolute_deadline(deadline);
+ StreamContextInterface* stream_interface =
+ stub_.channel()->CreateStream(method, &context, &request, nullptr);
+ std::unique_ptr<ClientReader<EchoResponse>> stream(
+ new ClientReader<EchoResponse>(stream_interface));
+ EXPECT_TRUE(stream->Read(&result));
+ EXPECT_FALSE(stream->Read(nullptr));
+ Status s = stream->Wait();
+ EXPECT_EQ(result.message(), request.message());
+ EXPECT_TRUE(s.IsOk());
+}
+
+} // namespace
+} // namespace grpc
+
+int main(int argc, char** argv) {
+ grpc_init();
+ ::testing::InitGoogleTest(&argc, argv);
+ int result = RUN_ALL_TESTS();
+ grpc_shutdown();
+ return result;
+}
diff --git a/test/cpp/server/thread_pool_test.cc b/test/cpp/server/thread_pool_test.cc
new file mode 100644
index 0000000000..cae1a105c9
--- /dev/null
+++ b/test/cpp/server/thread_pool_test.cc
@@ -0,0 +1,77 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <condition_variable>
+#include <functional>
+#include <mutex>
+
+#include "src/cpp/server/thread_pool.h"
+#include <gtest/gtest.h>
+
+namespace grpc {
+
+class ThreadPoolTest : public ::testing::Test {
+ public:
+ ThreadPoolTest() : thread_pool_(4) {}
+
+ protected:
+ ThreadPool thread_pool_;
+};
+
+void Callback(std::mutex* mu, std::condition_variable* cv, bool* done) {
+ std::unique_lock<std::mutex> lock(*mu);
+ *done = true;
+ cv->notify_all();
+}
+
+TEST_F(ThreadPoolTest, ScheduleCallback) {
+ std::mutex mu;
+ std::condition_variable cv;
+ bool done = false;
+ std::function<void()> callback = std::bind(Callback, &mu, &cv, &done);
+ thread_pool_.ScheduleCallback(callback);
+
+ // Wait for the callback to finish.
+ std::unique_lock<std::mutex> lock(mu);
+ while (!done) {
+ cv.wait(lock);
+ }
+}
+
+} // namespace grpc
+
+int main(int argc, char** argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ int result = RUN_ALL_TESTS();
+ return result;
+}
diff --git a/test/cpp/util/echo.proto b/test/cpp/util/echo.proto
new file mode 100644
index 0000000000..37ae6b9a39
--- /dev/null
+++ b/test/cpp/util/echo.proto
@@ -0,0 +1,15 @@
+syntax = "proto2";
+
+package grpc.cpp.test.util;
+
+message EchoRequest {
+ optional string message = 1;
+}
+
+message EchoResponse {
+ optional string message = 1;
+}
+
+service TestService {
+ rpc Echo(EchoRequest) returns (EchoResponse) {}
+}
diff --git a/test/cpp/util/status_test.cc b/test/cpp/util/status_test.cc
new file mode 100644
index 0000000000..1f371671db
--- /dev/null
+++ b/test/cpp/util/status_test.cc
@@ -0,0 +1,77 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#include <grpc++/status.h>
+#include <grpc/status.h>
+#include <grpc/support/log.h>
+
+// Make sure the existing grpc_status_code match with grpc::Code.
+int main(int argc, char **argv) {
+ GPR_ASSERT(grpc::StatusCode::OK ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_OK));
+ GPR_ASSERT(grpc::StatusCode::CANCELLED ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_CANCELLED));
+ GPR_ASSERT(grpc::StatusCode::UNKNOWN ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_UNKNOWN));
+ GPR_ASSERT(grpc::StatusCode::INVALID_ARGUMENT ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_INVALID_ARGUMENT));
+ GPR_ASSERT(grpc::StatusCode::DEADLINE_EXCEEDED ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_DEADLINE_EXCEEDED));
+ GPR_ASSERT(grpc::StatusCode::NOT_FOUND ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_NOT_FOUND));
+ GPR_ASSERT(grpc::StatusCode::ALREADY_EXISTS ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_ALREADY_EXISTS));
+ GPR_ASSERT(grpc::StatusCode::PERMISSION_DENIED ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_PERMISSION_DENIED));
+ GPR_ASSERT(grpc::StatusCode::UNAUTHENTICATED ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_UNAUTHENTICATED));
+ GPR_ASSERT(grpc::StatusCode::RESOURCE_EXHAUSTED ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_RESOURCE_EXHAUSTED));
+ GPR_ASSERT(grpc::StatusCode::FAILED_PRECONDITION ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_FAILED_PRECONDITION));
+ GPR_ASSERT(grpc::StatusCode::ABORTED ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_ABORTED));
+ GPR_ASSERT(grpc::StatusCode::OUT_OF_RANGE ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_OUT_OF_RANGE));
+ GPR_ASSERT(grpc::StatusCode::UNIMPLEMENTED ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_UNIMPLEMENTED));
+ GPR_ASSERT(grpc::StatusCode::INTERNAL ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_INTERNAL));
+ GPR_ASSERT(grpc::StatusCode::UNAVAILABLE ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_UNAVAILABLE));
+ GPR_ASSERT(grpc::StatusCode::DATA_LOSS ==
+ static_cast<grpc::StatusCode>(GRPC_STATUS_DATA_LOSS));
+
+ return 0;
+}
diff --git a/test/cpp/util/test_ssl_channel.cc b/test/cpp/util/test_ssl_channel.cc
new file mode 100644
index 0000000000..bf4a2d6e31
--- /dev/null
+++ b/test/cpp/util/test_ssl_channel.cc
@@ -0,0 +1,59 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "test/cpp/util/test_ssl_channel.h"
+
+#include <grpc/support/log.h>
+extern "C" {
+#include "src/core/channel/channel_args.h"
+#include <grpc/grpc_security.h>
+}
+
+#include "test/core/end2end/data/ssl_test_data.h"
+
+namespace grpc {
+
+TestSslChannel::TestSslChannel(const grpc::string& target) {
+ grpc_credentials* ssl_creds = grpc_ssl_credentials_create(
+ test_ca_cert, test_ca_cert_size, NULL, 0, NULL, 0);
+ grpc_arg ssl_name_override = {
+ GRPC_ARG_STRING,
+ const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
+ {const_cast<char*>("foo.test.google.com")}};
+ grpc_channel_args client_args = {1, &ssl_name_override};
+ set_c_channel(
+ grpc_secure_channel_create(ssl_creds, target.c_str(), &client_args));
+ grpc_credentials_release(ssl_creds);
+}
+
+} // namespace grpc
diff --git a/test/cpp/util/test_ssl_channel.h b/test/cpp/util/test_ssl_channel.h
new file mode 100644
index 0000000000..28905dc3ab
--- /dev/null
+++ b/test/cpp/util/test_ssl_channel.h
@@ -0,0 +1,55 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef __GRPCPP_TEST_UTIL_TEST_SSL_CHANNEL_H__
+#define __GRPCPP_TEST_UTIL_TEST_SSL_CHANNEL_H__
+
+#include <string>
+
+#include "src/cpp/client/channel.h"
+
+struct grpc_channel;
+
+namespace grpc {
+class StreamContextInterface;
+
+// The channel is used to test against test gfe or interop binaries with ssl
+// support.
+class TestSslChannel : public Channel {
+ public:
+ explicit TestSslChannel(const grpc::string& target);
+};
+
+} // namespace grpc
+
+#endif // __GRPCPP_TEST_UTIL_TEST_SSL_CHANNEL_H__
diff --git a/test/cpp/util/time_test.cc b/test/cpp/util/time_test.cc
new file mode 100644
index 0000000000..4c633f34f5
--- /dev/null
+++ b/test/cpp/util/time_test.cc
@@ -0,0 +1,68 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#include "src/cpp/util/time.h"
+
+#include <chrono>
+
+#include <grpc/support/time.h>
+#include <gtest/gtest.h>
+
+using std::chrono::duration_cast;
+using std::chrono::microseconds;
+using std::chrono::system_clock;
+
+namespace grpc {
+namespace {
+
+class TimeTest : public ::testing::Test {};
+
+TEST_F(TimeTest, AbsolutePointTest) {
+ long us = 10000000L;
+ gpr_timespec ts = gpr_time_from_micros(us);
+ system_clock::time_point tp{microseconds(us)};
+ system_clock::time_point tp_converted =
+ AbsoluteDeadlineTimespec2Timepoint(ts);
+ gpr_timespec ts_converted;
+ AbsoluteDeadlineTimepoint2Timespec(tp_converted, &ts_converted);
+ EXPECT_TRUE(ts.tv_sec == ts_converted.tv_sec);
+ EXPECT_TRUE(ts.tv_nsec == ts_converted.tv_nsec);
+ system_clock::time_point tp_converted_2 =
+ AbsoluteDeadlineTimespec2Timepoint(ts_converted);
+ EXPECT_TRUE(tp == tp_converted);
+ EXPECT_TRUE(tp == tp_converted_2);
+}
+
+} // namespace
+} // namespace grpc