blob: 4d050833d825dcc136285f8d1732ccb15f8c3a4f [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "test/core/end2end/end2end_tests.h"
35
36#include <stdio.h>
37#include <string.h>
38#include <unistd.h>
39
40#include <grpc/byte_buffer.h>
41#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
43#include <grpc/support/time.h>
44#include <grpc/support/useful.h>
45#include "test/core/end2end/cq_verifier.h"
46
47enum { TIMEOUT = 200000 };
48
49static void *tag(gpr_intptr t) { return (void *)t; }
50
51static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
52 const char *test_name,
53 grpc_channel_args *client_args,
54 grpc_channel_args *server_args) {
55 grpc_end2end_test_fixture f;
56 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
57 f = config.create_fixture(client_args, server_args);
58 config.init_client(&f, client_args);
59 config.init_server(&f, server_args);
60 return f;
61}
62
63static gpr_timespec n_seconds_time(int n) {
Craig Tiller8ad8a412015-02-25 08:36:40 -080064 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080065}
66
Craig Tiller32946d32015-01-15 11:37:30 -080067static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068
69static void drain_cq(grpc_completion_queue *cq) {
70 grpc_event *ev;
71 grpc_completion_type type;
72 do {
73 ev = grpc_completion_queue_next(cq, five_seconds_time());
74 GPR_ASSERT(ev);
75 type = ev->type;
76 grpc_event_finish(ev);
77 } while (type != GRPC_QUEUE_SHUTDOWN);
78}
79
80static void shutdown_server(grpc_end2end_test_fixture *f) {
81 if (!f->server) return;
82 grpc_server_shutdown(f->server);
83 grpc_server_destroy(f->server);
84 f->server = NULL;
85}
86
87static void shutdown_client(grpc_end2end_test_fixture *f) {
88 if (!f->client) return;
89 grpc_channel_destroy(f->client);
90 f->client = NULL;
91}
92
93static void end_test(grpc_end2end_test_fixture *f) {
94 shutdown_server(f);
95 shutdown_client(f);
96
97 grpc_completion_queue_shutdown(f->server_cq);
98 drain_cq(f->server_cq);
99 grpc_completion_queue_destroy(f->server_cq);
100 grpc_completion_queue_shutdown(f->client_cq);
101 drain_cq(f->client_cq);
102 grpc_completion_queue_destroy(f->client_cq);
103}
104
105static void request_response_with_payload(grpc_end2end_test_fixture f) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world");
107 gpr_slice response_payload_slice = gpr_slice_from_copied_string("hello you");
Craig Tiller24fc2c42015-02-04 13:01:16 -0800108 grpc_call *c;
109 grpc_call *s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 grpc_byte_buffer *request_payload =
111 grpc_byte_buffer_create(&request_payload_slice, 1);
112 grpc_byte_buffer *response_payload =
113 grpc_byte_buffer_create(&response_payload_slice, 1);
114 gpr_timespec deadline = five_seconds_time();
115 cq_verifier *v_client = cq_verifier_create(f.client_cq);
116 cq_verifier *v_server = cq_verifier_create(f.server_cq);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800117 grpc_op ops[6];
118 grpc_op *op;
119 grpc_metadata_array initial_metadata_recv;
120 grpc_metadata_array trailing_metadata_recv;
121 grpc_metadata_array request_metadata_recv;
122 grpc_byte_buffer *request_payload_recv = NULL;
123 grpc_byte_buffer *response_payload_recv = NULL;
124 grpc_call_details call_details;
125 grpc_status_code status;
126 char *details = NULL;
Craig Tiller4f972732015-02-05 12:40:20 -0800127 size_t details_capacity = 0;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800128 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800129
Julien Boeuf54b21922015-02-04 16:39:35 -0800130 c = grpc_channel_create_call(f.client, f.client_cq, "/foo",
Julien Boeuf597a4f22015-02-23 15:57:14 -0800131 "foo.test.google.fr", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132 GPR_ASSERT(c);
133
Craig Tiller24fc2c42015-02-04 13:01:16 -0800134 grpc_metadata_array_init(&initial_metadata_recv);
135 grpc_metadata_array_init(&trailing_metadata_recv);
136 grpc_metadata_array_init(&request_metadata_recv);
137 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138
Craig Tiller24fc2c42015-02-04 13:01:16 -0800139 op = ops;
140 op->op = GRPC_OP_SEND_INITIAL_METADATA;
141 op->data.send_initial_metadata.count = 0;
142 op++;
143 op->op = GRPC_OP_SEND_MESSAGE;
144 op->data.send_message = request_payload;
145 op++;
146 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
147 op++;
148 op->op = GRPC_OP_RECV_INITIAL_METADATA;
149 op->data.recv_initial_metadata = &initial_metadata_recv;
150 op++;
151 op->op = GRPC_OP_RECV_MESSAGE;
152 op->data.recv_message = &response_payload_recv;
153 op++;
154 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
155 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
156 op->data.recv_status_on_client.status = &status;
157 op->data.recv_status_on_client.status_details = &details;
158 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
159 op++;
160 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
161
162 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s,
163 &call_details,
164 &request_metadata_recv,
Craig Tiller8ad8a412015-02-25 08:36:40 -0800165 f.server_cq, tag(101)));
Craig Tiller24fc2c42015-02-04 13:01:16 -0800166 cq_expect_completion(v_server, tag(101), GRPC_OP_OK);
167 cq_verify(v_server);
168
169 op = ops;
170 op->op = GRPC_OP_SEND_INITIAL_METADATA;
171 op->data.send_initial_metadata.count = 0;
172 op++;
173 op->op = GRPC_OP_SEND_MESSAGE;
174 op->data.send_message = response_payload;
175 op++;
176 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
177 op->data.send_status_from_server.trailing_metadata_count = 0;
Craig Tillerd1abc812015-05-06 14:35:19 -0700178 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tiller24fc2c42015-02-04 13:01:16 -0800179 op->data.send_status_from_server.status_details = "xyz";
180 op++;
181 op->op = GRPC_OP_RECV_MESSAGE;
182 op->data.recv_message = &request_payload_recv;
183 op++;
184 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
185 op->data.recv_close_on_server.cancelled = &was_cancelled;
186 op++;
187 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
188
189 cq_expect_completion(v_server, tag(102), GRPC_OP_OK);
190 cq_verify(v_server);
191
192 cq_expect_completion(v_client, tag(1), GRPC_OP_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800193 cq_verify(v_client);
194
Craig Tillerd1abc812015-05-06 14:35:19 -0700195 GPR_ASSERT(status == GRPC_STATUS_OK);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800196 GPR_ASSERT(0 == strcmp(details, "xyz"));
197 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
Julien Boeuf597a4f22015-02-23 15:57:14 -0800198 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr"));
Craig Tillere801eb32015-02-10 14:01:14 -0800199 GPR_ASSERT(was_cancelled == 0);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800200 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
201 GPR_ASSERT(byte_buffer_eq_string(response_payload_recv, "hello you"));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800202
Craig Tiller4f972732015-02-05 12:40:20 -0800203 gpr_free(details);
204 grpc_metadata_array_destroy(&initial_metadata_recv);
205 grpc_metadata_array_destroy(&trailing_metadata_recv);
206 grpc_metadata_array_destroy(&request_metadata_recv);
207 grpc_call_details_destroy(&call_details);
208
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 grpc_call_destroy(c);
210 grpc_call_destroy(s);
211
212 cq_verifier_destroy(v_client);
213 cq_verifier_destroy(v_server);
Craig Tiller24fc2c42015-02-04 13:01:16 -0800214
215 grpc_byte_buffer_destroy(request_payload);
216 grpc_byte_buffer_destroy(response_payload);
217 grpc_byte_buffer_destroy(request_payload_recv);
218 grpc_byte_buffer_destroy(response_payload_recv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219}
220
221/* Client sends a request with payload, server reads then returns a response
222 payload and status. */
223static void test_invoke_request_response_with_payload(
224 grpc_end2end_test_config config) {
225 grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL);
226 request_response_with_payload(f);
227 end_test(&f);
228 config.tear_down_data(&f);
229}
230
231static void test_invoke_10_request_response_with_payload(
232 grpc_end2end_test_config config) {
233 int i;
234 grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL);
235 for (i = 0; i < 10; i++) {
236 request_response_with_payload(f);
237 }
238 end_test(&f);
239 config.tear_down_data(&f);
240}
241
242void grpc_end2end_tests(grpc_end2end_test_config config) {
243 test_invoke_request_response_with_payload(config);
244 test_invoke_10_request_response_with_payload(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800245}