blob: 992ee980139406746cf97b029bc56c58f155a0a5 [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-gd392fa02015-08-20 13:55:29 -070036#include <grpc++/channel.h>
Craig Tillere50e5cb2015-08-18 12:44:57 -070037#include <grpc++/client_context.h>
38#include <grpc++/create_channel.h>
Craig Tillere50e5cb2015-08-18 12:44:57 -070039#include <grpc++/server.h>
40#include <grpc++/server_builder.h>
41#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080042#include <grpc/grpc.h>
43#include <grpc/support/sync.h>
Craig Tillere50e5cb2015-08-18 12:44:57 -070044#include <gtest/gtest.h>
yang-g9e2f90c2015-08-21 15:35:03 -070045
46#include "src/core/support/env.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080047#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080048#include "test/core/util/port.h"
49#include "test/core/util/test_config.h"
Craig Tillere50e5cb2015-08-18 12:44:57 -070050
Craig Tiller1b4e3302015-12-17 16:35:00 -080051using grpc::testing::EchoRequest;
52using grpc::testing::EchoResponse;
Craig Tillere50e5cb2015-08-18 12:44:57 -070053
54namespace grpc {
55namespace testing {
56
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080057class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Craig Tillere50e5cb2015-08-18 12:44:57 -070058 public:
59 explicit TestServiceImpl(gpr_event* ev) : ev_(ev) {}
60
61 Status Echo(ServerContext* context, const EchoRequest* request,
62 EchoResponse* response) GRPC_OVERRIDE {
63 gpr_event_set(ev_, (void*)1);
64 while (!context->IsCancelled()) {
65 }
66 return Status::OK;
67 }
68
69 private:
70 gpr_event* ev_;
71};
72
73class ShutdownTest : public ::testing::Test {
74 public:
75 ShutdownTest() : shutdown_(false), service_(&ev_) { gpr_event_init(&ev_); }
76
77 void SetUp() GRPC_OVERRIDE {
78 port_ = grpc_pick_unused_port_or_die();
79 server_ = SetUpServer(port_);
80 }
81
82 std::unique_ptr<Server> SetUpServer(const int port) {
83 grpc::string server_address = "localhost:" + to_string(port);
84
85 ServerBuilder builder;
86 builder.AddListeningPort(server_address, InsecureServerCredentials());
87 builder.RegisterService(&service_);
88 std::unique_ptr<Server> server = builder.BuildAndStart();
89 return server;
90 }
91
92 void TearDown() GRPC_OVERRIDE { GPR_ASSERT(shutdown_); }
93
94 void ResetStub() {
95 string target = "dns:localhost:" + to_string(port_);
Julien Boeufe5adc0e2015-10-12 14:08:10 -070096 channel_ = CreateChannel(target, InsecureChannelCredentials());
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080097 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
Craig Tillere50e5cb2015-08-18 12:44:57 -070098 }
99
100 string to_string(const int number) {
101 std::stringstream strs;
102 strs << number;
103 return strs.str();
104 }
105
106 void SendRequest() {
107 EchoRequest request;
108 EchoResponse response;
109 request.set_message("Hello");
110 ClientContext context;
111 GPR_ASSERT(!shutdown_);
112 Status s = stub_->Echo(&context, request, &response);
113 GPR_ASSERT(shutdown_);
114 }
115
116 protected:
yang-gd392fa02015-08-20 13:55:29 -0700117 std::shared_ptr<Channel> channel_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800118 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Craig Tillere50e5cb2015-08-18 12:44:57 -0700119 std::unique_ptr<Server> server_;
120 bool shutdown_;
121 int port_;
122 gpr_event ev_;
123 TestServiceImpl service_;
124};
125
126// Tests zookeeper state change between two RPCs
127// TODO(ctiller): leaked objects in this test
128TEST_F(ShutdownTest, ShutdownTest) {
129 ResetStub();
130
131 // send the request in a background thread
132 std::thread thr(std::bind(&ShutdownTest::SendRequest, this));
133
134 // wait for the server to get the event
135 gpr_event_wait(&ev_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
136
137 shutdown_ = true;
138
139 // shutdown should trigger cancellation causing everything to shutdown
140 auto deadline =
141 std::chrono::system_clock::now() + std::chrono::microseconds(100);
142 server_->Shutdown(deadline);
143 EXPECT_GE(std::chrono::system_clock::now(), deadline);
144
145 thr.join();
146}
147
148} // namespace testing
149} // namespace grpc
150
151int main(int argc, char** argv) {
152 grpc_test_init(argc, argv);
153 ::testing::InitGoogleTest(&argc, argv);
154 return RUN_ALL_TESTS();
155}