blob: af5778910afa812c54e87dfda15c10f5a8e34aa5 [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) {
Craig Tiller64be9f72015-05-04 14:53:51 -070070 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071 do {
72 ev = grpc_completion_queue_next(cq, five_seconds_time());
Craig Tiller64be9f72015-05-04 14:53:51 -070073 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074}
75
76static void shutdown_server(grpc_end2end_test_fixture *f) {
77 if (!f->server) return;
78 grpc_server_shutdown(f->server);
79 grpc_server_destroy(f->server);
80 f->server = NULL;
81}
82
83static void shutdown_client(grpc_end2end_test_fixture *f) {
84 if (!f->client) return;
85 grpc_channel_destroy(f->client);
86 f->client = NULL;
87}
88
89static void end_test(grpc_end2end_test_fixture *f) {
90 shutdown_server(f);
91 shutdown_client(f);
92
93 grpc_completion_queue_shutdown(f->server_cq);
94 drain_cq(f->server_cq);
95 grpc_completion_queue_destroy(f->server_cq);
96 grpc_completion_queue_shutdown(f->client_cq);
97 drain_cq(f->client_cq);
98 grpc_completion_queue_destroy(f->client_cq);
99}
100
101/* Client sends a request with payload, server reads then returns status. */
102static void test_invoke_request_with_payload(grpc_end2end_test_config config) {
103 grpc_call *c;
104 grpc_call *s;
Craig Tillercff2d152015-02-05 13:22:49 -0800105 gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world");
106 grpc_byte_buffer *request_payload =
107 grpc_byte_buffer_create(&request_payload_slice, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800108 gpr_timespec deadline = five_seconds_time();
109 grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL);
110 cq_verifier *v_client = cq_verifier_create(f.client_cq);
111 cq_verifier *v_server = cq_verifier_create(f.server_cq);
Craig Tillercff2d152015-02-05 13:22:49 -0800112 grpc_op ops[6];
113 grpc_op *op;
114 grpc_metadata_array initial_metadata_recv;
115 grpc_metadata_array trailing_metadata_recv;
116 grpc_metadata_array request_metadata_recv;
117 grpc_byte_buffer *request_payload_recv = NULL;
118 grpc_call_details call_details;
119 grpc_status_code status;
120 char *details = NULL;
121 size_t details_capacity = 0;
122 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800123
Julien Boeuf54b21922015-02-04 16:39:35 -0800124 c = grpc_channel_create_call(f.client, f.client_cq, "/foo",
Julien Boeuf597a4f22015-02-23 15:57:14 -0800125 "foo.test.google.fr", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126 GPR_ASSERT(c);
127
Craig Tillercff2d152015-02-05 13:22:49 -0800128 grpc_metadata_array_init(&initial_metadata_recv);
129 grpc_metadata_array_init(&trailing_metadata_recv);
130 grpc_metadata_array_init(&request_metadata_recv);
131 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132
Craig Tillercff2d152015-02-05 13:22:49 -0800133 op = ops;
134 op->op = GRPC_OP_SEND_INITIAL_METADATA;
135 op->data.send_initial_metadata.count = 0;
136 op++;
137 op->op = GRPC_OP_SEND_MESSAGE;
138 op->data.send_message = request_payload;
139 op++;
140 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
141 op++;
142 op->op = GRPC_OP_RECV_INITIAL_METADATA;
143 op->data.recv_initial_metadata = &initial_metadata_recv;
144 op++;
145 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
146 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
147 op->data.recv_status_on_client.status = &status;
148 op->data.recv_status_on_client.status_details = &details;
149 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
150 op++;
151 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152
Craig Tillercff2d152015-02-05 13:22:49 -0800153 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, &s,
154 &call_details,
155 &request_metadata_recv,
Craig Tiller8ad8a412015-02-25 08:36:40 -0800156 f.server_cq, tag(101)));
Craig Tiller64be9f72015-05-04 14:53:51 -0700157 cq_expect_completion(v_server, tag(101), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158 cq_verify(v_server);
159
Craig Tillercff2d152015-02-05 13:22:49 -0800160 op = ops;
161 op->op = GRPC_OP_SEND_INITIAL_METADATA;
162 op->data.send_initial_metadata.count = 0;
163 op++;
164 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
165 op->data.send_status_from_server.trailing_metadata_count = 0;
166 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
167 op->data.send_status_from_server.status_details = "xyz";
168 op++;
169 op->op = GRPC_OP_RECV_MESSAGE;
170 op->data.recv_message = &request_payload_recv;
171 op++;
172 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
173 op->data.recv_close_on_server.cancelled = &was_cancelled;
174 op++;
175 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176
Craig Tiller64be9f72015-05-04 14:53:51 -0700177 cq_expect_completion(v_server, tag(102), 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800178 cq_verify(v_server);
179
Craig Tiller64be9f72015-05-04 14:53:51 -0700180 cq_expect_completion(v_client, tag(1), 1);
Craig Tillercff2d152015-02-05 13:22:49 -0800181 cq_verify(v_client);
182
183 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
184 GPR_ASSERT(0 == strcmp(details, "xyz"));
185 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
Julien Boeuf597a4f22015-02-23 15:57:14 -0800186 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr"));
Craig Tillere801eb32015-02-10 14:01:14 -0800187 GPR_ASSERT(was_cancelled == 0);
Craig Tillercff2d152015-02-05 13:22:49 -0800188 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
189
190 gpr_free(details);
191 grpc_metadata_array_destroy(&initial_metadata_recv);
192 grpc_metadata_array_destroy(&trailing_metadata_recv);
193 grpc_metadata_array_destroy(&request_metadata_recv);
194 grpc_call_details_destroy(&call_details);
195
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196 grpc_call_destroy(c);
197 grpc_call_destroy(s);
198
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199 cq_verifier_destroy(v_client);
200 cq_verifier_destroy(v_server);
Craig Tillercff2d152015-02-05 13:22:49 -0800201
202 grpc_byte_buffer_destroy(request_payload);
203 grpc_byte_buffer_destroy(request_payload_recv);
204
205 end_test(&f);
206 config.tear_down_data(&f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800207}
208
209void grpc_end2end_tests(grpc_end2end_test_config config) {
210 test_invoke_request_with_payload(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800211}