Debian Codegraph @

test/core/end2end/tests/cancel_with_status.cc

raw
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 <inttypes.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <string>
24
25 #include <grpc/grpc.h>
26 #include <grpc/impl/codegen/propagation_bits.h>
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/string_util.h>
32
33 #include "src/core/lib/surface/event_string.h"
34 #include "test/core/end2end/cq_verifier.h"
35 #include "test/core/end2end/end2end_tests.h"
36 #include "test/core/util/test_config.h"
37
38 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
39
40 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
41 const char* test_name,
42 size_t num_ops,
43 grpc_channel_args* client_args,
44 grpc_channel_args* server_args) {
45 grpc_end2end_test_fixture f;
46 gpr_log(GPR_INFO, "Running test: %s/%s [%" PRIdPTR " ops]", test_name,
47 config.name, num_ops);
48 f = config.create_fixture(client_args, server_args);
49 config.init_server(&f, server_args);
50 config.init_client(&f, client_args);
51 return f;
52 }
53
54 static gpr_timespec n_seconds_from_now(int n) {
55 return grpc_timeout_seconds_to_deadline(n);
56 }
57
58 static gpr_timespec five_seconds_from_now(void) {
59 return n_seconds_from_now(5);
60 }
61
62 static void drain_cq(grpc_completion_queue* cq) {
63 grpc_event ev;
64 do {
65 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
66 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
67 }
68
69 static void shutdown_server(grpc_end2end_test_fixture* f) {
70 if (!f->server) return;
71 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
72 grpc_event ev = grpc_completion_queue_next(
73 f->cq, grpc_timeout_seconds_to_deadline(5), nullptr);
74 gpr_log(GPR_DEBUG, "shutdown event: %s", grpc_event_string(&ev).c_str());
75 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
76 GPR_ASSERT(ev.tag == tag(1000));
77 grpc_server_destroy(f->server);
78 f->server = nullptr;
79 }
80
81 static void shutdown_client(grpc_end2end_test_fixture* f) {
82 if (!f->client) return;
83 grpc_channel_destroy(f->client);
84 f->client = nullptr;
85 }
86
87 static void end_test(grpc_end2end_test_fixture* f) {
88 shutdown_client(f);
89 shutdown_server(f);
90
91 grpc_completion_queue_shutdown(f->cq);
92 drain_cq(f->cq);
93 grpc_completion_queue_destroy(f->cq);
94 }
95
96 static void simple_request_body(grpc_end2end_test_config /*config*/,
97 grpc_end2end_test_fixture f, size_t num_ops) {
98 grpc_call* c;
99 grpc_core::CqVerifier cqv(f.cq);
100 grpc_op ops[6];
101 grpc_op* op;
102 grpc_metadata_array initial_metadata_recv;
103 grpc_metadata_array trailing_metadata_recv;
104 grpc_status_code status;
105 grpc_call_error error;
106 grpc_slice details;
107
108 gpr_log(GPR_DEBUG, "test with %" PRIuPTR " ops", num_ops);
109
110 gpr_timespec deadline = five_seconds_from_now();
111 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
112 grpc_slice_from_static_string("/foo"), nullptr,
113 deadline, nullptr);
114 GPR_ASSERT(c);
115
116 grpc_metadata_array_init(&initial_metadata_recv);
117 grpc_metadata_array_init(&trailing_metadata_recv);
118
119 memset(ops, 0, sizeof(ops));
120 op = ops;
121 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
122 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
123 op->data.recv_status_on_client.status = &status;
124 op->data.recv_status_on_client.status_details = &details;
125 op->flags = 0;
126 op->reserved = nullptr;
127 op++;
128 op->op = GRPC_OP_RECV_INITIAL_METADATA;
129 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
130 op->flags = 0;
131 op->reserved = nullptr;
132 op++;
133 op->op = GRPC_OP_SEND_INITIAL_METADATA;
134 op->data.send_initial_metadata.count = 0;
135 op->flags = 0;
136 op->reserved = nullptr;
137 op++;
138 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
139 op->flags = 0;
140 op->reserved = nullptr;
141 op++;
142 GPR_ASSERT(num_ops <= (size_t)(op - ops));
143 error = grpc_call_start_batch(c, ops, num_ops, tag(1), nullptr);
144 GPR_ASSERT(GRPC_CALL_OK == error);
145
146 char* dynamic_string = gpr_strdup("xyz");
147 grpc_call_cancel_with_status(c, GRPC_STATUS_UNIMPLEMENTED, dynamic_string,
148 nullptr);
149 // The API of \a description allows for it to be a dynamic/non-const
150 // string, test this guarantee.
151 gpr_free(dynamic_string);
152
153 cqv.Expect(tag(1), true);
154 cqv.Verify();
155
156 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
157 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
158
159 grpc_slice_unref(details);
160 grpc_metadata_array_destroy(&initial_metadata_recv);
161 grpc_metadata_array_destroy(&trailing_metadata_recv);
162
163 grpc_call_unref(c);
164 }
165
166 static void test_invoke_simple_request(grpc_end2end_test_config config,
167 size_t num_ops) {
168 grpc_end2end_test_fixture f;
169
170 f = begin_test(config, "test_invoke_simple_request", num_ops, nullptr,
171 nullptr);
172 simple_request_body(config, f, num_ops);
173 end_test(&f);
174 config.tear_down_data(&f);
175 }
176
177 void cancel_with_status(grpc_end2end_test_config config) {
178 size_t i;
179 for (i = 1; i <= 4; i++) {
180 test_invoke_simple_request(config, i);
181 }
182 }
183
184 void cancel_with_status_pre_init(void) {}