blob: 59fec6ad40619ca07c6f64738009672fbf2d2ac8 [file] [log] [blame]
Craig Tillere50e5cb2015-08-18 12:44:57 -07001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tillere50e5cb2015-08-18 12:44:57 -070034#include <thread>
35
yang-g9e2f90c2015-08-21 15:35:03 -070036#include <grpc/grpc.h>
37#include <grpc/support/sync.h>
yang-gd392fa02015-08-20 13:55:29 -070038#include <grpc++/channel.h>
Craig Tillere50e5cb2015-08-18 12:44:57 -070039#include <grpc++/client_context.h>
40#include <grpc++/create_channel.h>
41#include <grpc++/credentials.h>
42#include <grpc++/server.h>
43#include <grpc++/server_builder.h>
44#include <grpc++/server_context.h>
45#include <grpc++/server_credentials.h>
Craig Tillere50e5cb2015-08-18 12:44:57 -070046#include <gtest/gtest.h>
yang-g9e2f90c2015-08-21 15:35:03 -070047
48#include "src/core/support/env.h"
49#include "test/core/util/test_config.h"
50#include "test/core/util/port.h"
51#include "test/cpp/util/echo.grpc.pb.h"
Craig Tillere50e5cb2015-08-18 12:44:57 -070052
53using grpc::cpp::test::util::EchoRequest;
54using grpc::cpp::test::util::EchoResponse;
55
56namespace grpc {
57namespace testing {
58
59class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
60 public:
61 explicit TestServiceImpl(gpr_event* ev) : ev_(ev) {}
62
63 Status Echo(ServerContext* context, const EchoRequest* request,
64 EchoResponse* response) GRPC_OVERRIDE {
65 gpr_event_set(ev_, (void*)1);
66 while (!context->IsCancelled()) {
67 }
68 return Status::OK;
69 }
70
71 private:
72 gpr_event* ev_;
73};
74
75class ShutdownTest : public ::testing::Test {
76 public:
77 ShutdownTest() : shutdown_(false), service_(&ev_) { gpr_event_init(&ev_); }
78
79 void SetUp() GRPC_OVERRIDE {
80 port_ = grpc_pick_unused_port_or_die();
81 server_ = SetUpServer(port_);
82 }
83
84 std::unique_ptr<Server> SetUpServer(const int port) {
85 grpc::string server_address = "localhost:" + to_string(port);
86
87 ServerBuilder builder;
88 builder.AddListeningPort(server_address, InsecureServerCredentials());
89 builder.RegisterService(&service_);
90 std::unique_ptr<Server> server = builder.BuildAndStart();
91 return server;
92 }
93
94 void TearDown() GRPC_OVERRIDE { GPR_ASSERT(shutdown_); }
95
96 void ResetStub() {
97 string target = "dns:localhost:" + to_string(port_);
yang-g730055d2015-08-27 12:29:45 -070098 channel_ = CreateChannel(target, InsecureCredentials());
Craig Tillere50e5cb2015-08-18 12:44:57 -070099 stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel_));
100 }
101
102 string to_string(const int number) {
103 std::stringstream strs;
104 strs << number;
105 return strs.str();
106 }
107
108 void SendRequest() {
109 EchoRequest request;
110 EchoResponse response;
111 request.set_message("Hello");
112 ClientContext context;
113 GPR_ASSERT(!shutdown_);
114 Status s = stub_->Echo(&context, request, &response);
115 GPR_ASSERT(shutdown_);
116 }
117
118 protected:
yang-gd392fa02015-08-20 13:55:29 -0700119 std::shared_ptr<Channel> channel_;
Craig Tillere50e5cb2015-08-18 12:44:57 -0700120 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
121 std::unique_ptr<Server> server_;
122 bool shutdown_;
123 int port_;
124 gpr_event ev_;
125 TestServiceImpl service_;
126};
127
128// Tests zookeeper state change between two RPCs
129// TODO(ctiller): leaked objects in this test
130TEST_F(ShutdownTest, ShutdownTest) {
131 ResetStub();
132
133 // send the request in a background thread
134 std::thread thr(std::bind(&ShutdownTest::SendRequest, this));
135
136 // wait for the server to get the event
137 gpr_event_wait(&ev_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
138
139 shutdown_ = true;
140
141 // shutdown should trigger cancellation causing everything to shutdown
142 auto deadline =
143 std::chrono::system_clock::now() + std::chrono::microseconds(100);
144 server_->Shutdown(deadline);
145 EXPECT_GE(std::chrono::system_clock::now(), deadline);
146
147 thr.join();
148}
149
150} // namespace testing
151} // namespace grpc
152
153int main(int argc, char** argv) {
154 grpc_test_init(argc, argv);
155 ::testing::InitGoogleTest(&argc, argv);
156 return RUN_ALL_TESTS();
157}