blob: 997a8391eb72b19322bf0c9caeed5781e839da01 [file] [log] [blame]
David Garcia Quintas3fb8f732016-06-15 22:53:08 -07001/*
2 *
3 * Copyright 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
David Garcia Quintasd9cee6f2016-08-01 17:42:47 -070034#include <cinttypes>
David Garcia Quintasaaba1312016-06-22 18:10:37 -070035#include <cstdarg>
David Garcia Quintasd9cee6f2016-08-01 17:42:47 -070036#include <cstdint>
David Garcia Quintasaaba1312016-06-22 18:10:37 -070037#include <cstring>
38#include <string>
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070039
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -070040#include <gtest/gtest.h>
41
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070042#include <grpc/grpc.h>
David Garcia Quintas601bb122016-08-18 15:03:59 -070043#include <grpc/impl/codegen/byte_buffer_reader.h>
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070044#include <grpc/support/alloc.h>
45#include <grpc/support/host_port.h>
46#include <grpc/support/log.h>
47#include <grpc/support/string_util.h>
48#include <grpc/support/sync.h>
49#include <grpc/support/thd.h>
50#include <grpc/support/time.h>
51
David Garcia Quintas8a81aa12016-08-22 15:06:49 -070052#include <grpc++/impl/codegen/config.h>
David Garcia Quintas8a81aa12016-08-22 15:06:49 -070053extern "C" {
Craig Tiller9eb0fde2017-03-31 16:59:30 -070054#include "src/core/ext/filters/client_channel/client_channel.h"
David Garcia Quintas01291502017-02-07 13:26:41 -080055#include "src/core/lib/channel/channel_args.h"
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070056#include "src/core/lib/channel/channel_stack.h"
David Garcia Quintas88006452016-08-24 06:53:23 -070057#include "src/core/lib/iomgr/sockaddr.h"
David Garcia Quintas01291502017-02-07 13:26:41 -080058#include "src/core/lib/security/credentials/fake/fake_credentials.h"
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070059#include "src/core/lib/support/string.h"
60#include "src/core/lib/support/tmpfile.h"
61#include "src/core/lib/surface/channel.h"
62#include "src/core/lib/surface/server.h"
63#include "test/core/end2end/cq_verifier.h"
Mark D. Rothda0ec822016-10-03 11:32:04 -070064#include "test/core/end2end/fake_resolver.h"
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070065#include "test/core/util/port.h"
66#include "test/core/util/test_config.h"
David Garcia Quintas55145c02016-06-21 14:51:54 -070067}
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070068
David Garcia Quintasaaba1312016-06-22 18:10:37 -070069#include "src/proto/grpc/lb/v1/load_balancer.pb.h"
70
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070071#define NUM_BACKENDS 4
David Garcia Quintas5b0e9462016-08-15 19:38:39 -070072#define PAYLOAD "hello you"
David Garcia Quintas3fb8f732016-06-15 22:53:08 -070073
David Garcia Quintasf9f856b2016-06-22 18:25:53 -070074// TODO(dgq): Other scenarios in need of testing:
David Garcia Quintasea11d162016-07-14 17:27:28 -070075// - Send an empty serverlist update and verify that the client request blocks
76// until a new serverlist with actual contents is available.
David Garcia Quintasf9f856b2016-06-22 18:25:53 -070077// - Send identical serverlist update
David Garcia Quintas35c2aba2016-09-13 15:28:09 -070078// - Send a serverlist with faulty ip:port addresses (port > 2^16, etc).
David Garcia Quintasf9f856b2016-06-22 18:25:53 -070079// - Test reception of invalid serverlist
80// - Test pinging
David Garcia Quintas7ec29132016-11-01 04:09:05 +010081// - Test against a non-LB server.
David Garcia Quintasa26c77b2016-07-18 12:57:09 -070082// - Random LB server closing the stream unexpectedly.
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -070083// - Test using DNS-resolvable names (localhost?)
David Garcia Quintas149f09d2016-11-17 20:43:10 -080084// - Test handling of creation of faulty RR instance by having the LB return a
85// serverlist with non-existent backends after having initially returned a
86// valid one.
David Garcia Quintas7ec29132016-11-01 04:09:05 +010087//
88// Findings from end to end testing to be covered here:
89// - Handling of LB servers restart, including reconnection after backing-off
90// retries.
91// - Destruction of load balanced channel (and therefore of grpclb instance)
92// while:
93// 1) the internal LB call is still active. This should work by virtue
94// of the weak reference the LB call holds. The call should be terminated as
95// part of the grpclb shutdown process.
96// 2) the retry timer is active. Again, the weak reference it holds should
97// prevent a premature call to \a glb_destroy.
98// - Restart of backend servers with no changes to serverlist. This exercises
99// the RR handover mechanism.
David Garcia Quintasf9f856b2016-06-22 18:25:53 -0700100
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700101namespace grpc {
102namespace {
103
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700104typedef struct client_fixture {
105 grpc_channel *client;
106 char *server_uri;
107 grpc_completion_queue *cq;
108} client_fixture;
109
110typedef struct server_fixture {
111 grpc_server *server;
112 grpc_call *server_call;
113 grpc_completion_queue *cq;
114 char *servers_hostport;
David Garcia Quintas01291502017-02-07 13:26:41 -0800115 const char *balancer_name;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700116 int port;
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800117 const char *lb_token_prefix;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700118 gpr_thd_id tid;
119 int num_calls_serviced;
120} server_fixture;
121
122typedef struct test_fixture {
123 server_fixture lb_server;
124 server_fixture lb_backends[NUM_BACKENDS];
125 client_fixture client;
126 int lb_server_update_delay_ms;
127} test_fixture;
128
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700129static void *tag(intptr_t t) { return (void *)t; }
130
Craig Tillerd41a4a72016-10-26 16:16:06 -0700131static grpc_slice build_response_payload_slice(
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700132 const char *host, int *ports, size_t nports,
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800133 int64_t expiration_interval_secs, int32_t expiration_interval_nanos,
134 const char *token_prefix) {
David Garcia Quintasf9f856b2016-06-22 18:25:53 -0700135 // server_list {
136 // servers {
David Garcia Quintas8a81aa12016-08-22 15:06:49 -0700137 // ip_address: <in_addr/6 bytes of an IP>
138 // port: <16 bit uint>
David Garcia Quintasf9f856b2016-06-22 18:25:53 -0700139 // load_balance_token: "token..."
140 // }
141 // ...
142 // }
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700143 grpc::lb::v1::LoadBalanceResponse response;
144 auto *serverlist = response.mutable_server_list();
145
146 if (expiration_interval_secs > 0 || expiration_interval_nanos > 0) {
147 auto *expiration_interval = serverlist->mutable_expiration_interval();
148 if (expiration_interval_secs > 0) {
149 expiration_interval->set_seconds(expiration_interval_secs);
150 }
151 if (expiration_interval_nanos > 0) {
152 expiration_interval->set_nanos(expiration_interval_nanos);
153 }
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700154 }
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700155 for (size_t i = 0; i < nports; i++) {
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700156 auto *server = serverlist->add_servers();
David Garcia Quintas8a81aa12016-08-22 15:06:49 -0700157 // TODO(dgq): test ipv6
158 struct in_addr ip4;
159 GPR_ASSERT(inet_pton(AF_INET, host, &ip4) == 1);
160 server->set_ip_address(
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800161 string(reinterpret_cast<const char *>(&ip4), sizeof(ip4)));
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700162 server->set_port(ports[i]);
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800163 // Missing tokens are acceptable. Test that path.
164 if (strlen(token_prefix) > 0) {
David Garcia Quintas0133a1b2016-11-15 15:42:37 -0800165 string token_data = token_prefix + std::to_string(ports[i]);
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800166 server->set_load_balance_token(token_data);
167 }
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700168 }
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800169 const string &enc_resp = response.SerializeAsString();
Craig Tillerd41a4a72016-10-26 16:16:06 -0700170 return grpc_slice_from_copied_buffer(enc_resp.data(), enc_resp.size());
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700171}
172
173static void drain_cq(grpc_completion_queue *cq) {
174 grpc_event ev;
175 do {
Robbie Shadeca7effc2017-01-17 09:14:29 -0500176 ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700177 NULL);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700178 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
179}
180
181static void sleep_ms(int delay_ms) {
182 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
183 gpr_time_from_millis(delay_ms, GPR_TIMESPAN)));
184}
185
186static void start_lb_server(server_fixture *sf, int *ports, size_t nports,
187 int update_delay_ms) {
188 grpc_call *s;
189 cq_verifier *cqv = cq_verifier_create(sf->cq);
190 grpc_op ops[6];
191 grpc_op *op;
192 grpc_metadata_array request_metadata_recv;
193 grpc_call_details call_details;
194 grpc_call_error error;
195 int was_cancelled = 2;
196 grpc_byte_buffer *request_payload_recv;
197 grpc_byte_buffer *response_payload;
198
199 memset(ops, 0, sizeof(ops));
200 grpc_metadata_array_init(&request_metadata_recv);
201 grpc_call_details_init(&call_details);
202
203 error = grpc_server_request_call(sf->server, &s, &call_details,
204 &request_metadata_recv, sf->cq, sf->cq,
205 tag(200));
206 GPR_ASSERT(GRPC_CALL_OK == error);
David Garcia Quintas01291502017-02-07 13:26:41 -0800207 gpr_log(GPR_INFO, "LB Server[%s](%s) up", sf->servers_hostport,
208 sf->balancer_name);
Mark D. Roth7f9bba82016-08-25 08:35:42 -0700209 CQ_EXPECT_COMPLETION(cqv, tag(200), 1);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700210 cq_verify(cqv);
David Garcia Quintas01291502017-02-07 13:26:41 -0800211 gpr_log(GPR_INFO, "LB Server[%s](%s) after tag 200", sf->servers_hostport,
212 sf->balancer_name);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700213
David Garcia Quintas601bb122016-08-18 15:03:59 -0700214 // make sure we've received the initial metadata from the grpclb request.
215 GPR_ASSERT(request_metadata_recv.count > 0);
216 GPR_ASSERT(request_metadata_recv.metadata != NULL);
217
218 // receive request for backends
219 op = ops;
220 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800221 op->data.recv_message.recv_message = &request_payload_recv;
David Garcia Quintas601bb122016-08-18 15:03:59 -0700222 op->flags = 0;
223 op->reserved = NULL;
224 op++;
225 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), NULL);
226 GPR_ASSERT(GRPC_CALL_OK == error);
David Garcia Quintasb8245bd2016-09-01 18:29:46 +0200227 CQ_EXPECT_COMPLETION(cqv, tag(202), 1);
David Garcia Quintas601bb122016-08-18 15:03:59 -0700228 cq_verify(cqv);
David Garcia Quintas01291502017-02-07 13:26:41 -0800229 gpr_log(GPR_INFO, "LB Server[%s](%s) after RECV_MSG", sf->servers_hostport,
230 sf->balancer_name);
David Garcia Quintas601bb122016-08-18 15:03:59 -0700231
232 // validate initial request.
233 grpc_byte_buffer_reader bbr;
234 grpc_byte_buffer_reader_init(&bbr, request_payload_recv);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700235 grpc_slice request_payload_slice = grpc_byte_buffer_reader_readall(&bbr);
David Garcia Quintas601bb122016-08-18 15:03:59 -0700236 grpc::lb::v1::LoadBalanceRequest request;
Craig Tiller618e67d2016-10-26 21:08:10 -0700237 request.ParseFromArray(GRPC_SLICE_START_PTR(request_payload_slice),
238 GRPC_SLICE_LENGTH(request_payload_slice));
David Garcia Quintas601bb122016-08-18 15:03:59 -0700239 GPR_ASSERT(request.has_initial_request());
David Garcia Quintas55ba14a2016-09-27 18:45:30 -0700240 GPR_ASSERT(request.initial_request().name() == sf->servers_hostport);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700241 grpc_slice_unref(request_payload_slice);
David Garcia Quintas601bb122016-08-18 15:03:59 -0700242 grpc_byte_buffer_reader_destroy(&bbr);
243 grpc_byte_buffer_destroy(request_payload_recv);
244
Craig Tillerd41a4a72016-10-26 16:16:06 -0700245 grpc_slice response_payload_slice;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700246 op = ops;
247 op->op = GRPC_OP_SEND_INITIAL_METADATA;
248 op->data.send_initial_metadata.count = 0;
249 op->flags = 0;
250 op->reserved = NULL;
251 op++;
252 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
253 op->data.recv_close_on_server.cancelled = &was_cancelled;
254 op->flags = 0;
255 op->reserved = NULL;
256 op++;
257 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(201), NULL);
258 GPR_ASSERT(GRPC_CALL_OK == error);
David Garcia Quintas01291502017-02-07 13:26:41 -0800259 gpr_log(GPR_INFO, "LB Server[%s](%s) after tag 201", sf->servers_hostport,
260 sf->balancer_name);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700261
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700262 for (int i = 0; i < 2; i++) {
263 if (i == 0) {
264 // First half of the ports.
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800265 response_payload_slice = build_response_payload_slice(
266 "127.0.0.1", ports, nports / 2, -1, -1, sf->lb_token_prefix);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700267 } else {
268 // Second half of the ports.
269 sleep_ms(update_delay_ms);
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800270 response_payload_slice = build_response_payload_slice(
271 "127.0.0.1", ports + (nports / 2), (nports + 1) / 2 /* ceil */, -1,
272 -1, "" /* this half doesn't get to receive an LB token */);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700273 }
274
275 response_payload = grpc_raw_byte_buffer_create(&response_payload_slice, 1);
276 op = ops;
277 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800278 op->data.send_message.send_message = response_payload;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700279 op->flags = 0;
280 op->reserved = NULL;
281 op++;
282 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(203), NULL);
283 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7f9bba82016-08-25 08:35:42 -0700284 CQ_EXPECT_COMPLETION(cqv, tag(203), 1);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700285 cq_verify(cqv);
David Garcia Quintas01291502017-02-07 13:26:41 -0800286 gpr_log(GPR_INFO, "LB Server[%s](%s) after SEND_MESSAGE, iter %d",
287 sf->servers_hostport, sf->balancer_name, i);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700288
289 grpc_byte_buffer_destroy(response_payload);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700290 grpc_slice_unref(response_payload_slice);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700291 }
David Garcia Quintas01291502017-02-07 13:26:41 -0800292 gpr_log(GPR_INFO, "LB Server[%s](%s) shutting down", sf->servers_hostport,
293 sf->balancer_name);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700294
295 op = ops;
296 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
297 op->data.send_status_from_server.trailing_metadata_count = 0;
298 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800299 grpc_slice status_details = grpc_slice_from_static_string("xyz");
300 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700301 op->flags = 0;
302 op->reserved = NULL;
303 op++;
304 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(204), NULL);
305 GPR_ASSERT(GRPC_CALL_OK == error);
306
Mark D. Roth7f9bba82016-08-25 08:35:42 -0700307 CQ_EXPECT_COMPLETION(cqv, tag(201), 1);
308 CQ_EXPECT_COMPLETION(cqv, tag(204), 1);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700309 cq_verify(cqv);
David Garcia Quintas01291502017-02-07 13:26:41 -0800310 gpr_log(GPR_INFO, "LB Server[%s](%s) after tag 204. All done. LB server out",
311 sf->servers_hostport, sf->balancer_name);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700312
313 grpc_call_destroy(s);
314
315 cq_verifier_destroy(cqv);
316
317 grpc_metadata_array_destroy(&request_metadata_recv);
318 grpc_call_details_destroy(&call_details);
319}
320
321static void start_backend_server(server_fixture *sf) {
322 grpc_call *s;
323 cq_verifier *cqv;
324 grpc_op ops[6];
325 grpc_op *op;
326 grpc_metadata_array request_metadata_recv;
327 grpc_call_details call_details;
328 grpc_call_error error;
329 int was_cancelled;
330 grpc_byte_buffer *request_payload_recv;
331 grpc_byte_buffer *response_payload;
332 grpc_event ev;
333
334 while (true) {
335 memset(ops, 0, sizeof(ops));
336 cqv = cq_verifier_create(sf->cq);
337 was_cancelled = 2;
338 grpc_metadata_array_init(&request_metadata_recv);
339 grpc_call_details_init(&call_details);
340
341 error = grpc_server_request_call(sf->server, &s, &call_details,
342 &request_metadata_recv, sf->cq, sf->cq,
343 tag(100));
344 GPR_ASSERT(GRPC_CALL_OK == error);
345 gpr_log(GPR_INFO, "Server[%s] up", sf->servers_hostport);
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700346 ev = grpc_completion_queue_next(sf->cq,
Robbie Shadeca7effc2017-01-17 09:14:29 -0500347 grpc_timeout_seconds_to_deadline(60), NULL);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700348 if (!ev.success) {
349 gpr_log(GPR_INFO, "Server[%s] being torn down", sf->servers_hostport);
350 cq_verifier_destroy(cqv);
351 grpc_metadata_array_destroy(&request_metadata_recv);
352 grpc_call_details_destroy(&call_details);
353 return;
354 }
355 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800356 const string expected_token =
Sree Kuchibhotla5bec1332017-03-24 10:03:22 -0700357 strlen(sf->lb_token_prefix) == 0 ? "" : sf->lb_token_prefix +
358 std::to_string(sf->port);
David Garcia Quintasa3654db2016-10-11 15:52:39 -0700359 GPR_ASSERT(contains_metadata(&request_metadata_recv, "lb-token",
David Garcia Quintas8a81aa12016-08-22 15:06:49 -0700360 expected_token.c_str()));
David Garcia Quintas5b0e9462016-08-15 19:38:39 -0700361
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700362 gpr_log(GPR_INFO, "Server[%s] after tag 100", sf->servers_hostport);
363
364 op = ops;
365 op->op = GRPC_OP_SEND_INITIAL_METADATA;
366 op->data.send_initial_metadata.count = 0;
367 op->flags = 0;
368 op->reserved = NULL;
369 op++;
370 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
371 op->data.recv_close_on_server.cancelled = &was_cancelled;
372 op->flags = 0;
373 op->reserved = NULL;
374 op++;
375 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(101), NULL);
376 GPR_ASSERT(GRPC_CALL_OK == error);
377 gpr_log(GPR_INFO, "Server[%s] after tag 101", sf->servers_hostport);
378
379 bool exit = false;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700380 grpc_slice response_payload_slice = grpc_slice_from_copied_string(PAYLOAD);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700381 while (!exit) {
382 op = ops;
383 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800384 op->data.recv_message.recv_message = &request_payload_recv;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700385 op->flags = 0;
386 op->reserved = NULL;
387 op++;
388 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL);
389 GPR_ASSERT(GRPC_CALL_OK == error);
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700390 ev = grpc_completion_queue_next(
Robbie Shadeca7effc2017-01-17 09:14:29 -0500391 sf->cq, grpc_timeout_seconds_to_deadline(3), NULL);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700392 if (ev.type == GRPC_OP_COMPLETE && ev.success) {
393 GPR_ASSERT(ev.tag = tag(102));
394 if (request_payload_recv == NULL) {
395 exit = true;
396 gpr_log(GPR_INFO,
397 "Server[%s] recv \"close\" from client, exiting. Call #%d",
398 sf->servers_hostport, sf->num_calls_serviced);
399 }
400 } else {
401 gpr_log(GPR_INFO, "Server[%s] forced to shutdown. Call #%d",
402 sf->servers_hostport, sf->num_calls_serviced);
403 exit = true;
404 }
405 gpr_log(GPR_INFO, "Server[%s] after tag 102. Call #%d",
406 sf->servers_hostport, sf->num_calls_serviced);
407
408 if (!exit) {
409 response_payload =
410 grpc_raw_byte_buffer_create(&response_payload_slice, 1);
411 op = ops;
412 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800413 op->data.send_message.send_message = response_payload;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700414 op->flags = 0;
415 op->reserved = NULL;
416 op++;
417 error =
418 grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), NULL);
419 GPR_ASSERT(GRPC_CALL_OK == error);
David Garcia Quintas4166cb02016-07-29 14:33:15 -0700420 ev = grpc_completion_queue_next(
Robbie Shadeca7effc2017-01-17 09:14:29 -0500421 sf->cq, grpc_timeout_seconds_to_deadline(3), NULL);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700422 if (ev.type == GRPC_OP_COMPLETE && ev.success) {
423 GPR_ASSERT(ev.tag = tag(103));
424 } else {
425 gpr_log(GPR_INFO, "Server[%s] forced to shutdown. Call #%d",
426 sf->servers_hostport, sf->num_calls_serviced);
427 exit = true;
428 }
429 gpr_log(GPR_INFO, "Server[%s] after tag 103. Call #%d",
430 sf->servers_hostport, sf->num_calls_serviced);
431 grpc_byte_buffer_destroy(response_payload);
432 }
433
434 grpc_byte_buffer_destroy(request_payload_recv);
435 }
436 ++sf->num_calls_serviced;
437
438 gpr_log(GPR_INFO, "Server[%s] OUT OF THE LOOP", sf->servers_hostport);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700439 grpc_slice_unref(response_payload_slice);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700440
441 op = ops;
442 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
443 op->data.send_status_from_server.trailing_metadata_count = 0;
444 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800445 grpc_slice status_details =
446 grpc_slice_from_static_string("Backend server out a-ok");
447 op->data.send_status_from_server.status_details = &status_details;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700448 op->flags = 0;
449 op->reserved = NULL;
450 op++;
451 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), NULL);
452 GPR_ASSERT(GRPC_CALL_OK == error);
453
Mark D. Roth7f9bba82016-08-25 08:35:42 -0700454 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
455 CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700456 cq_verify(cqv);
457 gpr_log(GPR_INFO, "Server[%s] DONE. After servicing %d calls",
David Garcia Quintas8782d1b2016-06-15 23:58:44 -0700458 sf->servers_hostport, sf->num_calls_serviced);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700459
460 grpc_call_destroy(s);
461 cq_verifier_destroy(cqv);
462 grpc_metadata_array_destroy(&request_metadata_recv);
463 grpc_call_details_destroy(&call_details);
464 }
465}
466
467static void perform_request(client_fixture *cf) {
468 grpc_call *c;
469 cq_verifier *cqv = cq_verifier_create(cf->cq);
470 grpc_op ops[6];
471 grpc_op *op;
472 grpc_metadata_array initial_metadata_recv;
473 grpc_metadata_array trailing_metadata_recv;
474 grpc_status_code status;
475 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800476 grpc_slice details;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700477 grpc_byte_buffer *request_payload;
478 grpc_byte_buffer *response_payload_recv;
479 int i;
480
481 memset(ops, 0, sizeof(ops));
Craig Tiller28b72422016-10-26 21:15:29 -0700482 grpc_slice request_payload_slice =
483 grpc_slice_from_copied_string("hello world");
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700484
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800485 grpc_slice host = grpc_slice_from_static_string("foo.test.google.fr:1234");
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700486 c = grpc_channel_create_call(cf->client, NULL, GRPC_PROPAGATE_DEFAULTS,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800487 cf->cq, grpc_slice_from_static_string("/foo"),
Robbie Shadeca7effc2017-01-17 09:14:29 -0500488 &host, grpc_timeout_seconds_to_deadline(5),
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800489 NULL);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700490 gpr_log(GPR_INFO, "Call 0x%" PRIxPTR " created", (intptr_t)c);
491 GPR_ASSERT(c);
492 char *peer;
493
494 grpc_metadata_array_init(&initial_metadata_recv);
495 grpc_metadata_array_init(&trailing_metadata_recv);
496
497 op = ops;
498 op->op = GRPC_OP_SEND_INITIAL_METADATA;
499 op->data.send_initial_metadata.count = 0;
500 op->flags = 0;
501 op->reserved = NULL;
502 op++;
503 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800504 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700505 op->flags = 0;
506 op->reserved = NULL;
507 op++;
508 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
509 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
510 op->data.recv_status_on_client.status = &status;
511 op->data.recv_status_on_client.status_details = &details;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700512 op->flags = 0;
513 op->reserved = NULL;
514 op++;
515 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL);
516 GPR_ASSERT(GRPC_CALL_OK == error);
517
518 for (i = 0; i < 4; i++) {
519 request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
520
521 op = ops;
522 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800523 op->data.send_message.send_message = request_payload;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700524 op->flags = 0;
525 op->reserved = NULL;
526 op++;
527 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800528 op->data.recv_message.recv_message = &response_payload_recv;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700529 op->flags = 0;
530 op->reserved = NULL;
531 op++;
532 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), NULL);
533 GPR_ASSERT(GRPC_CALL_OK == error);
534
Mark D. Roth7f9bba82016-08-25 08:35:42 -0700535 CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700536 cq_verify(cqv);
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800537 gpr_log(GPR_INFO, "Client after sending msg %d / 4", i + 1);
David Garcia Quintas5b0e9462016-08-15 19:38:39 -0700538 GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, PAYLOAD));
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700539
540 grpc_byte_buffer_destroy(request_payload);
541 grpc_byte_buffer_destroy(response_payload_recv);
542 }
543
Craig Tillerd41a4a72016-10-26 16:16:06 -0700544 grpc_slice_unref(request_payload_slice);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700545
546 op = ops;
547 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
548 op->flags = 0;
549 op->reserved = NULL;
550 op++;
551 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), NULL);
552 GPR_ASSERT(GRPC_CALL_OK == error);
553
Mark D. Roth7f9bba82016-08-25 08:35:42 -0700554 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
555 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700556 cq_verify(cqv);
557 peer = grpc_call_get_peer(c);
558 gpr_log(GPR_INFO, "Client DONE WITH SERVER %s ", peer);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700559
560 grpc_call_destroy(c);
561
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800562 cq_verify_empty_timeout(cqv, 1 /* seconds */);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700563 cq_verifier_destroy(cqv);
564
565 grpc_metadata_array_destroy(&initial_metadata_recv);
566 grpc_metadata_array_destroy(&trailing_metadata_recv);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800567 grpc_slice_unref(details);
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800568 gpr_log(GPR_INFO, "Client call (peer %s) DESTROYED.", peer);
569 gpr_free(peer);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700570}
571
David Garcia Quintas01291502017-02-07 13:26:41 -0800572#define BALANCERS_NAME "lb.name"
573static void setup_client(const server_fixture *lb_server,
574 const server_fixture *backends, client_fixture *cf) {
575 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
David Garcia Quintas01291502017-02-07 13:26:41 -0800576
577 char *expected_target_names = NULL;
578 const char *backends_name = lb_server->servers_hostport;
579 gpr_asprintf(&expected_target_names, "%s;%s", backends_name, BALANCERS_NAME);
580
David Garcia Quintas53af23c2017-04-15 10:29:46 -0700581 grpc_fake_resolver_response_generator *response_generator =
582 grpc_fake_resolver_response_generator_create();
583
584 grpc_lb_addresses *addresses = grpc_lb_addresses_create(1, NULL);
585 char *lb_uri_str;
586 gpr_asprintf(&lb_uri_str, "ipv4:%s", lb_server->servers_hostport);
587 grpc_uri *lb_uri = grpc_uri_parse(&exec_ctx, lb_uri_str, true);
588 GPR_ASSERT(lb_uri != NULL);
589 grpc_lb_addresses_set_address_from_uri(addresses, 0, lb_uri, true,
590 lb_server->balancer_name, NULL);
591 grpc_uri_destroy(lb_uri);
592 gpr_free(lb_uri_str);
593
594 gpr_asprintf(&cf->server_uri, "test:///%s", lb_server->servers_hostport);
595 const grpc_arg fake_addresses =
596 grpc_lb_addresses_create_channel_arg(addresses);
597 grpc_channel_args *fake_result =
598 grpc_channel_args_copy_and_add(NULL, &fake_addresses, 1);
599 grpc_lb_addresses_destroy(&exec_ctx, addresses);
600
601 const grpc_arg new_args[] = {
602 grpc_fake_transport_expected_targets_arg(expected_target_names),
603 grpc_fake_resolver_response_generator_arg(response_generator)};
604
David Garcia Quintas01291502017-02-07 13:26:41 -0800605 grpc_channel_args *args =
David Garcia Quintas53af23c2017-04-15 10:29:46 -0700606 grpc_channel_args_copy_and_add(NULL, new_args, GPR_ARRAY_SIZE(new_args));
David Garcia Quintas01291502017-02-07 13:26:41 -0800607 gpr_free(expected_target_names);
608
Sree Kuchibhotlaf2c32152017-03-22 03:01:24 -0700609 cf->cq = grpc_completion_queue_create_for_next(NULL);
David Garcia Quintas01291502017-02-07 13:26:41 -0800610 grpc_channel_credentials *fake_creds =
611 grpc_fake_transport_security_credentials_create();
612 cf->client =
613 grpc_secure_channel_create(fake_creds, cf->server_uri, args, NULL);
David Garcia Quintas53af23c2017-04-15 10:29:46 -0700614 grpc_fake_resolver_response_generator_set_response(
615 &exec_ctx, response_generator, fake_result);
616 grpc_channel_args_destroy(&exec_ctx, fake_result);
David Garcia Quintas01291502017-02-07 13:26:41 -0800617 grpc_channel_credentials_unref(&exec_ctx, fake_creds);
618 grpc_channel_args_destroy(&exec_ctx, args);
David Garcia Quintas53af23c2017-04-15 10:29:46 -0700619 grpc_fake_resolver_response_generator_unref(response_generator);
620 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700621}
622
623static void teardown_client(client_fixture *cf) {
624 grpc_completion_queue_shutdown(cf->cq);
625 drain_cq(cf->cq);
626 grpc_completion_queue_destroy(cf->cq);
627 cf->cq = NULL;
628 grpc_channel_destroy(cf->client);
629 cf->client = NULL;
630 gpr_free(cf->server_uri);
631}
632
633static void setup_server(const char *host, server_fixture *sf) {
634 int assigned_port;
635
Sree Kuchibhotlaf2c32152017-03-22 03:01:24 -0700636 sf->cq = grpc_completion_queue_create_for_next(NULL);
David Garcia Quintas55145c02016-06-21 14:51:54 -0700637 const char *colon_idx = strchr(host, ':');
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700638 if (colon_idx) {
David Garcia Quintas55145c02016-06-21 14:51:54 -0700639 const char *port_str = colon_idx + 1;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700640 sf->port = atoi(port_str);
641 sf->servers_hostport = gpr_strdup(host);
642 } else {
643 sf->port = grpc_pick_unused_port_or_die();
644 gpr_join_host_port(&sf->servers_hostport, host, sf->port);
645 }
646
David Garcia Quintas01291502017-02-07 13:26:41 -0800647 grpc_server_credentials *server_creds =
648 grpc_fake_transport_security_server_credentials_create();
649
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700650 sf->server = grpc_server_create(NULL, NULL);
651 grpc_server_register_completion_queue(sf->server, sf->cq, NULL);
David Garcia Quintas01291502017-02-07 13:26:41 -0800652 GPR_ASSERT((assigned_port = grpc_server_add_secure_http2_port(
653 sf->server, sf->servers_hostport, server_creds)) > 0);
654 grpc_server_credentials_release(server_creds);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700655 GPR_ASSERT(sf->port == assigned_port);
656 grpc_server_start(sf->server);
657}
658
659static void teardown_server(server_fixture *sf) {
660 if (!sf->server) return;
661
662 gpr_log(GPR_INFO, "Server[%s] shutting down", sf->servers_hostport);
Sree Kuchibhotla982a6f22017-03-03 02:19:31 -0800663
664 grpc_completion_queue *shutdown_cq =
Sree Kuchibhotlaf2c32152017-03-22 03:01:24 -0700665 grpc_completion_queue_create_for_pluck(NULL);
Sree Kuchibhotla982a6f22017-03-03 02:19:31 -0800666 grpc_server_shutdown_and_notify(sf->server, shutdown_cq, tag(1000));
667 GPR_ASSERT(grpc_completion_queue_pluck(shutdown_cq, tag(1000),
668 grpc_timeout_seconds_to_deadline(5),
669 NULL)
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700670 .type == GRPC_OP_COMPLETE);
Sree Kuchibhotla982a6f22017-03-03 02:19:31 -0800671 grpc_completion_queue_destroy(shutdown_cq);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700672 grpc_server_destroy(sf->server);
673 gpr_thd_join(sf->tid);
674
675 sf->server = NULL;
676 grpc_completion_queue_shutdown(sf->cq);
677 drain_cq(sf->cq);
678 grpc_completion_queue_destroy(sf->cq);
679
680 gpr_log(GPR_INFO, "Server[%s] bye bye", sf->servers_hostport);
681 gpr_free(sf->servers_hostport);
682}
683
684static void fork_backend_server(void *arg) {
David Garcia Quintas55145c02016-06-21 14:51:54 -0700685 server_fixture *sf = static_cast<server_fixture *>(arg);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700686 start_backend_server(sf);
687}
688
689static void fork_lb_server(void *arg) {
David Garcia Quintas55145c02016-06-21 14:51:54 -0700690 test_fixture *tf = static_cast<test_fixture *>(arg);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700691 int ports[NUM_BACKENDS];
692 for (int i = 0; i < NUM_BACKENDS; i++) {
693 ports[i] = tf->lb_backends[i].port;
694 }
695 start_lb_server(&tf->lb_server, ports, NUM_BACKENDS,
696 tf->lb_server_update_delay_ms);
697}
698
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800699#define LB_TOKEN_PREFIX "token"
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700700static test_fixture setup_test_fixture(int lb_server_update_delay_ms) {
701 test_fixture tf;
702 memset(&tf, 0, sizeof(tf));
703 tf.lb_server_update_delay_ms = lb_server_update_delay_ms;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700704
705 gpr_thd_options options = gpr_thd_options_default();
706 gpr_thd_options_set_joinable(&options);
707
708 for (int i = 0; i < NUM_BACKENDS; ++i) {
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800709 // Only the first half of the servers expect an LB token.
710 if (i < NUM_BACKENDS / 2) {
711 tf.lb_backends[i].lb_token_prefix = LB_TOKEN_PREFIX;
712 } else {
713 tf.lb_backends[i].lb_token_prefix = "";
714 }
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700715 setup_server("127.0.0.1", &tf.lb_backends[i]);
716 gpr_thd_new(&tf.lb_backends[i].tid, fork_backend_server, &tf.lb_backends[i],
717 &options);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700718 }
719
David Garcia Quintas850cbaa2016-11-15 15:13:35 -0800720 tf.lb_server.lb_token_prefix = LB_TOKEN_PREFIX;
David Garcia Quintas01291502017-02-07 13:26:41 -0800721 tf.lb_server.balancer_name = BALANCERS_NAME;
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700722 setup_server("127.0.0.1", &tf.lb_server);
723 gpr_thd_new(&tf.lb_server.tid, fork_lb_server, &tf.lb_server, &options);
David Garcia Quintas01291502017-02-07 13:26:41 -0800724 setup_client(&tf.lb_server, tf.lb_backends, &tf.client);
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700725 return tf;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700726}
727
728static void teardown_test_fixture(test_fixture *tf) {
729 teardown_client(&tf->client);
730 for (int i = 0; i < NUM_BACKENDS; ++i) {
731 teardown_server(&tf->lb_backends[i]);
732 }
733 teardown_server(&tf->lb_server);
734}
735
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700736// The LB server will send two updates: batch 1 and batch 2. Each batch contains
737// two addresses, both of a valid and running backend server. Batch 1 is readily
738// available and provided as soon as the client establishes the streaming call.
739// Batch 2 is sent after a delay of \a lb_server_update_delay_ms milliseconds.
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700740static test_fixture test_update(int lb_server_update_delay_ms) {
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700741 gpr_log(GPR_INFO, "start %s(%d)", __func__, lb_server_update_delay_ms);
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700742 test_fixture tf = setup_test_fixture(lb_server_update_delay_ms);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700743 perform_request(
744 &tf.client); // "consumes" 1st backend server of 1st serverlist
745 perform_request(
746 &tf.client); // "consumes" 2nd backend server of 1st serverlist
747
748 perform_request(
749 &tf.client); // "consumes" 1st backend server of 2nd serverlist
750 perform_request(
751 &tf.client); // "consumes" 2nd backend server of 2nd serverlist
752
753 teardown_test_fixture(&tf);
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700754 gpr_log(GPR_INFO, "end %s(%d)", __func__, lb_server_update_delay_ms);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700755 return tf;
756}
757
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700758TEST(GrpclbTest, Updates) {
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700759 grpc::test_fixture tf_result;
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800760 // Clients take at least one second to complete a call (the last part of the
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700761 // call sleeps for 1 second while verifying the client's completion queue is
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800762 // empty), more if the system is under load. Therefore:
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700763 //
764 // If the LB server waits 800ms before sending an update, it will arrive
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800765 // before the first client request finishes, skipping the second server from
766 // batch 1. All subsequent picks will come from the second half of the
767 // backends, those coming in the LB update.
David Garcia Quintasaaba1312016-06-22 18:10:37 -0700768 tf_result = grpc::test_update(800);
David Garcia Quintas01291502017-02-07 13:26:41 -0800769 GPR_ASSERT(tf_result.lb_backends[0].num_calls_serviced +
770 tf_result.lb_backends[1].num_calls_serviced ==
771 1);
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800772 GPR_ASSERT(tf_result.lb_backends[2].num_calls_serviced +
773 tf_result.lb_backends[3].num_calls_serviced >
774 0);
775 int num_serviced_calls = 0;
776 for (int i = 0; i < 4; i++) {
777 num_serviced_calls += tf_result.lb_backends[i].num_calls_serviced;
778 }
779 GPR_ASSERT(num_serviced_calls == 4);
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700780
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800781 // If the LB server waits 2500ms, the update arrives after two calls and three
782 // picks. The third pick will be the 1st server of the 1st update (RR policy
783 // going around). The fourth and final pick will come from the second LB
784 // update. In any case, the total number of serviced calls must again be equal
785 // to four across all the backends.
David Garcia Quintase60ae9c2016-08-02 16:37:41 -0700786 tf_result = grpc::test_update(2500);
David Garcia Quintas01291502017-02-07 13:26:41 -0800787 GPR_ASSERT(tf_result.lb_backends[0].num_calls_serviced +
788 tf_result.lb_backends[1].num_calls_serviced >=
789 2);
David Garcia Quintas033c51e2016-11-30 00:49:29 -0800790 GPR_ASSERT(tf_result.lb_backends[2].num_calls_serviced +
791 tf_result.lb_backends[3].num_calls_serviced >
792 0);
793 num_serviced_calls = 0;
794 for (int i = 0; i < 4; i++) {
795 num_serviced_calls += tf_result.lb_backends[i].num_calls_serviced;
796 }
797 GPR_ASSERT(num_serviced_calls == 4);
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700798}
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700799
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700800TEST(GrpclbTest, InvalidAddressInServerlist) {}
801
802} // namespace
803} // namespace grpc
804
805int main(int argc, char **argv) {
806 ::testing::InitGoogleTest(&argc, argv);
Mark D. Rothda0ec822016-10-03 11:32:04 -0700807 grpc_fake_resolver_init();
David Garcia Quintas53af23c2017-04-15 10:29:46 -0700808 grpc_test_init(argc, argv);
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700809 grpc_init();
810 const auto result = RUN_ALL_TESTS();
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700811 grpc_shutdown();
David Garcia Quintasf47d6fb2016-09-14 12:59:17 -0700812 return result;
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700813}