blob: 1b9dc9632b73689254f6e6647facfeec36353e23 [file] [log] [blame]
Craig Tiller2ef5a642017-01-05 10:33:47 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2017 gRPC authors.
Craig Tiller2ef5a642017-01-05 10:33:47 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Craig Tiller2ef5a642017-01-05 10:33:47 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller2ef5a642017-01-05 10:33:47 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Craig Tiller2ef5a642017-01-05 10:33:47 -080016 *
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 <grpc/support/useful.h>
29#include "test/core/end2end/cq_verifier.h"
30
Craig Tillerbaa14a92017-11-03 09:09:36 -070031static void* tag(intptr_t t) { return (void*)t; }
Craig Tiller2ef5a642017-01-05 10:33:47 -080032
33static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
Craig Tillerbaa14a92017-11-03 09:09:36 -070034 const char* test_name,
35 grpc_channel_args* client_args,
36 grpc_channel_args* server_args) {
Craig Tiller2ef5a642017-01-05 10:33:47 -080037 grpc_end2end_test_fixture f;
Robbie Shade55a046a2017-01-25 15:14:28 -050038 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Craig Tiller2ef5a642017-01-05 10:33:47 -080039 f = config.create_fixture(client_args, server_args);
40 config.init_server(&f, server_args);
41 config.init_client(&f, client_args);
42 return f;
43}
44
Chris Evansed2a5472017-03-27 17:34:51 -050045static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050046 return grpc_timeout_seconds_to_deadline(n);
Craig Tiller2ef5a642017-01-05 10:33:47 -080047}
48
Chris Evansed2a5472017-03-27 17:34:51 -050049static gpr_timespec five_seconds_from_now(void) {
50 return n_seconds_from_now(5);
51}
Craig Tiller2ef5a642017-01-05 10:33:47 -080052
Craig Tillerbaa14a92017-11-03 09:09:36 -070053static void drain_cq(grpc_completion_queue* cq) {
Craig Tiller2ef5a642017-01-05 10:33:47 -080054 grpc_event ev;
55 do {
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080056 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -080057 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
58}
59
Craig Tillerbaa14a92017-11-03 09:09:36 -070060static void shutdown_server(grpc_end2end_test_fixture* f) {
Craig Tiller2ef5a642017-01-05 10:33:47 -080061 if (!f->server) return;
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080062 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
63 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
64 grpc_timeout_seconds_to_deadline(5),
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080065 nullptr)
Craig Tiller2ef5a642017-01-05 10:33:47 -080066 .type == GRPC_OP_COMPLETE);
67 grpc_server_destroy(f->server);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080068 f->server = nullptr;
Craig Tiller2ef5a642017-01-05 10:33:47 -080069}
70
Craig Tillerbaa14a92017-11-03 09:09:36 -070071static void shutdown_client(grpc_end2end_test_fixture* f) {
Craig Tiller2ef5a642017-01-05 10:33:47 -080072 if (!f->client) return;
73 grpc_channel_destroy(f->client);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080074 f->client = nullptr;
Craig Tiller2ef5a642017-01-05 10:33:47 -080075}
76
Craig Tillerbaa14a92017-11-03 09:09:36 -070077static void end_test(grpc_end2end_test_fixture* f) {
Craig Tiller2ef5a642017-01-05 10:33:47 -080078 shutdown_server(f);
79 shutdown_client(f);
80
81 grpc_completion_queue_shutdown(f->cq);
82 drain_cq(f->cq);
83 grpc_completion_queue_destroy(f->cq);
Sree Kuchibhotla321881d2017-02-27 11:25:28 -080084 grpc_completion_queue_destroy(f->shutdown_cq);
Craig Tiller2ef5a642017-01-05 10:33:47 -080085}
86
87/* Client sends a request with payload, server reads then returns status. */
88static void test_invoke_request_with_payload(grpc_end2end_test_config config) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070089 grpc_call* c;
90 grpc_call* s;
Craig Tiller2ef5a642017-01-05 10:33:47 -080091 grpc_slice request_payload_slice =
92 grpc_slice_from_copied_string("hello world");
Craig Tillerbaa14a92017-11-03 09:09:36 -070093 grpc_byte_buffer* request_payload =
Craig Tiller2ef5a642017-01-05 10:33:47 -080094 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
Craig Tiller2ef5a642017-01-05 10:33:47 -080095 grpc_end2end_test_fixture f =
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080096 begin_test(config, "test_invoke_request_with_payload", nullptr, nullptr);
Craig Tillerbaa14a92017-11-03 09:09:36 -070097 cq_verifier* cqv = cq_verifier_create(f.cq);
Craig Tiller2ef5a642017-01-05 10:33:47 -080098 grpc_op ops[6];
Craig Tillerbaa14a92017-11-03 09:09:36 -070099 grpc_op* op;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800100 grpc_metadata_array initial_metadata_recv;
101 grpc_metadata_array trailing_metadata_recv;
102 grpc_metadata_array request_metadata_recv;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800103 grpc_byte_buffer* request_payload_recv1 = nullptr;
104 grpc_byte_buffer* request_payload_recv2 = nullptr;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800105 grpc_call_details call_details;
106 grpc_status_code status;
107 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800108 grpc_slice details = grpc_empty_slice();
Craig Tiller2ef5a642017-01-05 10:33:47 -0800109 int was_cancelled = 2;
110
Chris Evansed2a5472017-03-27 17:34:51 -0500111 gpr_timespec deadline = five_seconds_from_now();
Craig Tiller2ef5a642017-01-05 10:33:47 -0800112 c = grpc_channel_create_call(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800113 f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800114 grpc_slice_from_static_string("/foo"),
115 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800116 nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800117 GPR_ASSERT(c);
118
119 grpc_metadata_array_init(&initial_metadata_recv);
120 grpc_metadata_array_init(&trailing_metadata_recv);
121 grpc_metadata_array_init(&request_metadata_recv);
122 grpc_call_details_init(&call_details);
123
124 memset(ops, 0, sizeof(ops));
125 op = ops;
126 op->op = GRPC_OP_SEND_INITIAL_METADATA;
127 op->data.send_initial_metadata.count = 0;
128 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800129 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800130 GPR_ASSERT(GRPC_CALL_OK == error);
131
132 memset(ops, 0, sizeof(ops));
133 op = ops;
134 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800135 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800136 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800137 op->reserved = nullptr;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800138 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800139 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800140 GPR_ASSERT(GRPC_CALL_OK == error);
141
142 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
143 f.server, &s, &call_details,
144 &request_metadata_recv, f.cq, f.cq, tag(101)));
145 CQ_EXPECT_COMPLETION(cqv, tag(1), true); /* send message is buffered */
146 CQ_EXPECT_COMPLETION(cqv, tag(101), true);
147 cq_verify(cqv);
148
149 memset(ops, 0, sizeof(ops));
150 op = ops;
151 op->op = GRPC_OP_SEND_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800152 op->data.send_message.send_message = request_payload;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800153 op->flags = GRPC_WRITE_BUFFER_HINT;
154 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800155 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800156 GPR_ASSERT(GRPC_CALL_OK == error);
157
158 memset(ops, 0, sizeof(ops));
159 op = ops;
160 op->op = GRPC_OP_SEND_INITIAL_METADATA;
161 op->data.send_initial_metadata.count = 0;
162 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800163 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800164 GPR_ASSERT(GRPC_CALL_OK == error);
165
166 /* recv message should not succeed yet - it's buffered at the client still */
167 memset(ops, 0, sizeof(ops));
168 op = ops;
169 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800170 op->data.recv_message.recv_message = &request_payload_recv1;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800171 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800172 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800173 GPR_ASSERT(GRPC_CALL_OK == error);
174
175 CQ_EXPECT_COMPLETION(cqv, tag(2), true);
176 CQ_EXPECT_COMPLETION(cqv, tag(3), true);
177 CQ_EXPECT_COMPLETION(cqv, tag(102), true);
178 cq_verify(cqv);
179
180 /* send end of stream: should release the buffering */
181 memset(ops, 0, sizeof(ops));
182 op = ops;
183 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
184 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800185 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800186 GPR_ASSERT(GRPC_CALL_OK == error);
187
188 /* now the first send should match up with the first recv */
189 CQ_EXPECT_COMPLETION(cqv, tag(103), true);
190 CQ_EXPECT_COMPLETION(cqv, tag(4), true);
191 cq_verify(cqv);
192
193 /* and the next recv should be ready immediately also (and empty) */
194 memset(ops, 0, sizeof(ops));
195 op = ops;
196 op->op = GRPC_OP_RECV_MESSAGE;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800197 op->data.recv_message.recv_message = &request_payload_recv2;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800198 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800199 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800200 GPR_ASSERT(GRPC_CALL_OK == error);
201
202 CQ_EXPECT_COMPLETION(cqv, tag(104), true);
203 cq_verify(cqv);
204
205 memset(ops, 0, sizeof(ops));
206 op = ops;
207 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
208 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
209 op->data.recv_status_on_client.status = &status;
210 op->data.recv_status_on_client.status_details = &details;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800211 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800212 op->reserved = nullptr;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800213 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800214 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800215
216 memset(ops, 0, sizeof(ops));
217 op = ops;
218 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
219 op->data.recv_close_on_server.cancelled = &was_cancelled;
220 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800221 op->reserved = nullptr;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800222 op++;
223 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
224 op->data.send_status_from_server.trailing_metadata_count = 0;
225 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800226 grpc_slice status_details = grpc_slice_from_static_string("xyz");
227 op->data.send_status_from_server.status_details = &status_details;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800228 op->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800229 op->reserved = nullptr;
Craig Tiller2ef5a642017-01-05 10:33:47 -0800230 op++;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800231 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(105), nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800232 GPR_ASSERT(GRPC_CALL_OK == error);
233
234 CQ_EXPECT_COMPLETION(cqv, tag(105), 1);
235 CQ_EXPECT_COMPLETION(cqv, tag(4), 1);
236 cq_verify(cqv);
237
238 GPR_ASSERT(status == GRPC_STATUS_OK);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800239 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
240 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
Craig Tiller2ef5a642017-01-05 10:33:47 -0800241 validate_host_override_string("foo.test.google.fr:1234", call_details.host,
242 config);
243 GPR_ASSERT(was_cancelled == 0);
244 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv1, "hello world"));
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800245 GPR_ASSERT(request_payload_recv2 == nullptr);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800246
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800247 grpc_slice_unref(details);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800248 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
Craig Tillerdd36b152017-03-31 08:27:28 -0700253 grpc_call_unref(c);
254 grpc_call_unref(s);
Craig Tiller2ef5a642017-01-05 10:33:47 -0800255
256 cq_verifier_destroy(cqv);
257
258 grpc_byte_buffer_destroy(request_payload);
259 grpc_byte_buffer_destroy(request_payload_recv1);
260
261 end_test(&f);
262 config.tear_down_data(&f);
263}
264
265void write_buffering_at_end(grpc_end2end_test_config config) {
266 test_invoke_request_with_payload(config);
267}
268
269void write_buffering_at_end_pre_init(void) {}