blob: b8b76d1c42646c3a9850f6700e6b76b42ef1a677 [file] [log] [blame]
ctiller18b49ab2014-12-09 14:39:16 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
ctiller18b49ab2014-12-09 14:39:16 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
ctiller18b49ab2014-12-09 14:39:16 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
ctiller18b49ab2014-12-09 14:39:16 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
ctiller18b49ab2014-12-09 14:39:16 -080016 *
17 */
18
murgatroid991191b722017-02-08 11:56:52 -080019#include "src/core/lib/iomgr/port.h"
20
21// This test won't work except with posix sockets enabled
22#ifdef GRPC_POSIX_SOCKET
23
Craig Tiller9533d042016-03-25 17:11:06 -070024#include "src/core/lib/iomgr/tcp_client.h"
ctiller18b49ab2014-12-09 14:39:16 -080025
26#include <errno.h>
27#include <netinet/in.h>
28#include <string.h>
29#include <sys/socket.h>
30#include <unistd.h>
31
Craig Tillerbe952492015-08-25 16:59:00 -070032#include <grpc/grpc.h>
Craig Tillera8be91b2016-02-19 16:22:44 -080033#include <grpc/support/alloc.h>
ctiller18b49ab2014-12-09 14:39:16 -080034#include <grpc/support/log.h>
35#include <grpc/support/time.h>
Craig Tillerbe952492015-08-25 16:59:00 -070036
Craig Tiller9533d042016-03-25 17:11:06 -070037#include "src/core/lib/iomgr/iomgr.h"
Craig Tiller8a034482016-03-28 16:09:04 -070038#include "src/core/lib/iomgr/pollset_set.h"
Craig Tiller9533d042016-03-25 17:11:06 -070039#include "src/core/lib/iomgr/socket_utils_posix.h"
40#include "src/core/lib/iomgr/timer.h"
Craig Tiller8ad8a412015-02-25 08:36:40 -080041#include "test/core/util/test_config.h"
ctiller18b49ab2014-12-09 14:39:16 -080042
Craig Tillera75d18a2016-02-25 08:45:00 -080043static grpc_pollset_set *g_pollset_set;
Craig Tiller85371a22016-02-25 13:55:13 -080044static gpr_mu *g_mu;
Craig Tillera8be91b2016-02-19 16:22:44 -080045static grpc_pollset *g_pollset;
Craig Tillercb63a9b2015-05-13 09:52:36 -070046static int g_connections_complete = 0;
Craig Tillerdfff1b82015-09-21 14:39:57 -070047static grpc_endpoint *g_connecting = NULL;
Craig Tillerb1fa1d52015-05-11 10:27:53 -070048
Craig Tillerc0df1c02017-07-17 16:12:33 -070049static grpc_millis test_deadline(void) {
Craig Tiller9a8c3f32017-07-21 13:14:14 -070050 return grpc_timespec_to_millis_round_up(grpc_timeout_seconds_to_deadline(10));
ctiller18b49ab2014-12-09 14:39:16 -080051}
52
Craig Tillera82950e2015-09-22 12:33:20 -070053static void finish_connection() {
Craig Tiller85371a22016-02-25 13:55:13 -080054 gpr_mu_lock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -070055 g_connections_complete++;
Craig Tiller0ff222a2017-09-01 09:41:43 -070056 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
57 GPR_ASSERT(GRPC_LOG_IF_ERROR("pollset_kick",
58 grpc_pollset_kick(&exec_ctx, g_pollset, NULL)));
59 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller85371a22016-02-25 13:55:13 -080060 gpr_mu_unlock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -070061}
62
Craig Tillerf707d622016-05-06 14:26:12 -070063static void must_succeed(grpc_exec_ctx *exec_ctx, void *arg,
64 grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -070065 GPR_ASSERT(g_connecting != NULL);
Craig Tillerf707d622016-05-06 14:26:12 -070066 GPR_ASSERT(error == GRPC_ERROR_NONE);
ncteisen4b36a3d2017-03-13 19:08:06 -070067 grpc_endpoint_shutdown(
68 exec_ctx, g_connecting,
69 GRPC_ERROR_CREATE_FROM_STATIC_STRING("must_succeed called"));
Craig Tillera82950e2015-09-22 12:33:20 -070070 grpc_endpoint_destroy(exec_ctx, g_connecting);
Craig Tillerdfff1b82015-09-21 14:39:57 -070071 g_connecting = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -070072 finish_connection();
ctiller18b49ab2014-12-09 14:39:16 -080073}
74
Craig Tillerf707d622016-05-06 14:26:12 -070075static void must_fail(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
Craig Tillera82950e2015-09-22 12:33:20 -070076 GPR_ASSERT(g_connecting == NULL);
Craig Tillerf707d622016-05-06 14:26:12 -070077 GPR_ASSERT(error != GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -070078 finish_connection();
ctiller18b49ab2014-12-09 14:39:16 -080079}
80
Craig Tillera82950e2015-09-22 12:33:20 -070081void test_succeeds(void) {
murgatroid997871f732016-09-23 13:49:05 -070082 grpc_resolved_address resolved_addr;
83 struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr;
ctiller18b49ab2014-12-09 14:39:16 -080084 int svr_fd;
85 int r;
Craig Tillercb63a9b2015-05-13 09:52:36 -070086 int connections_complete_before;
Craig Tillerdfff1b82015-09-21 14:39:57 -070087 grpc_closure done;
Craig Tillerf5768a62015-09-22 10:54:34 -070088 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
ctiller18b49ab2014-12-09 14:39:16 -080089
Craig Tillera82950e2015-09-22 12:33:20 -070090 gpr_log(GPR_DEBUG, "test_succeeds");
Craig Tillere9593352015-07-15 07:35:09 -070091
murgatroid997871f732016-09-23 13:49:05 -070092 memset(&resolved_addr, 0, sizeof(resolved_addr));
93 resolved_addr.len = sizeof(struct sockaddr_in);
94 addr->sin_family = AF_INET;
ctiller18b49ab2014-12-09 14:39:16 -080095
96 /* create a dummy server */
Craig Tillera82950e2015-09-22 12:33:20 -070097 svr_fd = socket(AF_INET, SOCK_STREAM, 0);
98 GPR_ASSERT(svr_fd >= 0);
murgatroid99dedb9232016-09-26 13:54:04 -070099 GPR_ASSERT(
100 0 == bind(svr_fd, (struct sockaddr *)addr, (socklen_t)resolved_addr.len));
Craig Tillera82950e2015-09-22 12:33:20 -0700101 GPR_ASSERT(0 == listen(svr_fd, 1));
ctiller18b49ab2014-12-09 14:39:16 -0800102
Craig Tiller85371a22016-02-25 13:55:13 -0800103 gpr_mu_lock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -0700104 connections_complete_before = g_connections_complete;
Craig Tiller85371a22016-02-25 13:55:13 -0800105 gpr_mu_unlock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -0700106
ctiller18b49ab2014-12-09 14:39:16 -0800107 /* connect to it */
murgatroid99dedb9232016-09-26 13:54:04 -0700108 GPR_ASSERT(getsockname(svr_fd, (struct sockaddr *)addr,
109 (socklen_t *)&resolved_addr.len) == 0);
ncteisen274bbbe2017-06-08 14:57:11 -0700110 GRPC_CLOSURE_INIT(&done, must_succeed, NULL, grpc_schedule_on_exec_ctx);
Craig Tiller30ff60e2016-09-23 14:54:27 -0700111 grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set, NULL,
Craig Tillerc0df1c02017-07-17 16:12:33 -0700112 &resolved_addr, GRPC_MILLIS_INF_FUTURE);
ctiller18b49ab2014-12-09 14:39:16 -0800113
114 /* await the connection */
Craig Tillera82950e2015-09-22 12:33:20 -0700115 do {
murgatroid997871f732016-09-23 13:49:05 -0700116 resolved_addr.len = sizeof(addr);
murgatroid99a4601922016-10-12 13:19:40 -0700117 r = accept(svr_fd, (struct sockaddr *)addr,
118 (socklen_t *)&resolved_addr.len);
Craig Tillera82950e2015-09-22 12:33:20 -0700119 } while (r == -1 && errno == EINTR);
120 GPR_ASSERT(r >= 0);
121 close(r);
ctiller18b49ab2014-12-09 14:39:16 -0800122
Craig Tiller85371a22016-02-25 13:55:13 -0800123 gpr_mu_lock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -0700124
Craig Tillera82950e2015-09-22 12:33:20 -0700125 while (g_connections_complete == connections_complete_before) {
Craig Tillerd0a8ae12016-02-18 08:01:19 -0800126 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700127 GPR_ASSERT(GRPC_LOG_IF_ERROR(
128 "pollset_work",
Craig Tiller9a8c3f32017-07-21 13:14:14 -0700129 grpc_pollset_work(&exec_ctx, g_pollset, &worker,
130 grpc_timespec_to_millis_round_up(
131 grpc_timeout_seconds_to_deadline(5)))));
Craig Tiller85371a22016-02-25 13:55:13 -0800132 gpr_mu_unlock(g_mu);
Craig Tiller311445f2016-02-18 07:31:39 -0800133 grpc_exec_ctx_flush(&exec_ctx);
Craig Tiller85371a22016-02-25 13:55:13 -0800134 gpr_mu_lock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700135 }
Craig Tillercb63a9b2015-05-13 09:52:36 -0700136
Craig Tiller85371a22016-02-25 13:55:13 -0800137 gpr_mu_unlock(g_mu);
Craig Tiller311445f2016-02-18 07:31:39 -0800138
139 grpc_exec_ctx_finish(&exec_ctx);
ctiller18b49ab2014-12-09 14:39:16 -0800140}
141
Craig Tillera82950e2015-09-22 12:33:20 -0700142void test_fails(void) {
murgatroid997871f732016-09-23 13:49:05 -0700143 grpc_resolved_address resolved_addr;
144 struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr;
Craig Tillercb63a9b2015-05-13 09:52:36 -0700145 int connections_complete_before;
Craig Tillerdfff1b82015-09-21 14:39:57 -0700146 grpc_closure done;
Craig Tillerf5768a62015-09-22 10:54:34 -0700147 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
ctiller18b49ab2014-12-09 14:39:16 -0800148
Craig Tillera82950e2015-09-22 12:33:20 -0700149 gpr_log(GPR_DEBUG, "test_fails");
Craig Tillere9593352015-07-15 07:35:09 -0700150
murgatroid997871f732016-09-23 13:49:05 -0700151 memset(&resolved_addr, 0, sizeof(resolved_addr));
152 resolved_addr.len = sizeof(struct sockaddr_in);
153 addr->sin_family = AF_INET;
ctiller18b49ab2014-12-09 14:39:16 -0800154
Craig Tiller85371a22016-02-25 13:55:13 -0800155 gpr_mu_lock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -0700156 connections_complete_before = g_connections_complete;
Craig Tiller85371a22016-02-25 13:55:13 -0800157 gpr_mu_unlock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -0700158
ctiller18b49ab2014-12-09 14:39:16 -0800159 /* connect to a broken address */
ncteisen274bbbe2017-06-08 14:57:11 -0700160 GRPC_CLOSURE_INIT(&done, must_fail, NULL, grpc_schedule_on_exec_ctx);
Craig Tiller30ff60e2016-09-23 14:54:27 -0700161 grpc_tcp_client_connect(&exec_ctx, &done, &g_connecting, g_pollset_set, NULL,
Craig Tillerc0df1c02017-07-17 16:12:33 -0700162 &resolved_addr, GRPC_MILLIS_INF_FUTURE);
ctiller18b49ab2014-12-09 14:39:16 -0800163
Craig Tiller85371a22016-02-25 13:55:13 -0800164 gpr_mu_lock(g_mu);
Craig Tillercb63a9b2015-05-13 09:52:36 -0700165
ctiller18b49ab2014-12-09 14:39:16 -0800166 /* wait for the connection callback to finish */
Craig Tillera82950e2015-09-22 12:33:20 -0700167 while (g_connections_complete == connections_complete_before) {
Craig Tillerd0a8ae12016-02-18 08:01:19 -0800168 grpc_pollset_worker *worker = NULL;
Craig Tillerc0df1c02017-07-17 16:12:33 -0700169 grpc_millis polling_deadline = test_deadline();
170 switch (grpc_timer_check(&exec_ctx, &polling_deadline)) {
Craig Tillerb2ef1cf2017-05-30 09:25:48 -0700171 case GRPC_TIMERS_FIRED:
172 break;
173 case GRPC_TIMERS_NOT_CHECKED:
Craig Tillerc0df1c02017-07-17 16:12:33 -0700174 polling_deadline = 0;
Craig Tillerb2ef1cf2017-05-30 09:25:48 -0700175 /* fall through */
176 case GRPC_TIMERS_CHECKED_AND_EMPTY:
177 GPR_ASSERT(GRPC_LOG_IF_ERROR(
178 "pollset_work", grpc_pollset_work(&exec_ctx, g_pollset, &worker,
Craig Tillerc0df1c02017-07-17 16:12:33 -0700179 polling_deadline)));
Craig Tillerb2ef1cf2017-05-30 09:25:48 -0700180 break;
Craig Tiller311445f2016-02-18 07:31:39 -0800181 }
Craig Tiller85371a22016-02-25 13:55:13 -0800182 gpr_mu_unlock(g_mu);
Craig Tiller311445f2016-02-18 07:31:39 -0800183 grpc_exec_ctx_flush(&exec_ctx);
Craig Tiller85371a22016-02-25 13:55:13 -0800184 gpr_mu_lock(g_mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700185 }
Craig Tillercb63a9b2015-05-13 09:52:36 -0700186
Craig Tiller85371a22016-02-25 13:55:13 -0800187 gpr_mu_unlock(g_mu);
Craig Tiller311445f2016-02-18 07:31:39 -0800188 grpc_exec_ctx_finish(&exec_ctx);
ctiller18b49ab2014-12-09 14:39:16 -0800189}
190
Craig Tillerf707d622016-05-06 14:26:12 -0700191static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p,
192 grpc_error *error) {
Craig Tillerf8401102017-04-17 09:47:28 -0700193 grpc_pollset_destroy(exec_ctx, p);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700194}
Craig Tillercb63a9b2015-05-13 09:52:36 -0700195
Craig Tillera82950e2015-09-22 12:33:20 -0700196int main(int argc, char **argv) {
Craig Tillerdfff1b82015-09-21 14:39:57 -0700197 grpc_closure destroyed;
Craig Tillerf5768a62015-09-22 10:54:34 -0700198 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700199 grpc_test_init(argc, argv);
200 grpc_init();
Craig Tillera75d18a2016-02-25 08:45:00 -0800201 g_pollset_set = grpc_pollset_set_create();
Yuchen Zeng47de64c2017-02-22 19:04:38 -0800202 g_pollset = gpr_zalloc(grpc_pollset_size());
Craig Tillera8be91b2016-02-19 16:22:44 -0800203 grpc_pollset_init(g_pollset, &g_mu);
Craig Tillera75d18a2016-02-25 08:45:00 -0800204 grpc_pollset_set_add_pollset(&exec_ctx, g_pollset_set, g_pollset);
Craig Tillera82950e2015-09-22 12:33:20 -0700205 grpc_exec_ctx_finish(&exec_ctx);
206 test_succeeds();
207 gpr_log(GPR_ERROR, "End of first test");
208 test_fails();
Craig Tiller9e5ac1b2017-02-14 22:25:50 -0800209 grpc_pollset_set_destroy(&exec_ctx, g_pollset_set);
ncteisen274bbbe2017-06-08 14:57:11 -0700210 GRPC_CLOSURE_INIT(&destroyed, destroy_pollset, g_pollset,
Craig Tillerd4654562017-01-03 08:45:56 -0800211 grpc_schedule_on_exec_ctx);
Craig Tillera8be91b2016-02-19 16:22:44 -0800212 grpc_pollset_shutdown(&exec_ctx, g_pollset, &destroyed);
Craig Tillera82950e2015-09-22 12:33:20 -0700213 grpc_exec_ctx_finish(&exec_ctx);
214 grpc_shutdown();
Craig Tillera8be91b2016-02-19 16:22:44 -0800215 gpr_free(g_pollset);
ctiller18b49ab2014-12-09 14:39:16 -0800216 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800217}
murgatroid991191b722017-02-08 11:56:52 -0800218
219#else /* GRPC_POSIX_SOCKET */
220
221int main(int argc, char **argv) { return 1; }
222
223#endif /* GRPC_POSIX_SOCKET */