blob: 5b52b1fc1aa08bf848f6f86f8a327e7590b2e10d [file] [log] [blame]
Craig Tillere50e5cb2015-08-18 12:44:57 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tillere50e5cb2015-08-18 12:44:57 -07004 * 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
David Garcia Quintasc79b0652016-07-27 21:11:58 -070036#include <gtest/gtest.h>
37
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>
Craig Tillere50e5cb2015-08-18 12:44:57 -070041#include <grpc++/server.h>
42#include <grpc++/server_builder.h>
43#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080044#include <grpc/grpc.h>
David Garcia Quintasc79b0652016-07-27 21:11:58 -070045#include <grpc/support/log.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080046#include <grpc/support/sync.h>
yang-g9e2f90c2015-08-21 15:35:03 -070047
Craig Tiller9533d042016-03-25 17:11:06 -070048#include "src/core/lib/support/env.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080049#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080050#include "test/core/util/port.h"
51#include "test/core/util/test_config.h"
Craig Tillere50e5cb2015-08-18 12:44:57 -070052
Craig Tiller1b4e3302015-12-17 16:35:00 -080053using grpc::testing::EchoRequest;
54using grpc::testing::EchoResponse;
Craig Tillere50e5cb2015-08-18 12:44:57 -070055
56namespace grpc {
57namespace testing {
58
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080059class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Craig Tillere50e5cb2015-08-18 12:44:57 -070060 public:
61 explicit TestServiceImpl(gpr_event* ev) : ev_(ev) {}
62
63 Status Echo(ServerContext* context, const EchoRequest* request,
Vijay Paic0b2acb2016-11-01 16:31:56 -070064 EchoResponse* response) override {
Craig Tillere50e5cb2015-08-18 12:44:57 -070065 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
Vijay Paic0b2acb2016-11-01 16:31:56 -070079 void SetUp() override {
Craig Tillere50e5cb2015-08-18 12:44:57 -070080 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
Vijay Paic0b2acb2016-11-01 16:31:56 -070094 void TearDown() override { GPR_ASSERT(shutdown_); }
Craig Tillere50e5cb2015-08-18 12:44:57 -070095
96 void ResetStub() {
97 string target = "dns:localhost:" + to_string(port_);
Julien Boeufe5adc0e2015-10-12 14:08:10 -070098 channel_ = CreateChannel(target, InsecureChannelCredentials());
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080099 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
Craig Tillere50e5cb2015-08-18 12:44:57 -0700100 }
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_;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800120 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Craig Tillere50e5cb2015-08-18 12:44:57 -0700121 std::unique_ptr<Server> server_;
122 bool shutdown_;
123 int port_;
124 gpr_event ev_;
125 TestServiceImpl service_;
126};
127
Craig Tillere50e5cb2015-08-18 12:44:57 -0700128// TODO(ctiller): leaked objects in this test
129TEST_F(ShutdownTest, ShutdownTest) {
130 ResetStub();
131
132 // send the request in a background thread
133 std::thread thr(std::bind(&ShutdownTest::SendRequest, this));
134
135 // wait for the server to get the event
136 gpr_event_wait(&ev_, gpr_inf_future(GPR_CLOCK_MONOTONIC));
137
138 shutdown_ = true;
139
140 // shutdown should trigger cancellation causing everything to shutdown
141 auto deadline =
142 std::chrono::system_clock::now() + std::chrono::microseconds(100);
143 server_->Shutdown(deadline);
144 EXPECT_GE(std::chrono::system_clock::now(), deadline);
145
146 thr.join();
147}
148
149} // namespace testing
150} // namespace grpc
151
152int main(int argc, char** argv) {
153 grpc_test_init(argc, argv);
154 ::testing::InitGoogleTest(&argc, argv);
155 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800156}