• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 #include <grpc/support/string_util.h>
25 
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/gprpp/host_port.h"
28 #include "src/core/lib/iomgr/exec_ctx.h"
29 #include "src/core/lib/slice/slice_internal.h"
30 #include "src/core/lib/transport/metadata.h"
31 
32 #include "test/core/end2end/cq_verifier.h"
33 #include "test/core/util/port.h"
34 #include "test/core/util/test_config.h"
35 
tag(intptr_t i)36 static void* tag(intptr_t i) { return (void*)i; }
37 
run_test(bool wait_for_ready,bool use_service_config)38 static void run_test(bool wait_for_ready, bool use_service_config) {
39   grpc_channel* chan;
40   grpc_call* call;
41   grpc_completion_queue* cq;
42   cq_verifier* cqv;
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   cqv = cq_verifier_create(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   chan = grpc_insecure_channel_create(addr.c_str(), args, nullptr);
83   grpc_slice host = grpc_slice_from_static_string("nonexistant");
84   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2);
85   call =
86       grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
87                                grpc_slice_from_static_string("/service/method"),
88                                &host, deadline, nullptr);
89 
90   memset(ops, 0, sizeof(ops));
91   op = ops;
92   op->op = GRPC_OP_SEND_INITIAL_METADATA;
93   op->data.send_initial_metadata.count = 0;
94   op->flags = (wait_for_ready && !use_service_config)
95                   ? GRPC_INITIAL_METADATA_WAIT_FOR_READY
96                   : 0;
97   op->reserved = nullptr;
98   op++;
99   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
100   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
101   op->data.recv_status_on_client.status = &status;
102   op->data.recv_status_on_client.status_details = &details;
103   op->flags = 0;
104   op->reserved = nullptr;
105   op++;
106   GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
107                                                    (size_t)(op - ops), tag(1),
108                                                    nullptr));
109   /* verify that all tags get completed */
110   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
111   cq_verify(cqv);
112 
113   if (wait_for_ready) {
114     GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED);
115   } else {
116     GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
117   }
118 
119   grpc_completion_queue_shutdown(cq);
120   while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
121                                     nullptr)
122              .type != GRPC_QUEUE_SHUTDOWN)
123     ;
124   grpc_completion_queue_destroy(cq);
125   grpc_call_unref(call);
126   grpc_channel_destroy(chan);
127   cq_verifier_destroy(cqv);
128 
129   grpc_slice_unref(details);
130   grpc_metadata_array_destroy(&trailing_metadata_recv);
131 
132   {
133     grpc_core::ExecCtx exec_ctx;
134     if (args != nullptr) grpc_channel_args_destroy(args);
135   }
136 
137   grpc_shutdown();
138 }
139 
main(int argc,char ** argv)140 int main(int argc, char** argv) {
141   grpc::testing::TestEnvironment env(argc, argv);
142   run_test(false /* wait_for_ready */, false /* use_service_config */);
143   run_test(true /* wait_for_ready */, false /* use_service_config */);
144   run_test(true /* wait_for_ready */, true /* use_service_config */);
145   return 0;
146 }
147