• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,bool recv_message_separately)92 static void test(grpc_end2end_test_config config, bool request_status_early,
93                  bool recv_message_separately) {
94   grpc_call* c;
95   grpc_call* s;
96   grpc_slice response_payload1_slice = grpc_slice_from_copied_string("hello");
97   grpc_byte_buffer* response_payload1 =
98       grpc_raw_byte_buffer_create(&response_payload1_slice, 1);
99   grpc_slice response_payload2_slice = grpc_slice_from_copied_string("world");
100   grpc_byte_buffer* response_payload2 =
101       grpc_raw_byte_buffer_create(&response_payload2_slice, 1);
102   grpc_end2end_test_fixture f =
103       begin_test(config, "streaming_error_response", nullptr, nullptr,
104                  request_status_early);
105   cq_verifier* cqv = cq_verifier_create(f.cq);
106   grpc_op ops[6];
107   grpc_op* op;
108   grpc_metadata_array initial_metadata_recv;
109   grpc_metadata_array trailing_metadata_recv;
110   grpc_metadata_array request_metadata_recv;
111   grpc_byte_buffer* response_payload1_recv = nullptr;
112   grpc_byte_buffer* response_payload2_recv = nullptr;
113   grpc_call_details call_details;
114   grpc_status_code status = GRPC_STATUS_OK;
115   grpc_call_error error;
116   grpc_slice details;
117   int was_cancelled = 2;
118 
119   gpr_timespec deadline = five_seconds_from_now();
120   GPR_ASSERT(!recv_message_separately || request_status_early);
121   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
122                                grpc_slice_from_static_string("/foo"), nullptr,
123                                deadline, nullptr);
124   GPR_ASSERT(c);
125 
126   grpc_metadata_array_init(&initial_metadata_recv);
127   grpc_metadata_array_init(&trailing_metadata_recv);
128   grpc_metadata_array_init(&request_metadata_recv);
129   grpc_call_details_init(&call_details);
130 
131   memset(ops, 0, sizeof(ops));
132   op = ops;
133   op->op = GRPC_OP_SEND_INITIAL_METADATA;
134   op->data.send_initial_metadata.count = 0;
135   op++;
136   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
137   op++;
138   op->op = GRPC_OP_RECV_INITIAL_METADATA;
139   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
140   op++;
141   if (!recv_message_separately) {
142     op->op = GRPC_OP_RECV_MESSAGE;
143     op->data.recv_message.recv_message = &response_payload1_recv;
144     op++;
145   }
146   if (request_status_early) {
147     op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
148     op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
149     op->data.recv_status_on_client.status = &status;
150     op->data.recv_status_on_client.status_details = &details;
151     op++;
152   }
153   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
154                                 nullptr);
155   GPR_ASSERT(GRPC_CALL_OK == error);
156 
157   GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
158                                  f.server, &s, &call_details,
159                                  &request_metadata_recv, f.cq, f.cq, tag(101)));
160   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
161   cq_verify(cqv);
162 
163   memset(ops, 0, sizeof(ops));
164   op = ops;
165   op->op = GRPC_OP_SEND_INITIAL_METADATA;
166   op->data.send_initial_metadata.count = 0;
167   op++;
168   op->op = GRPC_OP_SEND_MESSAGE;
169   op->data.send_message.send_message = response_payload1;
170   op++;
171   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
172                                 nullptr);
173   GPR_ASSERT(GRPC_CALL_OK == error);
174 
175   if (recv_message_separately) {
176     memset(ops, 0, sizeof(ops));
177     op = ops;
178     op->op = GRPC_OP_RECV_MESSAGE;
179     op->data.recv_message.recv_message = &response_payload1_recv;
180     op++;
181     error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(4),
182                                   nullptr);
183     GPR_ASSERT(GRPC_CALL_OK == error);
184   }
185 
186   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
187   if (!request_status_early) {
188     CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
189   }
190   if (recv_message_separately) {
191     CQ_EXPECT_COMPLETION(cqv, tag(4), 1);
192   }
193   cq_verify(cqv);
194 
195   memset(ops, 0, sizeof(ops));
196   op = ops;
197   op->op = GRPC_OP_SEND_MESSAGE;
198   op->data.send_message.send_message = response_payload2;
199   op++;
200   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
201                                 nullptr);
202   GPR_ASSERT(GRPC_CALL_OK == error);
203   // The success of the op depends on whether the payload is written before the
204   // transport sees the end of stream. If the stream has been write closed
205   // before the write completes, it would fail, otherwise it would succeed.
206   // Since this behavior is dependent on the transport implementation, we allow
207   // any success status with this op.
208   CQ_EXPECT_COMPLETION_ANY_STATUS(cqv, tag(103));
209 
210   if (!request_status_early) {
211     memset(ops, 0, sizeof(ops));
212     op = ops;
213     op->op = GRPC_OP_RECV_MESSAGE;
214     op->data.recv_message.recv_message = &response_payload2_recv;
215     op++;
216     error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
217                                   nullptr);
218     GPR_ASSERT(GRPC_CALL_OK == error);
219 
220     CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
221     cq_verify(cqv);
222   }
223 
224   memset(ops, 0, sizeof(ops));
225   op = ops;
226   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
227   op->data.recv_close_on_server.cancelled = &was_cancelled;
228   op++;
229   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
230   op->data.send_status_from_server.trailing_metadata_count = 0;
231   op->data.send_status_from_server.status = GRPC_STATUS_FAILED_PRECONDITION;
232   grpc_slice status_details = grpc_slice_from_static_string("xyz");
233   op->data.send_status_from_server.status_details = &status_details;
234   op++;
235   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
236                                 nullptr);
237   GPR_ASSERT(GRPC_CALL_OK == error);
238 
239   CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
240   if (request_status_early) {
241     CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
242   }
243   cq_verify(cqv);
244 
245   if (!request_status_early) {
246     memset(ops, 0, sizeof(ops));
247     op = ops;
248     op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
249     op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
250     op->data.recv_status_on_client.status = &status;
251     op->data.recv_status_on_client.status_details = &details;
252     op++;
253     error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
254                                   nullptr);
255     GPR_ASSERT(GRPC_CALL_OK == error);
256 
257     CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
258     cq_verify(cqv);
259 
260     GPR_ASSERT(response_payload1_recv != nullptr);
261     GPR_ASSERT(response_payload2_recv != nullptr);
262   }
263 
264   GPR_ASSERT(status == GRPC_STATUS_FAILED_PRECONDITION);
265   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
266   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
267   GPR_ASSERT(was_cancelled == 0);
268 
269   grpc_slice_unref(details);
270   grpc_metadata_array_destroy(&initial_metadata_recv);
271   grpc_metadata_array_destroy(&trailing_metadata_recv);
272   grpc_metadata_array_destroy(&request_metadata_recv);
273   grpc_call_details_destroy(&call_details);
274 
275   grpc_call_unref(c);
276   grpc_call_unref(s);
277 
278   cq_verifier_destroy(cqv);
279 
280   grpc_byte_buffer_destroy(response_payload1);
281   grpc_byte_buffer_destroy(response_payload2);
282   grpc_byte_buffer_destroy(response_payload1_recv);
283   grpc_byte_buffer_destroy(response_payload2_recv);
284 
285   end_test(&f);
286   config.tear_down_data(&f);
287 }
288 
streaming_error_response(grpc_end2end_test_config config)289 void streaming_error_response(grpc_end2end_test_config config) {
290   test(config, false, false);
291   test(config, true, false);
292   test(config, true, true);
293 }
294 
streaming_error_response_pre_init(void)295 void streaming_error_response_pre_init(void) {}
296