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