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 <utility>
22
23 #include "absl/status/status.h"
24 #include "absl/status/statusor.h"
25 #include "absl/time/time.h"
26
27 #include <grpc/grpc.h>
28 #include <grpc/grpc_security.h>
29 #include <grpc/impl/propagation_bits.h>
30 #include <grpc/slice.h>
31 #include <grpc/status.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/time.h>
34
35 #include "src/core/lib/channel/channel_args.h"
36 #include "src/core/lib/gprpp/ref_counted_ptr.h"
37 #include "src/core/lib/iomgr/exec_ctx.h"
38 #include "src/core/resolver/endpoint_addresses.h"
39 #include "src/core/resolver/fake/fake_resolver.h"
40 #include "src/core/resolver/resolver.h"
41 #include "src/core/service_config/service_config.h"
42 #include "test/core/end2end/cq_verifier.h"
43 #include "test/core/util/test_config.h"
44
run_test(bool wait_for_ready)45 void run_test(bool wait_for_ready) {
46 gpr_log(GPR_INFO, "TEST: wait_for_ready=%d", wait_for_ready);
47
48 grpc_init();
49
50 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
51 grpc_core::CqVerifier cqv(cq);
52
53 grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>
54 response_generator =
55 grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
56 auto args = grpc_core::ChannelArgs().SetObject(response_generator).ToC();
57
58 // create a call, channel to a non existant server
59 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
60 grpc_channel* chan =
61 grpc_channel_create("fake:nonexistant", creds, args.get());
62 grpc_channel_credentials_release(creds);
63 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2);
64 grpc_call* call = grpc_channel_create_call(
65 chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
66 grpc_slice_from_static_string("/Foo"), nullptr, deadline, nullptr);
67
68 grpc_op ops[6];
69 memset(ops, 0, sizeof(ops));
70 grpc_op* op = ops;
71 op->op = GRPC_OP_SEND_INITIAL_METADATA;
72 op->data.send_initial_metadata.count = 0;
73 op->flags = wait_for_ready ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0;
74 op->reserved = nullptr;
75 op++;
76 grpc_metadata_array trailing_metadata_recv;
77 grpc_metadata_array_init(&trailing_metadata_recv);
78 grpc_status_code status;
79 grpc_slice details;
80 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
81 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
82 op->data.recv_status_on_client.status = &status;
83 op->data.recv_status_on_client.status_details = &details;
84 op->flags = 0;
85 op->reserved = nullptr;
86 op++;
87 GPR_ASSERT(GRPC_CALL_OK ==
88 grpc_call_start_batch(call, ops, (size_t)(op - ops),
89 grpc_core::CqVerifier::tag(1), nullptr));
90
91 {
92 response_generator->WaitForResolverSet(
93 absl::Seconds(5 * grpc_test_slowdown_factor()));
94 grpc_core::ExecCtx exec_ctx;
95 grpc_core::Resolver::Result result;
96 result.addresses = absl::UnavailableError("Resolver transient failure");
97 result.service_config = result.addresses.status();
98 response_generator->SetResponseSynchronously(std::move(result));
99 }
100
101 // verify that all tags get completed
102 cqv.Expect(grpc_core::CqVerifier::tag(1), true);
103 cqv.Verify();
104
105 gpr_log(GPR_INFO, "call status: %d", status);
106 if (wait_for_ready) {
107 GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED);
108 } else {
109 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
110 }
111
112 grpc_slice_unref(details);
113 grpc_metadata_array_destroy(&trailing_metadata_recv);
114
115 grpc_completion_queue_shutdown(cq);
116 while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
117 nullptr)
118 .type != GRPC_QUEUE_SHUTDOWN) {
119 }
120 grpc_completion_queue_destroy(cq);
121 grpc_call_unref(call);
122 grpc_channel_destroy(chan);
123
124 grpc_shutdown();
125 }
126
main(int argc,char ** argv)127 int main(int argc, char** argv) {
128 grpc::testing::TestEnvironment env(&argc, argv);
129 run_test(true /* wait_for_ready */);
130 run_test(false /* wait_for_ready */);
131 return 0;
132 }
133