Debian Codegraph @

test/core/end2end/tests/client_streaming.cc

raw
1 //
2 //
3 // Copyright 2020 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 <stdint.h>
20 #include <string.h>
21
22 #include <grpc/byte_buffer.h>
23 #include <grpc/grpc.h>
24 #include <grpc/impl/codegen/propagation_bits.h>
25 #include <grpc/slice.h>
26 #include <grpc/status.h>
27 #include <grpc/support/log.h>
28
29 #include "test/core/end2end/cq_verifier.h"
30 #include "test/core/end2end/end2end_tests.h"
31 #include "test/core/util/test_config.h"
32
33 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
34
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 grpc_end2end_test_fixture f;
40 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
41 f = config.create_fixture(client_args, server_args);
42 config.init_server(&f, server_args);
43 config.init_client(&f, client_args);
44 return f;
45 }
46
47 static gpr_timespec n_seconds_from_now(int n) {
48 return grpc_timeout_seconds_to_deadline(n);
49 }
50
51 static gpr_timespec five_seconds_from_now(void) {
52 return n_seconds_from_now(5);
53 }
54
55 static void drain_cq(grpc_completion_queue* cq) {
56 grpc_event ev;
57 do {
58 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
59 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
60 }
61
62 static void shutdown_server(grpc_end2end_test_fixture* f) {
63 if (!f->server) return;
64 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
65 grpc_event ev;
66 do {
67 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
68 nullptr);
69 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
70 grpc_server_destroy(f->server);
71 f->server = nullptr;
72 }
73
74 static void shutdown_client(grpc_end2end_test_fixture* f) {
75 if (!f->client) return;
76 grpc_channel_destroy(f->client);
77 f->client = nullptr;
78 }
79
80 static void end_test(grpc_end2end_test_fixture* f) {
81 shutdown_server(f);
82 shutdown_client(f);
83
84 grpc_completion_queue_shutdown(f->cq);
85 drain_cq(f->cq);
86 grpc_completion_queue_destroy(f->cq);
87 }
88
89 // Client streaming test where the client sends a bunch of messages and the
90 // server reads them. After reading some messages, the server sends the status.
91 // Client writes fail after that due to the end of stream and the client
92 // subsequently requests and receives the status.
93 static void test_client_streaming(grpc_end2end_test_config config,
94 int messages) {
95 grpc_end2end_test_fixture f =
96 begin_test(config, "test_client_streaming", nullptr, nullptr);
97 grpc_call* c;
98 grpc_call* s;
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_metadata_array request_metadata_recv;
105 grpc_call_details call_details;
106 grpc_status_code status;
107 grpc_call_error error;
108 grpc_slice details;
109 grpc_byte_buffer* request_payload_recv = nullptr;
110 grpc_byte_buffer* request_payload = nullptr;
111 int i;
112 grpc_slice request_payload_slice =
113 grpc_slice_from_copied_string("hello world");
114
115 gpr_timespec deadline = five_seconds_from_now();
116 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
117 grpc_slice_from_static_string("/foo"), nullptr,
118 deadline, nullptr);
119 GPR_ASSERT(c);
120
121 grpc_metadata_array_init(&initial_metadata_recv);
122 grpc_metadata_array_init(&trailing_metadata_recv);
123 grpc_metadata_array_init(&request_metadata_recv);
124 grpc_call_details_init(&call_details);
125
126 memset(ops, 0, sizeof(ops));
127 op = ops;
128 op->op = GRPC_OP_SEND_INITIAL_METADATA;
129 op->data.send_initial_metadata.count = 0;
130 op->flags = 0;
131 op->reserved = nullptr;
132 op++;
133 op->op = GRPC_OP_RECV_INITIAL_METADATA;
134 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
135 op->flags = 0;
136 op->reserved = nullptr;
137 op++;
138 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
139 nullptr);
140 GPR_ASSERT(GRPC_CALL_OK == error);
141
142 error =
143 grpc_server_request_call(f.server, &s, &call_details,
144 &request_metadata_recv, f.cq, f.cq, tag(100));
145 GPR_ASSERT(GRPC_CALL_OK == error);
146 cqv.Expect(tag(100), true);
147 cqv.Verify();
148
149 memset(ops, 0, sizeof(ops));
150 op = ops;
151 op->op = GRPC_OP_SEND_INITIAL_METADATA;
152 op->data.send_initial_metadata.count = 0;
153 op->flags = 0;
154 op->reserved = nullptr;
155 op++;
156 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(101),
157 nullptr);
158 GPR_ASSERT(GRPC_CALL_OK == error);
159
160 cqv.Expect(tag(101), true);
161 cqv.Expect(tag(1), true);
162 cqv.Verify();
163
164 // Client writes bunch of messages and server reads them
165 for (i = 0; i < messages; i++) {
166 request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
167 memset(ops, 0, sizeof(ops));
168 op = ops;
169 op->op = GRPC_OP_SEND_MESSAGE;
170 op->data.send_message.send_message = request_payload;
171 op->flags = 0;
172 op->reserved = nullptr;
173 op++;
174 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops),
175 tag(103), nullptr);
176 GPR_ASSERT(GRPC_CALL_OK == error);
177 grpc_byte_buffer_destroy(request_payload);
178
179 memset(ops, 0, sizeof(ops));
180 op = ops;
181 op->op = GRPC_OP_RECV_MESSAGE;
182 op->data.recv_message.recv_message = &request_payload_recv;
183 op->flags = 0;
184 op->reserved = nullptr;
185 op++;
186 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
187 tag(102), nullptr);
188 GPR_ASSERT(GRPC_CALL_OK == error);
189 cqv.Expect(tag(102), true);
190 cqv.Expect(tag(103), true);
191 cqv.Verify();
192 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
193 grpc_byte_buffer_destroy(request_payload_recv);
194 }
195
196 // Server sends status denoting end of stream
197 memset(ops, 0, sizeof(ops));
198 op = ops;
199 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
200 op->data.send_status_from_server.trailing_metadata_count = 0;
201 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
202 grpc_slice status_details = grpc_slice_from_static_string("xyz");
203 op->data.send_status_from_server.status_details = &status_details;
204 op->flags = 0;
205 op->reserved = nullptr;
206 op++;
207 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
208 nullptr);
209 GPR_ASSERT(GRPC_CALL_OK == error);
210 cqv.Expect(tag(104), true);
211 cqv.Verify();
212 // Do an empty verify to make sure that the client receives the status
213 cqv.VerifyEmpty();
214
215 // Client tries sending another message which should fail
216 request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
217 memset(ops, 0, sizeof(ops));
218 op = ops;
219 op->op = GRPC_OP_SEND_MESSAGE;
220 op->data.send_message.send_message = request_payload;
221 op->flags = 0;
222 op->reserved = nullptr;
223 op++;
224 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(103),
225 nullptr);
226 GPR_ASSERT(GRPC_CALL_OK == error);
227 grpc_byte_buffer_destroy(request_payload);
228 cqv.Expect(tag(103), false);
229 cqv.Verify();
230
231 // Client sends close and requests status
232 memset(ops, 0, sizeof(ops));
233 op = ops;
234 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
235 op->flags = 0;
236 op->reserved = nullptr;
237 op++;
238 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
239 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
240 op->data.recv_status_on_client.status = &status;
241 op->data.recv_status_on_client.status_details = &details;
242 op->flags = 0;
243 op->reserved = nullptr;
244 op++;
245 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
246 nullptr);
247 GPR_ASSERT(GRPC_CALL_OK == error);
248 cqv.Expect(tag(3), true);
249 cqv.Verify();
250 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
251 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
252
253 grpc_slice_unref(request_payload_slice);
254
255 grpc_call_unref(c);
256 grpc_call_unref(s);
257
258 grpc_metadata_array_destroy(&initial_metadata_recv);
259 grpc_metadata_array_destroy(&trailing_metadata_recv);
260 grpc_metadata_array_destroy(&request_metadata_recv);
261 grpc_call_details_destroy(&call_details);
262 grpc_slice_unref(details);
263
264 end_test(&f);
265 config.tear_down_data(&f);
266 }
267
268 void client_streaming(grpc_end2end_test_config config) {
269 for (int i = 0; i < 10; i++) {
270 test_client_streaming(config, i);
271 }
272 }
273
274 void client_streaming_pre_init(void) {}