Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: third_party/grpc/test/cpp/end2end/zookeeper_test.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <grpc++/channel.h>
35 #include <grpc++/client_context.h>
36 #include <grpc++/create_channel.h>
37 #include <grpc++/server.h>
38 #include <grpc++/server_builder.h>
39 #include <grpc++/server_context.h>
40 #include <grpc/grpc.h>
41 #include <grpc/grpc_zookeeper.h>
42 #include <gtest/gtest.h>
43 #include <zookeeper/zookeeper.h>
44
45 #include "src/core/support/env.h"
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"
49
50 using grpc::testing::EchoRequest;
51 using grpc::testing::EchoResponse;
52
53 namespace grpc {
54 namespace testing {
55
56 class ZookeeperTestServiceImpl
57 : public ::grpc::testing::EchoTestService::Service {
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 {
71 SetUpZookeeper();
72
73 // Sets up two servers
74 int port1 = grpc_pick_unused_port_or_die();
75 server1_ = SetUpServer(port1);
76
77 int port2 = grpc_pick_unused_port_or_die();
78 server2_ = SetUpServer(port2);
79
80 // Registers service /test in zookeeper
81 RegisterService("/test", "test");
82
83 // Registers service instance /test/1 in zookeeper
84 string value =
85 "{\"host\":\"localhost\",\"port\":\"" + to_string(port1) + "\"}";
86 RegisterService("/test/1", value);
87
88 // Registers service instance /test/2 in zookeeper
89 value = "{\"host\":\"localhost\",\"port\":\"" + to_string(port2) + "\"}";
90 RegisterService("/test/2", value);
91 }
92
93 // Requires zookeeper server running
94 void SetUpZookeeper() {
95 // Finds zookeeper server address in environment
96 // Default is localhost:2181
97 zookeeper_address_ = "localhost:2181";
98 char* addr = gpr_getenv("GRPC_ZOOKEEPER_SERVER_TEST");
99 if (addr != NULL) {
100 string addr_str(addr);
101 zookeeper_address_ = addr_str;
102 gpr_free(addr);
103 }
104 gpr_log(GPR_DEBUG, zookeeper_address_.c_str());
105
106 // Connects to zookeeper server
107 zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);
108 zookeeper_handle_ =
109 zookeeper_init(zookeeper_address_.c_str(), NULL, 15000, 0, 0, 0);
110 GPR_ASSERT(zookeeper_handle_ != NULL);
111
112 // Registers zookeeper name resolver in grpc
113 grpc_zookeeper_register();
114 }
115
116 std::unique_ptr<Server> SetUpServer(const int port) {
117 string server_address = "localhost:" + to_string(port);
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;
124 }
125
126 void RegisterService(const string& name, const string& value) {
127 char* path = (char*)gpr_malloc(name.size());
128
129 int status = zoo_exists(zookeeper_handle_, name.c_str(), 0, NULL);
130 if (status == ZNONODE) {
131 status =
132 zoo_create(zookeeper_handle_, name.c_str(), value.c_str(),
133 value.size(), &ZOO_OPEN_ACL_UNSAFE, 0, path, name.size());
134 } else {
135 status = zoo_set(zookeeper_handle_, name.c_str(), value.c_str(),
136 value.size(), -1);
137 }
138 gpr_free(path);
139 GPR_ASSERT(status == 0);
140 }
141
142 void DeleteService(const string& name) {
143 int status = zoo_delete(zookeeper_handle_, name.c_str(), -1);
144 GPR_ASSERT(status == 0);
145 }
146
147 void ChangeZookeeperState() {
148 server1_->Shutdown();
149 DeleteService("/test/1");
150 }
151
152 void TearDown() GRPC_OVERRIDE {
153 server1_->Shutdown();
154 server2_->Shutdown();
155 zookeeper_close(zookeeper_handle_);
156 }
157
158 void ResetStub() {
159 string target = "zookeeper://" + zookeeper_address_ + "/test";
160 channel_ = CreateChannel(target, InsecureChannelCredentials());
161 stub_ = grpc::testing::EchoTestService::NewStub(channel_);
162 }
163
164 string to_string(const int number) {
165 std::stringstream strs;
166 strs << number;
167 return strs.str();
168 }
169
170 std::shared_ptr<Channel> channel_;
171 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
172 std::unique_ptr<Server> server1_;
173 std::unique_ptr<Server> server2_;
174 ZookeeperTestServiceImpl service_;
175 zhandle_t* zookeeper_handle_;
176 string zookeeper_address_;
177 };
178
179 // Tests zookeeper state change between two RPCs
180 // TODO(ctiller): leaked objects in this test
181 TEST_F(ZookeeperTest, ZookeeperStateChangeTwoRpc) {
182 ResetStub();
183
184 // First RPC
185 EchoRequest request1;
186 EchoResponse response1;
187 ClientContext context1;
188 context1.set_authority("test");
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
194 // Zookeeper state changes
195 gpr_log(GPR_DEBUG, "Zookeeper state change");
196 ChangeZookeeperState();
197 // Waits for re-resolving addresses
198 // TODO(ctiller): RPC will probably fail if not waiting
199 sleep(1);
200
201 // Second RPC
202 EchoRequest request2;
203 EchoResponse response2;
204 ClientContext context2;
205 context2.set_authority("test");
206 request2.set_message("World");
207 Status s2 = stub_->Echo(&context2, request2, &response2);
208 EXPECT_EQ(response2.message(), request2.message());
209 EXPECT_TRUE(s2.ok());
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();
219 }
OLDNEW
« no previous file with comments | « third_party/grpc/test/cpp/end2end/thread_stress_test.cc ('k') | third_party/grpc/test/cpp/grpclb/grpclb_api_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698