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