blob: 1b8a39c1beb36025a147f51001aa15ea2629574d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
murgatroid99c36f6ea2016-10-03 09:24:09 -070034#include "src/core/lib/iomgr/port.h"
35
36// This test won't work except with posix sockets enabled
37#ifdef GRPC_POSIX_SOCKET
38
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/iomgr/tcp_server.h"
Craig Tiller69b093b2016-02-25 19:04:07 -080040
41#include <errno.h>
42#include <netinet/in.h>
43#include <string.h>
44#include <sys/socket.h>
45#include <unistd.h>
46
Dan Bornb13a69d2016-01-14 16:53:18 -080047#include <grpc/grpc.h>
Craig Tiller69b093b2016-02-25 19:04:07 -080048#include <grpc/support/alloc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049#include <grpc/support/log.h>
50#include <grpc/support/sync.h>
51#include <grpc/support/time.h>
Craig Tiller69b093b2016-02-25 19:04:07 -080052
Craig Tiller9533d042016-03-25 17:11:06 -070053#include "src/core/lib/iomgr/iomgr.h"
murgatroid997871f732016-09-23 13:49:05 -070054#include "src/core/lib/iomgr/resolve_address.h"
Craig Tiller9533d042016-03-25 17:11:06 -070055#include "src/core/lib/iomgr/sockaddr_utils.h"
Dan Bornd890a212016-01-14 15:00:21 -080056#include "test/core/util/port.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057#include "test/core/util/test_config.h"
58
Craig Tiller35696192015-05-24 15:00:37 -070059#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", #x)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060
Craig Tiller69b093b2016-02-25 19:04:07 -080061static gpr_mu *g_mu;
62static grpc_pollset *g_pollset;
Craig Tiller451b4df2015-05-12 16:47:46 -070063static int g_nconnects = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064
Dan Born601acad2016-01-13 19:52:58 -080065typedef struct on_connect_result {
Dan Bornb13a69d2016-01-14 16:53:18 -080066 /* Owns a ref to server. */
67 grpc_tcp_server *server;
Dan Born601acad2016-01-13 19:52:58 -080068 unsigned port_index;
69 unsigned fd_index;
Dan Bornfa6b6062016-01-08 21:01:59 -080070 int server_fd;
Dan Born601acad2016-01-13 19:52:58 -080071} on_connect_result;
72
Dan Born9c12bc22016-01-13 16:52:20 -080073typedef struct server_weak_ref {
74 grpc_tcp_server *server;
75
76 /* arg is this server_weak_ref. */
77 grpc_closure server_shutdown;
78} server_weak_ref;
79
80static on_connect_result g_result = {NULL, 0, 0, -1};
81
82static void on_connect_result_init(on_connect_result *result) {
Dan Bornb13a69d2016-01-14 16:53:18 -080083 result->server = NULL;
84 result->port_index = 0;
85 result->fd_index = 0;
86 result->server_fd = -1;
87}
88
Dan Born9c12bc22016-01-13 16:52:20 -080089static void on_connect_result_set(on_connect_result *result,
90 const grpc_tcp_server_acceptor *acceptor) {
Dan Bornb13a69d2016-01-14 16:53:18 -080091 result->server = grpc_tcp_server_ref(acceptor->from_server);
92 result->port_index = acceptor->port_index;
93 result->fd_index = acceptor->fd_index;
94 result->server_fd = grpc_tcp_server_port_fd(
95 result->server, acceptor->port_index, acceptor->fd_index);
96}
97
Dan Born9c12bc22016-01-13 16:52:20 -080098static void server_weak_ref_shutdown(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tillerf707d622016-05-06 14:26:12 -070099 grpc_error *error) {
Dan Born9c12bc22016-01-13 16:52:20 -0800100 server_weak_ref *weak_ref = arg;
101 weak_ref->server = NULL;
102}
103
104static void server_weak_ref_init(server_weak_ref *weak_ref) {
105 weak_ref->server = NULL;
106 grpc_closure_init(&weak_ref->server_shutdown, server_weak_ref_shutdown,
107 weak_ref);
108}
109
110/* Make weak_ref->server_shutdown a shutdown_starting cb on server.
111 grpc_tcp_server promises that the server object will live until
112 weak_ref->server_shutdown has returned. A strong ref on grpc_tcp_server
113 should be held until server_weak_ref_set() returns to avoid a race where the
114 server is deleted before the shutdown_starting cb is added. */
115static void server_weak_ref_set(server_weak_ref *weak_ref,
116 grpc_tcp_server *server) {
117 grpc_tcp_server_shutdown_starting_add(server, &weak_ref->server_shutdown);
118 weak_ref->server = server;
119}
Dan Bornfa6b6062016-01-08 21:01:59 -0800120
121static void on_connect(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp,
Craig Tiller29dc4902016-05-20 08:43:48 -0700122 grpc_pollset *pollset,
Dan Born5d81d152016-01-12 20:29:29 -0800123 grpc_tcp_server_acceptor *acceptor) {
Craig Tillera82950e2015-09-22 12:33:20 -0700124 grpc_endpoint_shutdown(exec_ctx, tcp);
125 grpc_endpoint_destroy(exec_ctx, tcp);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Craig Tiller2406e312016-10-19 13:26:48 -0700127 on_connect_result temp_result;
128 on_connect_result_set(&temp_result, acceptor);
129
Craig Tiller69b093b2016-02-25 19:04:07 -0800130 gpr_mu_lock(g_mu);
Craig Tiller2406e312016-10-19 13:26:48 -0700131 g_result = temp_result;
Craig Tiller451b4df2015-05-12 16:47:46 -0700132 g_nconnects++;
Craig Tiller1aee5362016-05-07 11:26:50 -0700133 GPR_ASSERT(
134 GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, NULL)));
Craig Tiller69b093b2016-02-25 19:04:07 -0800135 gpr_mu_unlock(g_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800136}
137
Craig Tillera82950e2015-09-22 12:33:20 -0700138static void test_no_op(void) {
Craig Tillerb8b1a462015-09-22 12:50:03 -0700139 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -0700140 grpc_tcp_server *s;
Craig Tiller30ff60e2016-09-23 14:54:27 -0700141 GPR_ASSERT(GRPC_ERROR_NONE ==
142 grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
Dan Bornfa6b6062016-01-08 21:01:59 -0800143 grpc_tcp_server_unref(&exec_ctx, s);
Craig Tillerb8b1a462015-09-22 12:50:03 -0700144 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145}
146
Craig Tillera82950e2015-09-22 12:33:20 -0700147static void test_no_op_with_start(void) {
Craig Tillerf5768a62015-09-22 10:54:34 -0700148 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerf707d622016-05-06 14:26:12 -0700149 grpc_tcp_server *s;
Craig Tiller30ff60e2016-09-23 14:54:27 -0700150 GPR_ASSERT(GRPC_ERROR_NONE ==
151 grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
Craig Tillera82950e2015-09-22 12:33:20 -0700152 LOG_TEST("test_no_op_with_start");
153 grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL);
Dan Bornfa6b6062016-01-08 21:01:59 -0800154 grpc_tcp_server_unref(&exec_ctx, s);
Craig Tillera82950e2015-09-22 12:33:20 -0700155 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156}
157
Craig Tillera82950e2015-09-22 12:33:20 -0700158static void test_no_op_with_port(void) {
Craig Tillerf5768a62015-09-22 10:54:34 -0700159 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid997871f732016-09-23 13:49:05 -0700160 grpc_resolved_address resolved_addr;
161 struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr;
Craig Tillerf707d622016-05-06 14:26:12 -0700162 grpc_tcp_server *s;
Craig Tiller30ff60e2016-09-23 14:54:27 -0700163 GPR_ASSERT(GRPC_ERROR_NONE ==
164 grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
Craig Tillera82950e2015-09-22 12:33:20 -0700165 LOG_TEST("test_no_op_with_port");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166
murgatroid997871f732016-09-23 13:49:05 -0700167 memset(&resolved_addr, 0, sizeof(resolved_addr));
168 resolved_addr.len = sizeof(struct sockaddr_in);
169 addr->sin_family = AF_INET;
Craig Tillerf707d622016-05-06 14:26:12 -0700170 int port;
murgatroid99dedb9232016-09-26 13:54:04 -0700171 GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) ==
172 GRPC_ERROR_NONE &&
Craig Tillerf707d622016-05-06 14:26:12 -0700173 port > 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174
Dan Bornfa6b6062016-01-08 21:01:59 -0800175 grpc_tcp_server_unref(&exec_ctx, s);
Craig Tillera82950e2015-09-22 12:33:20 -0700176 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177}
178
Craig Tillera82950e2015-09-22 12:33:20 -0700179static void test_no_op_with_port_and_start(void) {
Craig Tillerf5768a62015-09-22 10:54:34 -0700180 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid997871f732016-09-23 13:49:05 -0700181 grpc_resolved_address resolved_addr;
182 struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr;
Craig Tillerf707d622016-05-06 14:26:12 -0700183 grpc_tcp_server *s;
Craig Tiller30ff60e2016-09-23 14:54:27 -0700184 GPR_ASSERT(GRPC_ERROR_NONE ==
185 grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
Craig Tillera82950e2015-09-22 12:33:20 -0700186 LOG_TEST("test_no_op_with_port_and_start");
Craig Tillerf707d622016-05-06 14:26:12 -0700187 int port;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188
murgatroid997871f732016-09-23 13:49:05 -0700189 memset(&resolved_addr, 0, sizeof(resolved_addr));
190 resolved_addr.len = sizeof(struct sockaddr_in);
191 addr->sin_family = AF_INET;
murgatroid99dedb9232016-09-26 13:54:04 -0700192 GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) ==
193 GRPC_ERROR_NONE &&
Craig Tillerf707d622016-05-06 14:26:12 -0700194 port > 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195
Craig Tillera82950e2015-09-22 12:33:20 -0700196 grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197
Dan Bornfa6b6062016-01-08 21:01:59 -0800198 grpc_tcp_server_unref(&exec_ctx, s);
Craig Tillera82950e2015-09-22 12:33:20 -0700199 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200}
201
Dan Born601acad2016-01-13 19:52:58 -0800202static void tcp_connect(grpc_exec_ctx *exec_ctx, const struct sockaddr *remote,
203 socklen_t remote_len, on_connect_result *result) {
204 gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10);
205 int clifd = socket(remote->sa_family, SOCK_STREAM, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 int nconnects_before;
Craig Tiller451b4df2015-05-12 16:47:46 -0700207
Craig Tiller69b093b2016-02-25 19:04:07 -0800208 gpr_mu_lock(g_mu);
Dan Born601acad2016-01-13 19:52:58 -0800209 nconnects_before = g_nconnects;
Dan Bornb13a69d2016-01-14 16:53:18 -0800210 on_connect_result_init(&g_result);
Dan Born601acad2016-01-13 19:52:58 -0800211 GPR_ASSERT(clifd >= 0);
212 gpr_log(GPR_DEBUG, "start connect");
213 GPR_ASSERT(connect(clifd, remote, remote_len) == 0);
214 gpr_log(GPR_DEBUG, "wait");
215 while (g_nconnects == nconnects_before &&
216 gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) > 0) {
Craig Tillerf290e302016-02-18 08:04:36 -0800217 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700218 GPR_ASSERT(GRPC_LOG_IF_ERROR(
219 "pollset_work",
220 grpc_pollset_work(exec_ctx, g_pollset, &worker,
221 gpr_now(GPR_CLOCK_MONOTONIC), deadline)));
Craig Tiller69b093b2016-02-25 19:04:07 -0800222 gpr_mu_unlock(g_mu);
Dan Born601acad2016-01-13 19:52:58 -0800223 grpc_exec_ctx_finish(exec_ctx);
Craig Tiller69b093b2016-02-25 19:04:07 -0800224 gpr_mu_lock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700225 }
Dan Born601acad2016-01-13 19:52:58 -0800226 gpr_log(GPR_DEBUG, "wait done");
227 GPR_ASSERT(g_nconnects == nconnects_before + 1);
228 close(clifd);
229 *result = g_result;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230
Craig Tiller69b093b2016-02-25 19:04:07 -0800231 gpr_mu_unlock(g_mu);
Dan Born601acad2016-01-13 19:52:58 -0800232}
233
Dan Born601acad2016-01-13 19:52:58 -0800234/* Tests a tcp server with multiple ports. TODO(daniel-j-born): Multiple fds for
235 the same port should be tested. */
Dan Bornb13a69d2016-01-14 16:53:18 -0800236static void test_connect(unsigned n) {
Dan Born601acad2016-01-13 19:52:58 -0800237 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
murgatroid997871f732016-09-23 13:49:05 -0700238 grpc_resolved_address resolved_addr;
239 grpc_resolved_address resolved_addr1;
240 struct sockaddr_storage *addr = (struct sockaddr_storage *)resolved_addr.addr;
murgatroid99dedb9232016-09-26 13:54:04 -0700241 struct sockaddr_storage *addr1 =
242 (struct sockaddr_storage *)resolved_addr1.addr;
Dan Born601acad2016-01-13 19:52:58 -0800243 unsigned svr_fd_count;
244 int svr_port;
245 unsigned svr1_fd_count;
Dan Bornb13a69d2016-01-14 16:53:18 -0800246 int svr1_port;
Craig Tillerf707d622016-05-06 14:26:12 -0700247 grpc_tcp_server *s;
Craig Tiller30ff60e2016-09-23 14:54:27 -0700248 GPR_ASSERT(GRPC_ERROR_NONE ==
249 grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s));
Dan Born601acad2016-01-13 19:52:58 -0800250 unsigned i;
Dan Born9c12bc22016-01-13 16:52:20 -0800251 server_weak_ref weak_ref;
252 server_weak_ref_init(&weak_ref);
Dan Born601acad2016-01-13 19:52:58 -0800253 LOG_TEST("test_connect");
254 gpr_log(GPR_INFO, "clients=%d", n);
murgatroid997871f732016-09-23 13:49:05 -0700255 memset(&resolved_addr, 0, sizeof(resolved_addr));
256 memset(&resolved_addr1, 0, sizeof(resolved_addr1));
257 resolved_addr.len = sizeof(struct sockaddr_storage);
258 resolved_addr1.len = sizeof(struct sockaddr_storage);
259 addr->ss_family = addr1->ss_family = AF_INET;
Craig Tillerf707d622016-05-06 14:26:12 -0700260 GPR_ASSERT(GRPC_ERROR_NONE ==
murgatroid997871f732016-09-23 13:49:05 -0700261 grpc_tcp_server_add_port(s, &resolved_addr, &svr_port));
Dan Born601acad2016-01-13 19:52:58 -0800262 GPR_ASSERT(svr_port > 0);
Dan Bornb13a69d2016-01-14 16:53:18 -0800263 /* Cannot use wildcard (port==0), because add_port() will try to reuse the
264 same port as a previous add_port(). */
265 svr1_port = grpc_pick_unused_port_or_die();
murgatroid997871f732016-09-23 13:49:05 -0700266 grpc_sockaddr_set_port(&resolved_addr1, svr1_port);
murgatroid99dedb9232016-09-26 13:54:04 -0700267 GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr1, &svr_port) ==
268 GRPC_ERROR_NONE &&
Craig Tillerf707d622016-05-06 14:26:12 -0700269 svr_port == svr1_port);
Dan Born601acad2016-01-13 19:52:58 -0800270
271 /* Bad port_index. */
272 GPR_ASSERT(grpc_tcp_server_port_fd_count(s, 2) == 0);
273 GPR_ASSERT(grpc_tcp_server_port_fd(s, 2, 0) < 0);
274
275 /* Bad fd_index. */
276 GPR_ASSERT(grpc_tcp_server_port_fd(s, 0, 100) < 0);
277 GPR_ASSERT(grpc_tcp_server_port_fd(s, 1, 100) < 0);
278
279 /* Got at least one fd per port. */
280 svr_fd_count = grpc_tcp_server_port_fd_count(s, 0);
281 GPR_ASSERT(svr_fd_count >= 1);
282 svr1_fd_count = grpc_tcp_server_port_fd_count(s, 1);
283 GPR_ASSERT(svr1_fd_count >= 1);
284
285 for (i = 0; i < svr_fd_count; ++i) {
286 int fd = grpc_tcp_server_port_fd(s, 0, i);
287 GPR_ASSERT(fd >= 0);
288 if (i == 0) {
murgatroid99dedb9232016-09-26 13:54:04 -0700289 GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr,
290 (socklen_t *)&resolved_addr.len) == 0);
murgatroid997871f732016-09-23 13:49:05 -0700291 GPR_ASSERT(resolved_addr.len <= sizeof(*addr));
Dan Born601acad2016-01-13 19:52:58 -0800292 }
293 }
294 for (i = 0; i < svr1_fd_count; ++i) {
295 int fd = grpc_tcp_server_port_fd(s, 1, i);
296 GPR_ASSERT(fd >= 0);
297 if (i == 0) {
murgatroid99dedb9232016-09-26 13:54:04 -0700298 GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr1,
299 (socklen_t *)&resolved_addr1.len) == 0);
murgatroid997871f732016-09-23 13:49:05 -0700300 GPR_ASSERT(resolved_addr1.len <= sizeof(*addr1));
Dan Born601acad2016-01-13 19:52:58 -0800301 }
302 }
303
Craig Tiller69b093b2016-02-25 19:04:07 -0800304 grpc_tcp_server_start(&exec_ctx, s, &g_pollset, 1, on_connect, NULL);
Dan Born601acad2016-01-13 19:52:58 -0800305
306 for (i = 0; i < n; i++) {
Dan Bornb13a69d2016-01-14 16:53:18 -0800307 on_connect_result result;
Dan Born601acad2016-01-13 19:52:58 -0800308 int svr_fd;
Dan Bornb13a69d2016-01-14 16:53:18 -0800309 on_connect_result_init(&result);
murgatroid99dedb9232016-09-26 13:54:04 -0700310 tcp_connect(&exec_ctx, (struct sockaddr *)addr,
311 (socklen_t)resolved_addr.len, &result);
Dan Born601acad2016-01-13 19:52:58 -0800312 GPR_ASSERT(result.server_fd >= 0);
313 svr_fd = result.server_fd;
Dan Borna78ca382016-01-14 13:02:09 -0800314 GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) ==
315 result.server_fd);
Dan Born601acad2016-01-13 19:52:58 -0800316 GPR_ASSERT(result.port_index == 0);
317 GPR_ASSERT(result.fd_index < svr_fd_count);
Dan Bornb13a69d2016-01-14 16:53:18 -0800318 GPR_ASSERT(result.server == s);
Dan Born9c12bc22016-01-13 16:52:20 -0800319 if (weak_ref.server == NULL) {
320 server_weak_ref_set(&weak_ref, result.server);
321 }
Dan Bornb13a69d2016-01-14 16:53:18 -0800322 grpc_tcp_server_unref(&exec_ctx, result.server);
Dan Born601acad2016-01-13 19:52:58 -0800323
Dan Bornb13a69d2016-01-14 16:53:18 -0800324 on_connect_result_init(&result);
murgatroid99dedb9232016-09-26 13:54:04 -0700325 tcp_connect(&exec_ctx, (struct sockaddr *)addr1,
326 (socklen_t)resolved_addr1.len, &result);
Dan Born601acad2016-01-13 19:52:58 -0800327 GPR_ASSERT(result.server_fd >= 0);
328 GPR_ASSERT(result.server_fd != svr_fd);
Dan Borna78ca382016-01-14 13:02:09 -0800329 GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) ==
330 result.server_fd);
Dan Born601acad2016-01-13 19:52:58 -0800331 GPR_ASSERT(result.port_index == 1);
332 GPR_ASSERT(result.fd_index < svr_fd_count);
Dan Bornb13a69d2016-01-14 16:53:18 -0800333 GPR_ASSERT(result.server == s);
334 grpc_tcp_server_unref(&exec_ctx, result.server);
Dan Born601acad2016-01-13 19:52:58 -0800335 }
Craig Tillera82950e2015-09-22 12:33:20 -0700336
Dan Born9c12bc22016-01-13 16:52:20 -0800337 /* Weak ref to server valid until final unref. */
338 GPR_ASSERT(weak_ref.server != NULL);
339 GPR_ASSERT(grpc_tcp_server_port_fd(s, 0, 0) >= 0);
340
Dan Bornfa6b6062016-01-08 21:01:59 -0800341 grpc_tcp_server_unref(&exec_ctx, s);
Dan Born930c2982016-09-08 19:12:18 -0700342 grpc_exec_ctx_finish(&exec_ctx);
Dan Born9c12bc22016-01-13 16:52:20 -0800343
344 /* Weak ref lost. */
345 GPR_ASSERT(weak_ref.server == NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800346}
347
Craig Tillerf707d622016-05-06 14:26:12 -0700348static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p,
349 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -0700350 grpc_pollset_destroy(p);
Craig Tiller294d0ec2015-09-21 14:59:45 -0700351}
Craig Tiller451b4df2015-05-12 16:47:46 -0700352
Craig Tillera82950e2015-09-22 12:33:20 -0700353int main(int argc, char **argv) {
Craig Tiller294d0ec2015-09-21 14:59:45 -0700354 grpc_closure destroyed;
Craig Tillerf5768a62015-09-22 10:54:34 -0700355 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700356 grpc_test_init(argc, argv);
Dan Bornb13a69d2016-01-14 16:53:18 -0800357 grpc_init();
Craig Tiller69b093b2016-02-25 19:04:07 -0800358 g_pollset = gpr_malloc(grpc_pollset_size());
359 grpc_pollset_init(g_pollset, &g_mu);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800360
Craig Tillera82950e2015-09-22 12:33:20 -0700361 test_no_op();
362 test_no_op_with_start();
363 test_no_op_with_port();
364 test_no_op_with_port_and_start();
Dan Bornb13a69d2016-01-14 16:53:18 -0800365 test_connect(1);
366 test_connect(10);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800367
Craig Tiller69b093b2016-02-25 19:04:07 -0800368 grpc_closure_init(&destroyed, destroy_pollset, g_pollset);
369 grpc_pollset_shutdown(&exec_ctx, g_pollset, &destroyed);
Craig Tillera82950e2015-09-22 12:33:20 -0700370 grpc_exec_ctx_finish(&exec_ctx);
Dan Bornb13a69d2016-01-14 16:53:18 -0800371 grpc_shutdown();
Craig Tiller69b093b2016-02-25 19:04:07 -0800372 gpr_free(g_pollset);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800373 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800374}
murgatroid99c36f6ea2016-10-03 09:24:09 -0700375
376#else /* GRPC_POSIX_SOCKET */
377
murgatroid99aa9c5782016-10-10 12:16:13 -0700378int main(int argc, char **argv) { return 1; }
murgatroid99c36f6ea2016-10-03 09:24:09 -0700379
380#endif /* GRPC_POSIX_SOCKET */