OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 |
| 34 #include <thread> |
| 35 |
| 36 #include <grpc++/channel.h> |
| 37 #include <grpc++/client_context.h> |
| 38 #include <grpc++/create_channel.h> |
| 39 #include <grpc++/server.h> |
| 40 #include <grpc++/server_builder.h> |
| 41 #include <grpc++/server_context.h> |
| 42 #include <grpc/grpc.h> |
| 43 #include <grpc/support/sync.h> |
| 44 #include <gtest/gtest.h> |
| 45 |
| 46 #include "src/core/support/env.h" |
| 47 #include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 48 #include "test/core/util/port.h" |
| 49 #include "test/core/util/test_config.h" |
| 50 |
| 51 using grpc::testing::EchoRequest; |
| 52 using grpc::testing::EchoResponse; |
| 53 |
| 54 namespace grpc { |
| 55 namespace testing { |
| 56 |
| 57 class TestServiceImpl : public ::grpc::testing::EchoTestService::Service { |
| 58 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 |
| 73 class 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_); |
| 96 channel_ = CreateChannel(target, InsecureChannelCredentials()); |
| 97 stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
| 98 } |
| 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: |
| 117 std::shared_ptr<Channel> channel_; |
| 118 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 119 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 |
| 128 TEST_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 |
| 151 int main(int argc, char** argv) { |
| 152 grpc_test_init(argc, argv); |
| 153 ::testing::InitGoogleTest(&argc, argv); |
| 154 return RUN_ALL_TESTS(); |
| 155 } |
OLD | NEW |