Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 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 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 34 | #include <grpc++/channel.h> |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 35 | #include <grpc++/client_context.h> |
| 36 | #include <grpc++/create_channel.h> |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 37 | #include <grpc++/server.h> |
| 38 | #include <grpc++/server_builder.h> |
| 39 | #include <grpc++/server_context.h> |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 40 | #include <grpc/grpc.h> |
| 41 | #include <grpc/grpc_zookeeper.h> |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 42 | #include <gtest/gtest.h> |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 43 | #include <zookeeper/zookeeper.h> |
| 44 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 45 | #include "src/core/support/env.h" |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 46 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 47 | #include "test/core/util/port.h" |
| 48 | #include "test/core/util/test_config.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 49 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 50 | using grpc::testing::EchoRequest; |
| 51 | using grpc::testing::EchoResponse; |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 52 | |
| 53 | namespace grpc { |
| 54 | namespace testing { |
| 55 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 56 | class ZookeeperTestServiceImpl |
| 57 | : public ::grpc::testing::EchoTestService::Service { |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 58 | public: |
| 59 | Status Echo(ServerContext* context, const EchoRequest* request, |
| 60 | EchoResponse* response) GRPC_OVERRIDE { |
| 61 | response->set_message(request->message()); |
| 62 | return Status::OK; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | class ZookeeperTest : public ::testing::Test { |
| 67 | protected: |
| 68 | ZookeeperTest() {} |
| 69 | |
| 70 | void SetUp() GRPC_OVERRIDE { |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 71 | SetUpZookeeper(); |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 72 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 73 | // Sets up two servers |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 74 | int port1 = grpc_pick_unused_port_or_die(); |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 75 | server1_ = SetUpServer(port1); |
Hongwei Wang | ff6097a | 2015-08-12 17:48:56 -0700 | [diff] [blame] | 76 | |
| 77 | int port2 = grpc_pick_unused_port_or_die(); |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 78 | server2_ = SetUpServer(port2); |
Hongwei Wang | d7c5aa8 | 2015-08-10 20:30:39 -0700 | [diff] [blame] | 79 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 80 | // Registers service /test in zookeeper |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 81 | RegisterService("/test", "test"); |
| 82 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 83 | // Registers service instance /test/1 in zookeeper |
Hongwei Wang | d7c5aa8 | 2015-08-10 20:30:39 -0700 | [diff] [blame] | 84 | string value = |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 85 | "{\"host\":\"localhost\",\"port\":\"" + to_string(port1) + "\"}"; |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 86 | RegisterService("/test/1", value); |
| 87 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 88 | // Registers service instance /test/2 in zookeeper |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 89 | value = "{\"host\":\"localhost\",\"port\":\"" + to_string(port2) + "\"}"; |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 90 | RegisterService("/test/2", value); |
| 91 | } |
| 92 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 93 | // Requires zookeeper server running |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 94 | void SetUpZookeeper() { |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 95 | // Finds zookeeper server address in environment |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 96 | // Default is localhost:2181 |
Hongwei Wang | 497d26a | 2015-08-07 16:39:24 -0700 | [diff] [blame] | 97 | zookeeper_address_ = "localhost:2181"; |
Hongwei Wang | 6d44596 | 2015-08-07 10:53:22 -0700 | [diff] [blame] | 98 | char* addr = gpr_getenv("GRPC_ZOOKEEPER_SERVER_TEST"); |
| 99 | if (addr != NULL) { |
| 100 | string addr_str(addr); |
Hongwei Wang | 497d26a | 2015-08-07 16:39:24 -0700 | [diff] [blame] | 101 | zookeeper_address_ = addr_str; |
Hongwei Wang | 6d44596 | 2015-08-07 10:53:22 -0700 | [diff] [blame] | 102 | gpr_free(addr); |
Hongwei Wang | 2ad07d7 | 2015-08-07 11:30:36 -0700 | [diff] [blame] | 103 | } |
Hongwei Wang | 6bbcd04 | 2015-08-07 19:09:36 -0700 | [diff] [blame] | 104 | gpr_log(GPR_DEBUG, zookeeper_address_.c_str()); |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 105 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 106 | // Connects to zookeeper server |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 107 | zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); |
Hongwei Wang | d7c5aa8 | 2015-08-10 20:30:39 -0700 | [diff] [blame] | 108 | zookeeper_handle_ = |
| 109 | zookeeper_init(zookeeper_address_.c_str(), NULL, 15000, 0, 0, 0); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 110 | GPR_ASSERT(zookeeper_handle_ != NULL); |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 111 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 112 | // Registers zookeeper name resolver in grpc |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 113 | grpc_zookeeper_register(); |
Hongwei Wang | ff6097a | 2015-08-12 17:48:56 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 116 | std::unique_ptr<Server> SetUpServer(const int port) { |
| 117 | string server_address = "localhost:" + to_string(port); |
Hongwei Wang | ff6097a | 2015-08-12 17:48:56 -0700 | [diff] [blame] | 118 | |
| 119 | ServerBuilder builder; |
| 120 | builder.AddListeningPort(server_address, InsecureServerCredentials()); |
| 121 | builder.RegisterService(&service_); |
| 122 | std::unique_ptr<Server> server = builder.BuildAndStart(); |
| 123 | return server; |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 126 | void RegisterService(const string& name, const string& value) { |
Hongwei Wang | d7c5aa8 | 2015-08-10 20:30:39 -0700 | [diff] [blame] | 127 | char* path = (char*)gpr_malloc(name.size()); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 128 | |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 129 | int status = zoo_exists(zookeeper_handle_, name.c_str(), 0, NULL); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 130 | if (status == ZNONODE) { |
Hongwei Wang | d7c5aa8 | 2015-08-10 20:30:39 -0700 | [diff] [blame] | 131 | status = |
| 132 | zoo_create(zookeeper_handle_, name.c_str(), value.c_str(), |
| 133 | value.size(), &ZOO_OPEN_ACL_UNSAFE, 0, path, name.size()); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 134 | } else { |
Hongwei Wang | d7c5aa8 | 2015-08-10 20:30:39 -0700 | [diff] [blame] | 135 | status = zoo_set(zookeeper_handle_, name.c_str(), value.c_str(), |
| 136 | value.size(), -1); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 137 | } |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 138 | gpr_free(path); |
| 139 | GPR_ASSERT(status == 0); |
| 140 | } |
| 141 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 142 | void DeleteService(const string& name) { |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 143 | int status = zoo_delete(zookeeper_handle_, name.c_str(), -1); |
| 144 | GPR_ASSERT(status == 0); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 147 | void ChangeZookeeperState() { |
| 148 | server1_->Shutdown(); |
| 149 | DeleteService("/test/1"); |
| 150 | } |
| 151 | |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 152 | void TearDown() GRPC_OVERRIDE { |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 153 | server1_->Shutdown(); |
| 154 | server2_->Shutdown(); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 155 | zookeeper_close(zookeeper_handle_); |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void ResetStub() { |
Hongwei Wang | 497d26a | 2015-08-07 16:39:24 -0700 | [diff] [blame] | 159 | string target = "zookeeper://" + zookeeper_address_ + "/test"; |
Julien Boeuf | 18a12c4 | 2015-10-12 15:05:59 -0700 | [diff] [blame] | 160 | channel_ = CreateChannel(target, InsecureChannelCredentials()); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 161 | stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 164 | string to_string(const int number) { |
| 165 | std::stringstream strs; |
| 166 | strs << number; |
| 167 | return strs.str(); |
| 168 | } |
| 169 | |
yang-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 170 | std::shared_ptr<Channel> channel_; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 171 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 172 | std::unique_ptr<Server> server1_; |
| 173 | std::unique_ptr<Server> server2_; |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 174 | ZookeeperTestServiceImpl service_; |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 175 | zhandle_t* zookeeper_handle_; |
Hongwei Wang | 497d26a | 2015-08-07 16:39:24 -0700 | [diff] [blame] | 176 | string zookeeper_address_; |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 179 | // Tests zookeeper state change between two RPCs |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 180 | // TODO(ctiller): leaked objects in this test |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 181 | TEST_F(ZookeeperTest, ZookeeperStateChangeTwoRpc) { |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 182 | ResetStub(); |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 183 | |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 184 | // First RPC |
| 185 | EchoRequest request1; |
| 186 | EchoResponse response1; |
| 187 | ClientContext context1; |
Hongwei Wang | 443a750 | 2015-08-05 16:27:54 -0700 | [diff] [blame] | 188 | context1.set_authority("test"); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 189 | request1.set_message("Hello"); |
| 190 | Status s1 = stub_->Echo(&context1, request1, &response1); |
| 191 | EXPECT_EQ(response1.message(), request1.message()); |
| 192 | EXPECT_TRUE(s1.ok()); |
| 193 | |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 194 | // Zookeeper state changes |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 195 | gpr_log(GPR_DEBUG, "Zookeeper state change"); |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 196 | ChangeZookeeperState(); |
Hongwei Wang | c3f48c8 | 2015-08-13 17:22:58 -0700 | [diff] [blame] | 197 | // Waits for re-resolving addresses |
Hongwei Wang | 85ad685 | 2015-08-13 16:13:10 -0700 | [diff] [blame] | 198 | // TODO(ctiller): RPC will probably fail if not waiting |
Hongwei Wang | d7c5aa8 | 2015-08-10 20:30:39 -0700 | [diff] [blame] | 199 | sleep(1); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 200 | |
| 201 | // Second RPC |
| 202 | EchoRequest request2; |
| 203 | EchoResponse response2; |
| 204 | ClientContext context2; |
Hongwei Wang | 443a750 | 2015-08-05 16:27:54 -0700 | [diff] [blame] | 205 | context2.set_authority("test"); |
Hongwei Wang | b0453fb | 2015-08-10 17:04:36 -0700 | [diff] [blame] | 206 | request2.set_message("World"); |
Hongwei Wang | eace190 | 2015-08-03 12:22:32 -0700 | [diff] [blame] | 207 | Status s2 = stub_->Echo(&context2, request2, &response2); |
| 208 | EXPECT_EQ(response2.message(), request2.message()); |
| 209 | EXPECT_TRUE(s2.ok()); |
Hongwei Wang | 8e04d41 | 2015-07-31 15:12:51 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | } // namespace testing |
| 213 | } // namespace grpc |
| 214 | |
| 215 | int main(int argc, char** argv) { |
| 216 | grpc_test_init(argc, argv); |
| 217 | ::testing::InitGoogleTest(&argc, argv); |
| 218 | return RUN_ALL_TESTS(); |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame^] | 219 | } |