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 <grpc/credentials.h>
20 #include <grpc/grpc.h>
21 #include <grpc/grpc_security.h>
22 #include <grpc/impl/channel_arg_names.h>
23 #include <grpc/impl/propagation_bits.h>
24 #include <grpc/slice.h>
25 #include <grpc/status.h>
26 #include <grpc/support/time.h>
27 #include <string.h>
28
29 #include <string>
30
31 #include "absl/log/check.h"
32 #include "absl/log/log.h"
33 #include "src/core/lib/channel/channel_args.h"
34 #include "src/core/lib/iomgr/exec_ctx.h"
35 #include "src/core/util/host_port.h"
36 #include "test/core/end2end/cq_verifier.h"
37 #include "test/core/test_util/port.h"
38 #include "test/core/test_util/test_config.h"
39
run_test(bool wait_for_ready,bool use_service_config)40 static void run_test(bool wait_for_ready, bool use_service_config) {
41 grpc_channel* chan;
42 grpc_call* call;
43 grpc_completion_queue* cq;
44 grpc_op ops[6];
45 grpc_op* op;
46 grpc_metadata_array trailing_metadata_recv;
47 grpc_status_code status;
48 grpc_slice details;
49
50 LOG(INFO) << "TEST: wait_for_ready=" << wait_for_ready
51 << " use_service_config=" << use_service_config;
52
53 grpc_init();
54
55 grpc_metadata_array_init(&trailing_metadata_recv);
56
57 cq = grpc_completion_queue_create_for_next(nullptr);
58 grpc_core::CqVerifier cqv(cq);
59
60 // if using service config, create channel args
61 grpc_channel_args* args = nullptr;
62 if (use_service_config) {
63 CHECK(wait_for_ready);
64 grpc_arg arg;
65 arg.type = GRPC_ARG_STRING;
66 arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG);
67 arg.value.string = const_cast<char*>(
68 "{\n"
69 " \"methodConfig\": [ {\n"
70 " \"name\": [\n"
71 " { \"service\": \"service\", \"method\": \"method\" }\n"
72 " ],\n"
73 " \"waitForReady\": true\n"
74 " } ]\n"
75 "}");
76 args = grpc_channel_args_copy_and_add(args, &arg, 1);
77 }
78
79 // create a call, channel to a port which will refuse connection
80 int port = grpc_pick_unused_port_or_die();
81 std::string addr = grpc_core::JoinHostPort("127.0.0.1", port);
82 LOG(INFO) << "server: " << addr;
83 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
84 chan = grpc_channel_create(addr.c_str(), creds, args);
85 grpc_channel_credentials_release(creds);
86 grpc_slice host = grpc_slice_from_static_string("nonexistent");
87 gpr_timespec deadline =
88 grpc_timeout_seconds_to_deadline(wait_for_ready ? 2 : 600);
89 call =
90 grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
91 grpc_slice_from_static_string("/service/method"),
92 &host, deadline, nullptr);
93
94 memset(ops, 0, sizeof(ops));
95 op = ops;
96 op->op = GRPC_OP_SEND_INITIAL_METADATA;
97 op->data.send_initial_metadata.count = 0;
98 op->flags = (wait_for_ready && !use_service_config)
99 ? GRPC_INITIAL_METADATA_WAIT_FOR_READY
100 : 0;
101 op->reserved = nullptr;
102 op++;
103 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
104 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
105 op->data.recv_status_on_client.status = &status;
106 op->data.recv_status_on_client.status_details = &details;
107 op->flags = 0;
108 op->reserved = nullptr;
109 op++;
110 CHECK_EQ(GRPC_CALL_OK,
111 grpc_call_start_batch(call, ops, (size_t)(op - ops),
112 grpc_core::CqVerifier::tag(1), nullptr));
113 // verify that all tags get completed
114 cqv.Expect(grpc_core::CqVerifier::tag(1), true);
115 cqv.Verify();
116
117 if (wait_for_ready) {
118 CHECK_EQ(status, GRPC_STATUS_DEADLINE_EXCEEDED);
119 } else {
120 CHECK_EQ(status, GRPC_STATUS_UNAVAILABLE);
121 }
122
123 grpc_completion_queue_shutdown(cq);
124 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
125 nullptr)
126 .type != GRPC_QUEUE_SHUTDOWN) {
127 }
128 grpc_completion_queue_destroy(cq);
129 grpc_call_unref(call);
130 grpc_channel_destroy(chan);
131
132 grpc_slice_unref(details);
133 grpc_metadata_array_destroy(&trailing_metadata_recv);
134
135 {
136 grpc_core::ExecCtx exec_ctx;
137 if (args != nullptr) grpc_channel_args_destroy(args);
138 }
139
140 grpc_shutdown();
141 }
142
main(int argc,char ** argv)143 int main(int argc, char** argv) {
144 grpc::testing::TestEnvironment env(&argc, argv);
145 run_test(false /* wait_for_ready */, false /* use_service_config */);
146 run_test(true /* wait_for_ready */, false /* use_service_config */);
147 run_test(true /* wait_for_ready */, true /* use_service_config */);
148 return 0;
149 }
150