1 /*
2 *
3 * Copyright 2015 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
25 #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
26 #include "src/core/lib/iomgr/exec_ctx.h"
27 #include "test/core/end2end/cq_verifier.h"
28 #include "test/core/util/test_config.h"
29
tag(intptr_t i)30 static void* tag(intptr_t i) { return (void*)i; }
31
run_test(bool wait_for_ready)32 void run_test(bool wait_for_ready) {
33 gpr_log(GPR_INFO, "TEST: wait_for_ready=%d", wait_for_ready);
34
35 grpc_init();
36
37 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
38 cq_verifier* cqv = cq_verifier_create(cq);
39
40 grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>
41 response_generator =
42 grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
43 grpc_arg arg = grpc_core::FakeResolverResponseGenerator::MakeChannelArg(
44 response_generator.get());
45 grpc_channel_args args = {1, &arg};
46
47 /* create a call, channel to a non existant server */
48 grpc_channel* chan =
49 grpc_insecure_channel_create("fake:nonexistant", &args, nullptr);
50 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2);
51 grpc_call* call = grpc_channel_create_call(
52 chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
53 grpc_slice_from_static_string("/Foo"), nullptr, deadline, nullptr);
54
55 grpc_op ops[6];
56 memset(ops, 0, sizeof(ops));
57 grpc_op* op = ops;
58 op->op = GRPC_OP_SEND_INITIAL_METADATA;
59 op->data.send_initial_metadata.count = 0;
60 op->flags = wait_for_ready ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0;
61 op->reserved = nullptr;
62 op++;
63 grpc_metadata_array trailing_metadata_recv;
64 grpc_metadata_array_init(&trailing_metadata_recv);
65 grpc_status_code status;
66 grpc_slice details;
67 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
68 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
69 op->data.recv_status_on_client.status = &status;
70 op->data.recv_status_on_client.status_details = &details;
71 op->flags = 0;
72 op->reserved = nullptr;
73 op++;
74 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
75 (size_t)(op - ops), tag(1),
76 nullptr));
77
78 {
79 grpc_core::ExecCtx exec_ctx;
80 response_generator->SetFailure();
81 }
82
83 /* verify that all tags get completed */
84 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
85 cq_verify(cqv);
86
87 gpr_log(GPR_INFO, "call status: %d", status);
88 if (wait_for_ready) {
89 GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED);
90 } else {
91 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
92 }
93
94 grpc_slice_unref(details);
95 grpc_metadata_array_destroy(&trailing_metadata_recv);
96
97 grpc_completion_queue_shutdown(cq);
98 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
99 nullptr)
100 .type != GRPC_QUEUE_SHUTDOWN)
101 ;
102 grpc_completion_queue_destroy(cq);
103 grpc_call_unref(call);
104 grpc_channel_destroy(chan);
105 cq_verifier_destroy(cqv);
106
107 grpc_shutdown();
108 }
109
main(int argc,char ** argv)110 int main(int argc, char** argv) {
111 grpc::testing::TestEnvironment env(argc, argv);
112 run_test(true /* wait_for_ready */);
113 run_test(false /* wait_for_ready */);
114 return 0;
115 }
116