resolve_address_posix_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * 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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * 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.
16  *
17  */
18 
19 #include <net/if.h>
20 #include <string.h>
21 #include <sys/un.h>
22 
23 #include <string>
24 
25 #include "absl/strings/str_format.h"
26 
27 #include <grpc/grpc.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/sync.h>
31 #include <grpc/support/time.h>
32 
34 #include "src/core/lib/gpr/env.h"
37 #include "src/core/lib/gprpp/thd.h"
42 #include "test/core/util/cmdline.h"
44 
47 }
48 
49 typedef struct args_struct {
53  bool done; // guarded by mu
54  grpc_pollset* pollset; // guarded by mu
56 } args_struct;
57 
58 static void do_nothing(void* /*arg*/, grpc_error_handle /*error*/) {}
59 
61  gpr_event_init(&args->ev);
62  args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
63  grpc_pollset_init(args->pollset, &args->mu);
64  args->pollset_set = grpc_pollset_set_create();
65  grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
66  args->done = false;
67 }
68 
71  args->thd.Join();
72  // Don't need to explicitly destruct args->thd since
73  // args is actually going to be destructed, not just freed
74  grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
75  grpc_pollset_set_destroy(args->pollset_set);
76  grpc_closure do_nothing_cb;
77  GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
78  grpc_schedule_on_exec_ctx);
79  grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
80  // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
82  grpc_pollset_destroy(args->pollset);
83  gpr_free(args->pollset);
84 }
85 
89 }
90 
91 static void actually_poll(void* argsp) {
92  args_struct* args = static_cast<args_struct*>(argsp);
93  grpc_core::Timestamp deadline = n_sec_deadline(10);
94  while (true) {
96  {
98  if (args->done) {
99  break;
100  }
101  grpc_core::Duration time_left =
102  deadline - grpc_core::ExecCtx::Get()->Now();
103  gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, args->done,
104  time_left.millis());
105  GPR_ASSERT(time_left >= grpc_core::Duration::Zero());
106  grpc_pollset_worker* worker = nullptr;
108  "pollset_work",
109  grpc_pollset_work(args->pollset, &worker, n_sec_deadline(1)));
110  }
111  }
112  gpr_event_set(&args->ev, reinterpret_cast<void*>(1));
113 }
114 
116  args->thd = grpc_core::Thread("grpc_poll_pollset", actually_poll, args);
117  args->thd.Start();
118 }
119 
120 namespace {
121 
122 void MustSucceed(args_struct* args,
123  absl::StatusOr<std::vector<grpc_resolved_address>> result) {
124  GPR_ASSERT(result.ok());
125  GPR_ASSERT(!result->empty());
127  args->done = true;
128  GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(args->pollset, nullptr));
129 }
130 
131 } // namespace
132 
133 static void resolve_address_must_succeed(const char* target) {
136  args_init(&args);
139  target, "1" /* port number */, args.pollset_set,
140  [&args](absl::StatusOr<std::vector<grpc_resolved_address>> result) {
141  MustSucceed(&args, std::move(result));
142  });
144  args_finish(&args);
145 }
146 
148  char* arbitrary_interface_name = static_cast<char*>(gpr_zalloc(IF_NAMESIZE));
149  int interface_index = 0;
150  // Probe candidate interface index numbers until we find one that the
151  // system recognizes, and then use that for the test.
152  for (size_t i = 1; i < 65536; i++) {
153  if (if_indextoname(i, arbitrary_interface_name) != nullptr) {
155  "Found interface at index %" PRIuPTR
156  " named %s. Will use this for the test",
157  i, arbitrary_interface_name);
158  interface_index = static_cast<int>(i);
159  break;
160  }
161  }
162  GPR_ASSERT(strlen(arbitrary_interface_name) > 0);
163  // Test resolution of an ipv6 address with a named scope ID
164  gpr_log(GPR_DEBUG, "test resolution with a named scope ID");
165  std::string target_with_named_scope_id =
166  absl::StrFormat("fe80::1234%%%s", arbitrary_interface_name);
167  resolve_address_must_succeed(target_with_named_scope_id.c_str());
168  gpr_free(arbitrary_interface_name);
169  // Test resolution of an ipv6 address with a numeric scope ID
170  gpr_log(GPR_DEBUG, "test resolution with a numeric scope ID");
171  std::string target_with_numeric_scope_id =
172  absl::StrFormat("fe80::1234%%%d", interface_index);
173  resolve_address_must_succeed(target_with_numeric_scope_id.c_str());
174 }
175 
176 int main(int argc, char** argv) {
177  // First set the resolver type based off of --resolver
178  const char* resolver_type = nullptr;
179  gpr_cmdline* cl = gpr_cmdline_create("resolve address test");
180  gpr_cmdline_add_string(cl, "resolver", "Resolver type (ares or native)",
181  &resolver_type);
182  // In case that there are more than one argument on the command line,
183  // --resolver will always be the first one, so only parse the first argument
184  // (other arguments may be unknown to cl)
185  gpr_cmdline_parse(cl, argc > 2 ? 2 : argc, argv);
186  grpc_core::UniquePtr<char> resolver =
187  GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
188  if (strlen(resolver.get()) != 0) {
189  gpr_log(GPR_INFO, "Warning: overriding resolver setting of %s",
190  resolver.get());
191  }
192  if (resolver_type != nullptr && gpr_stricmp(resolver_type, "native") == 0) {
193  GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "native");
194  } else if (resolver_type != nullptr &&
195  gpr_stricmp(resolver_type, "ares") == 0) {
196  GPR_GLOBAL_CONFIG_SET(grpc_dns_resolver, "ares");
197  } else {
198  gpr_log(GPR_ERROR, "--resolver_type was not set to ares or native");
199  abort();
200  }
201  grpc::testing::TestEnvironment env(&argc, argv);
202  grpc_init();
203 
204  {
207  // c-ares resolver doesn't support UDS (ability for native DNS resolver
208  // to handle this is only expected to be used by servers, which
209  // unconditionally use the native DNS resolver).
210  grpc_core::UniquePtr<char> resolver =
211  GPR_GLOBAL_CONFIG_GET(grpc_dns_resolver);
212  }
214 
215  grpc_shutdown();
216  return 0;
217 }
grpc_pollset_worker
struct grpc_pollset_worker grpc_pollset_worker
Definition: pollset.h:39
GRPC_CLOSURE_INIT
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
Definition: closure.h:115
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_pollset_size
size_t grpc_pollset_size(void)
Definition: pollset.cc:56
args_struct::ev
gpr_event ev
Definition: resolve_address_posix_test.cc:51
iomgr.h
do_nothing
static void do_nothing(void *, grpc_error_handle)
Definition: resolve_address_posix_test.cc:58
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
log.h
generate.env
env
Definition: generate.py:37
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
n_sec_deadline
static grpc_core::Timestamp n_sec_deadline(int seconds)
Definition: resolve_address_posix_test.cc:86
main
int main(int argc, char **argv)
Definition: resolve_address_posix_test.cc:176
GPR_GLOBAL_CONFIG_GET
#define GPR_GLOBAL_CONFIG_GET(name)
Definition: global_config_generic.h:24
grpc_core::MutexLockForGprMu
Definition: src/core/lib/gprpp/sync.h:152
grpc_pollset_set
struct grpc_pollset_set grpc_pollset_set
Definition: iomgr_fwd.h:23
string.h
grpc_pollset_set_create
grpc_pollset_set * grpc_pollset_set_create()
Definition: pollset_set.cc:29
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
gpr_event_set
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Definition: sync.cc:59
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
gpr_cmdline_destroy
void gpr_cmdline_destroy(gpr_cmdline *cl)
Definition: cmdline.cc:79
resolve_address.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
args_struct
struct args_struct args_struct
env.h
grpc_pollset_work
grpc_error_handle grpc_pollset_work(grpc_pollset *pollset, grpc_pollset_worker **worker, grpc_core::Timestamp deadline)
Definition: pollset.cc:45
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
time.h
grpc_pollset_init
void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu)
Definition: pollset.cc:33
actually_poll
static void actually_poll(void *argsp)
Definition: resolve_address_posix_test.cc:91
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
resolve_address_must_succeed
static void resolve_address_must_succeed(const char *target)
Definition: resolve_address_posix_test.cc:133
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_pollset_set_del_pollset
void grpc_pollset_set_del_pollset(grpc_pollset_set *pollset_set, grpc_pollset *pollset)
Definition: pollset_set.cc:42
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
worker
Definition: worker.py:1
gpr_cmdline_add_string
void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help, const char **value)
Definition: cmdline.cc:115
grpc.h
grpc_pollset_set_destroy
void grpc_pollset_set_destroy(grpc_pollset_set *pollset_set)
Definition: pollset_set.cc:33
args_finish
void args_finish(args_struct *args)
Definition: resolve_address_posix_test.cc:69
test_deadline
static gpr_timespec test_deadline(void)
Definition: resolve_address_posix_test.cc:45
args_struct::pollset_set
grpc_pollset_set * pollset_set
Definition: resolve_address_posix_test.cc:55
time.h
gpr_stricmp
int gpr_stricmp(const char *a, const char *b)
Definition: string.cc:282
gpr_event_init
GPRAPI void gpr_event_init(gpr_event *ev)
Definition: sync.cc:54
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
args_struct::thd
grpc_core::Thread thd
Definition: resolve_address_posix_test.cc:50
grpc_pollset_kick
grpc_error_handle grpc_pollset_kick(grpc_pollset *pollset, grpc_pollset_worker *specific_worker)
Definition: pollset.cc:51
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::UniquePtr
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: src/core/lib/gprpp/memory.h:43
GPR_GLOBAL_CONFIG_SET
#define GPR_GLOBAL_CONFIG_SET(name, value)
Definition: global_config_generic.h:26
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
executor.h
args_init
void args_init(args_struct *args)
Definition: resolve_address_posix_test.cc:60
test_config.h
grpc_core::Duration::Zero
static constexpr Duration Zero()
Definition: src/core/lib/gprpp/time.h:130
poll_pollset_until_request_done
static void poll_pollset_until_request_done(args_struct *args)
Definition: resolve_address_posix_test.cc:115
gpr_event
Definition: impl/codegen/sync_generic.h:31
grpc_core::Duration::millis
constexpr int64_t millis() const
Definition: src/core/lib/gprpp/time.h:208
test_named_and_numeric_scope_ids
static void test_named_and_numeric_scope_ids(void)
Definition: resolve_address_posix_test.cc:147
args_struct::pollset
grpc_pollset * pollset
Definition: resolve_address_posix_test.cc:54
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
grpc_core::GetDNSResolver
DNSResolver * GetDNSResolver()
Definition: resolve_address.cc:38
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_pollset_shutdown
void grpc_pollset_shutdown(grpc_pollset *pollset, grpc_closure *closure)
Definition: pollset.cc:37
alloc.h
cmdline.h
dns_resolver_selection.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::DNSResolver::ResolveName
virtual TaskHandle ResolveName(absl::string_view name, absl::string_view default_port, grpc_pollset_set *interested_parties, std::function< void(absl::StatusOr< std::vector< grpc_resolved_address >>)> on_done)=0
thd.h
args_struct
Definition: resolve_address_posix_test.cc:49
gpr_cmdline_create
gpr_cmdline * gpr_cmdline_create(const char *description)
Definition: cmdline.cc:66
grpc_core::Thread
Definition: thd.h:43
gpr_cmdline
Definition: cmdline.cc:48
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
gpr_timespec
Definition: gpr_types.h:50
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
grpc_core::Timestamp::FromTimespecRoundUp
static Timestamp FromTimespecRoundUp(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:136
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
args_struct::mu
gpr_mu * mu
Definition: resolve_address_posix_test.cc:52
grpc_pollset
Definition: bm_cq_multiple_threads.cc:37
grpc_pollset_destroy
void grpc_pollset_destroy(grpc_pollset *pollset)
Definition: pollset.cc:41
sync.h
grpc_closure
Definition: closure.h:56
grpc_pollset_set_add_pollset
void grpc_pollset_set_add_pollset(grpc_pollset_set *pollset_set, grpc_pollset *pollset)
Definition: pollset_set.cc:37
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
IF_NAMESIZE
#define IF_NAMESIZE
Definition: ares_ipv6.h:77
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
gpr_cmdline_parse
int gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv)
Definition: cmdline.cc:309
args_struct::done
bool done
Definition: resolve_address_posix_test.cc:53


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:04