blob: 6194b841d8af6ade88216c6de7c669abf7ab0913 [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>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
Craig Tiller7da8ef12015-01-23 15:07:22 -080039#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/byte_buffer.h>
Craig Tillerea61b072015-02-03 19:19:27 -080041#include <grpc/grpc.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include <grpc/support/alloc.h>
43#include <grpc/support/log.h>
44#include <grpc/support/time.h>
45#include <grpc/support/useful.h>
46#include "test/core/end2end/cq_verifier.h"
47
48enum { TIMEOUT = 200000 };
49
50static void *tag(gpr_intptr t) { return (void *)t; }
51
52static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
53 const char *test_name,
54 grpc_channel_args *client_args,
55 grpc_channel_args *server_args) {
56 grpc_end2end_test_fixture f;
57 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
58 f = config.create_fixture(client_args, server_args);
59 config.init_client(&f, client_args);
60 config.init_server(&f, server_args);
61 return f;
62}
63
64static gpr_timespec n_seconds_time(int n) {
Craig Tiller8ad8a412015-02-25 08:36:40 -080065 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066}
67
Craig Tiller32946d32015-01-15 11:37:30 -080068static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069
70static void drain_cq(grpc_completion_queue *cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070071 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072 do {
73 ev = grpc_completion_queue_next(cq, five_seconds_time());
Craig Tiller64be9f72015-05-04 14:53:51 -070074 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075}
76
77static void shutdown_server(grpc_end2end_test_fixture *f) {
78 if (!f->server) return;
Craig Tillerba2e7552015-05-28 12:53:54 -070079 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
Craig Tiller9a576332015-06-17 10:21:49 -070080 GPR_ASSERT(grpc_completion_queue_pluck(f->cq, tag(1000),
81 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5))
82 .type == GRPC_OP_COMPLETE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 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
Craig Tillerbc0ec332015-05-11 12:11:32 -070097 grpc_completion_queue_shutdown(f->cq);
98 drain_cq(f->cq);
99 grpc_completion_queue_destroy(f->cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100}
101
102static void simple_request_body(grpc_end2end_test_fixture f) {
103 grpc_call *c;
104 grpc_call *s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105 gpr_timespec deadline = five_seconds_time();
Craig Tillerbc0ec332015-05-11 12:11:32 -0700106 cq_verifier *cqv = cq_verifier_create(f.cq);
Craig Tillerea61b072015-02-03 19:19:27 -0800107 grpc_op ops[6];
Craig Tiller5a34d452015-02-03 14:29:22 -0800108 grpc_op *op;
Craig Tillerea61b072015-02-03 19:19:27 -0800109 grpc_metadata_array initial_metadata_recv;
Craig Tiller5a34d452015-02-03 14:29:22 -0800110 grpc_metadata_array trailing_metadata_recv;
111 grpc_metadata_array request_metadata_recv;
112 grpc_call_details call_details;
113 grpc_status_code status;
114 char *details = NULL;
Craig Tiller4f972732015-02-05 12:40:20 -0800115 size_t details_capacity = 0;
Craig Tillerea61b072015-02-03 19:19:27 -0800116 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
Craig Tillerbc0ec332015-05-11 12:11:32 -0700118 c = grpc_channel_create_call(f.client, f.cq, "/foo",
Craig Tillerdeb49dd2015-02-25 11:58:49 -0800119 "foo.test.google.fr:1234", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800120 GPR_ASSERT(c);
121
Craig Tillerea61b072015-02-03 19:19:27 -0800122 grpc_metadata_array_init(&initial_metadata_recv);
Craig Tiller5a34d452015-02-03 14:29:22 -0800123 grpc_metadata_array_init(&trailing_metadata_recv);
124 grpc_metadata_array_init(&request_metadata_recv);
125 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126
Craig Tiller5a34d452015-02-03 14:29:22 -0800127 op = ops;
128 op->op = GRPC_OP_SEND_INITIAL_METADATA;
129 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700130 op->flags = 0;
Craig Tiller5a34d452015-02-03 14:29:22 -0800131 op++;
Craig Tillerea61b072015-02-03 19:19:27 -0800132 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700133 op->flags = 0;
Craig Tillerea61b072015-02-03 19:19:27 -0800134 op++;
135 op->op = GRPC_OP_RECV_INITIAL_METADATA;
136 op->data.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700137 op->flags = 0;
Craig Tillerea61b072015-02-03 19:19:27 -0800138 op++;
Craig Tiller5a34d452015-02-03 14:29:22 -0800139 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Craig Tillerea61b072015-02-03 19:19:27 -0800140 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
Craig Tiller5a34d452015-02-03 14:29:22 -0800141 op->data.recv_status_on_client.status = &status;
142 op->data.recv_status_on_client.status_details = &details;
143 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700144 op->flags = 0;
Craig Tiller5a34d452015-02-03 14:29:22 -0800145 op++;
Craig Tillerea61b072015-02-03 19:19:27 -0800146 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
147
Craig Tiller9a576332015-06-17 10:21:49 -0700148 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
149 f.server, &s, &call_details,
150 &request_metadata_recv, f.cq, f.cq, tag(101)));
Craig Tillera0a6b122015-05-12 14:42:27 -0700151 cq_expect_completion(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700152 cq_verify(cqv);
Craig Tillerea61b072015-02-03 19:19:27 -0800153
154 op = ops;
155 op->op = GRPC_OP_SEND_INITIAL_METADATA;
156 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700157 op->flags = 0;
Craig Tiller5a34d452015-02-03 14:29:22 -0800158 op++;
Craig Tillerea61b072015-02-03 19:19:27 -0800159 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
160 op->data.send_status_from_server.trailing_metadata_count = 0;
161 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
162 op->data.send_status_from_server.status_details = "xyz";
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700163 op->flags = 0;
Craig Tillerea61b072015-02-03 19:19:27 -0800164 op++;
165 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
166 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700167 op->flags = 0;
Craig Tillerea61b072015-02-03 19:19:27 -0800168 op++;
169 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170
Craig Tillera0a6b122015-05-12 14:42:27 -0700171 cq_expect_completion(cqv, tag(102), 1);
172 cq_expect_completion(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700173 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174
Craig Tillerea61b072015-02-03 19:19:27 -0800175 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
176 GPR_ASSERT(0 == strcmp(details, "xyz"));
177 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
Craig Tillerdeb49dd2015-02-25 11:58:49 -0800178 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr:1234"));
Craig Tilleraea081f2015-06-11 14:19:33 -0700179 GPR_ASSERT(was_cancelled == 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800180
Craig Tiller4f972732015-02-05 12:40:20 -0800181 gpr_free(details);
182 grpc_metadata_array_destroy(&initial_metadata_recv);
183 grpc_metadata_array_destroy(&trailing_metadata_recv);
184 grpc_metadata_array_destroy(&request_metadata_recv);
185 grpc_call_details_destroy(&call_details);
186
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 grpc_call_destroy(c);
188 grpc_call_destroy(s);
189
Craig Tillerbc0ec332015-05-11 12:11:32 -0700190 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800191}
192
Craig Tillerea61b072015-02-03 19:19:27 -0800193static void test_invoke_simple_request(grpc_end2end_test_config config) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 grpc_end2end_test_fixture f;
195
Craig Tiller35696192015-05-24 15:00:37 -0700196 f = begin_test(config, "test_invoke_simple_request", NULL, NULL);
Craig Tillerea61b072015-02-03 19:19:27 -0800197 simple_request_body(f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 end_test(&f);
199 config.tear_down_data(&f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200}
201
202static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
203 int i;
Craig Tiller9a576332015-06-17 10:21:49 -0700204 grpc_end2end_test_fixture f =
205 begin_test(config, "test_invoke_10_simple_requests", NULL, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 for (i = 0; i < 10; i++) {
207 simple_request_body(f);
208 gpr_log(GPR_INFO, "Passed simple request %d", i);
209 }
210 end_test(&f);
211 config.tear_down_data(&f);
212}
213
214void grpc_end2end_tests(grpc_end2end_test_config config) {
Craig Tillere398fa22015-06-02 12:51:53 -0700215 int i;
216 for (i = 0; i < 10; i++) {
217 test_invoke_simple_request(config);
218 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800219 test_invoke_10_simple_requests(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800220}