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