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 /** \file Verify that status ordering rules are obeyed.
20 \ref doc/status_ordering.md */
21
22 #include "test/core/end2end/end2end_tests.h"
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <grpc/byte_buffer.h>
28 #include <grpc/support/alloc.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/time.h>
31 #include "test/core/end2end/cq_verifier.h"
32
tag(intptr_t t)33 static void* tag(intptr_t t) { return (void*)t; }
34
begin_test(grpc_end2end_test_config config,const char * test_name,grpc_channel_args * client_args,grpc_channel_args * server_args,bool request_status_early)35 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
36 const char* test_name,
37 grpc_channel_args* client_args,
38 grpc_channel_args* server_args,
39 bool request_status_early) {
40 grpc_end2end_test_fixture f;
41 gpr_log(GPR_INFO, "Running test: %s/%s/request_status_early=%s", test_name,
42 config.name, request_status_early ? "true" : "false");
43 f = config.create_fixture(client_args, server_args);
44 config.init_server(&f, server_args);
45 config.init_client(&f, client_args);
46 return f;
47 }
48
n_seconds_from_now(int n)49 static gpr_timespec n_seconds_from_now(int n) {
50 return grpc_timeout_seconds_to_deadline(n);
51 }
52
five_seconds_from_now(void)53 static gpr_timespec five_seconds_from_now(void) {
54 return n_seconds_from_now(5);
55 }
56
drain_cq(grpc_completion_queue * cq)57 static void drain_cq(grpc_completion_queue* cq) {
58 grpc_event ev;
59 do {
60 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
61 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
62 }
63
shutdown_server(grpc_end2end_test_fixture * f)64 static void shutdown_server(grpc_end2end_test_fixture* f) {
65 if (!f->server) return;
66 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
67 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
68 grpc_timeout_seconds_to_deadline(5),
69 nullptr)
70 .type == GRPC_OP_COMPLETE);
71 grpc_server_destroy(f->server);
72 f->server = nullptr;
73 }
74
shutdown_client(grpc_end2end_test_fixture * f)75 static void shutdown_client(grpc_end2end_test_fixture* f) {
76 if (!f->client) return;
77 grpc_channel_destroy(f->client);
78 f->client = nullptr;
79 }
80
end_test(grpc_end2end_test_fixture * f)81 static void end_test(grpc_end2end_test_fixture* f) {
82 shutdown_server(f);
83 shutdown_client(f);
84
85 grpc_completion_queue_shutdown(f->cq);
86 drain_cq(f->cq);
87 grpc_completion_queue_destroy(f->cq);
88 grpc_completion_queue_destroy(f->shutdown_cq);
89 }
90
91 /* Client sends a request with payload, server reads then returns status. */
test(grpc_end2end_test_config config,bool request_status_early)92 static void test(grpc_end2end_test_config config, bool request_status_early) {
93 grpc_call* c;
94 grpc_call* s;
95 grpc_slice response_payload1_slice = grpc_slice_from_copied_string("hello");
96 grpc_byte_buffer* response_payload1 =
97 grpc_raw_byte_buffer_create(&response_payload1_slice, 1);
98 grpc_slice response_payload2_slice = grpc_slice_from_copied_string("world");
99 grpc_byte_buffer* response_payload2 =
100 grpc_raw_byte_buffer_create(&response_payload2_slice, 1);
101 grpc_end2end_test_fixture f =
102 begin_test(config, "streaming_error_response", nullptr, nullptr,
103 request_status_early);
104 cq_verifier* cqv = cq_verifier_create(f.cq);
105 grpc_op ops[6];
106 grpc_op* op;
107 grpc_metadata_array initial_metadata_recv;
108 grpc_metadata_array trailing_metadata_recv;
109 grpc_metadata_array request_metadata_recv;
110 grpc_byte_buffer* response_payload1_recv = nullptr;
111 grpc_byte_buffer* response_payload2_recv = nullptr;
112 grpc_call_details call_details;
113 grpc_status_code status = GRPC_STATUS_OK;
114 grpc_call_error error;
115 grpc_slice details;
116 int was_cancelled = 2;
117
118 gpr_timespec deadline = five_seconds_from_now();
119 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
120 grpc_slice_from_static_string("/foo"), nullptr,
121 deadline, nullptr);
122 GPR_ASSERT(c);
123
124 grpc_metadata_array_init(&initial_metadata_recv);
125 grpc_metadata_array_init(&trailing_metadata_recv);
126 grpc_metadata_array_init(&request_metadata_recv);
127 grpc_call_details_init(&call_details);
128
129 memset(ops, 0, sizeof(ops));
130 op = ops;
131 op->op = GRPC_OP_SEND_INITIAL_METADATA;
132 op->data.send_initial_metadata.count = 0;
133 op++;
134 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
135 op++;
136 op->op = GRPC_OP_RECV_INITIAL_METADATA;
137 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
138 op++;
139 op->op = GRPC_OP_RECV_MESSAGE;
140 op->data.recv_message.recv_message = &response_payload1_recv;
141 op++;
142 if (request_status_early) {
143 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
144 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
145 op->data.recv_status_on_client.status = &status;
146 op->data.recv_status_on_client.status_details = &details;
147 op++;
148 }
149 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
150 nullptr);
151 GPR_ASSERT(GRPC_CALL_OK == error);
152
153 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
154 f.server, &s, &call_details,
155 &request_metadata_recv, f.cq, f.cq, tag(101)));
156 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
157 cq_verify(cqv);
158
159 memset(ops, 0, sizeof(ops));
160 op = ops;
161 op->op = GRPC_OP_SEND_INITIAL_METADATA;
162 op->data.send_initial_metadata.count = 0;
163 op++;
164 op->op = GRPC_OP_SEND_MESSAGE;
165 op->data.send_message.send_message = response_payload1;
166 op++;
167 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
168 nullptr);
169 GPR_ASSERT(GRPC_CALL_OK == error);
170
171 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
172 if (!request_status_early) {
173 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
174 }
175 cq_verify(cqv);
176
177 memset(ops, 0, sizeof(ops));
178 op = ops;
179 op->op = GRPC_OP_SEND_MESSAGE;
180 op->data.send_message.send_message = response_payload2;
181 op++;
182 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
183 nullptr);
184 GPR_ASSERT(GRPC_CALL_OK == error);
185
186 CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
187
188 if (!request_status_early) {
189 memset(ops, 0, sizeof(ops));
190 op = ops;
191 op->op = GRPC_OP_RECV_MESSAGE;
192 op->data.recv_message.recv_message = &response_payload2_recv;
193 op++;
194 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
195 nullptr);
196 GPR_ASSERT(GRPC_CALL_OK == error);
197
198 CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
199 cq_verify(cqv);
200 }
201
202 memset(ops, 0, sizeof(ops));
203 op = ops;
204 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
205 op->data.recv_close_on_server.cancelled = &was_cancelled;
206 op++;
207 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
208 op->data.send_status_from_server.trailing_metadata_count = 0;
209 op->data.send_status_from_server.status = GRPC_STATUS_FAILED_PRECONDITION;
210 grpc_slice status_details = grpc_slice_from_static_string("xyz");
211 op->data.send_status_from_server.status_details = &status_details;
212 op++;
213 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
214 nullptr);
215 GPR_ASSERT(GRPC_CALL_OK == error);
216
217 CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
218 if (request_status_early) {
219 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
220 }
221 cq_verify(cqv);
222
223 if (!request_status_early) {
224 memset(ops, 0, sizeof(ops));
225 op = ops;
226 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
227 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
228 op->data.recv_status_on_client.status = &status;
229 op->data.recv_status_on_client.status_details = &details;
230 op++;
231 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
232 nullptr);
233 GPR_ASSERT(GRPC_CALL_OK == error);
234
235 CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
236 cq_verify(cqv);
237
238 GPR_ASSERT(response_payload1_recv != nullptr);
239 GPR_ASSERT(response_payload2_recv != nullptr);
240 }
241
242 GPR_ASSERT(status == GRPC_STATUS_FAILED_PRECONDITION);
243 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
244 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
245 GPR_ASSERT(was_cancelled == 1);
246
247 grpc_slice_unref(details);
248 grpc_metadata_array_destroy(&initial_metadata_recv);
249 grpc_metadata_array_destroy(&trailing_metadata_recv);
250 grpc_metadata_array_destroy(&request_metadata_recv);
251 grpc_call_details_destroy(&call_details);
252
253 grpc_call_unref(c);
254 grpc_call_unref(s);
255
256 cq_verifier_destroy(cqv);
257
258 grpc_byte_buffer_destroy(response_payload1);
259 grpc_byte_buffer_destroy(response_payload2);
260 grpc_byte_buffer_destroy(response_payload1_recv);
261 grpc_byte_buffer_destroy(response_payload2_recv);
262
263 end_test(&f);
264 config.tear_down_data(&f);
265 }
266
streaming_error_response(grpc_end2end_test_config config)267 void streaming_error_response(grpc_end2end_test_config config) {
268 test(config, false);
269 test(config, true);
270 }
271
streaming_error_response_pre_init(void)272 void streaming_error_response_pre_init(void) {}
273