blob: 313b0c3324f0306069448094bc5e9c68414b8391 [file] [log] [blame]
Craig Tiller7a290982015-05-19 12:49:54 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller7a290982015-05-19 12:49:54 -07004 * 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
Craig Tiller7a290982015-05-19 12:49:54 -070039#include <grpc/byte_buffer.h>
40#include <grpc/grpc.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>
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/support/string.h"
Craig Tiller7a290982015-05-19 12:49:54 -070046#include "test/core/end2end/cq_verifier.h"
47
Craig Tiller7536af02015-12-22 13:49:30 -080048static void *tag(intptr_t t) { return (void *)t; }
Craig Tiller7a290982015-05-19 12:49:54 -070049
50static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
51 const char *test_name,
52 grpc_channel_args *client_args,
53 grpc_channel_args *server_args) {
54 grpc_end2end_test_fixture f;
55 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
56 f = config.create_fixture(client_args, server_args);
Craig Tiller7a290982015-05-19 12:49:54 -070057 config.init_server(&f, server_args);
Craig Tiller27e5aa42015-11-24 16:28:54 -080058 config.init_client(&f, client_args);
Craig Tiller7a290982015-05-19 12:49:54 -070059 return f;
60}
61
62static gpr_timespec n_seconds_time(int n) {
63 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
64}
65
66static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
67
68static void drain_cq(grpc_completion_queue *cq) {
69 grpc_event ev;
70 do {
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +020071 ev = grpc_completion_queue_next(cq, five_seconds_time(), NULL);
Craig Tiller7a290982015-05-19 12:49:54 -070072 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
73}
74
75static void shutdown_server(grpc_end2end_test_fixture *f) {
76 if (!f->server) return;
Craig Tillerba2e7552015-05-28 12:53:54 -070077 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
Craig Tillerf40df232016-03-25 13:38:14 -070078 GPR_ASSERT(grpc_completion_queue_pluck(
79 f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL)
80 .type == GRPC_OP_COMPLETE);
Craig Tiller7a290982015-05-19 12:49:54 -070081 grpc_server_destroy(f->server);
82 f->server = NULL;
83}
84
85static void shutdown_client(grpc_end2end_test_fixture *f) {
86 if (!f->client) return;
87 grpc_channel_destroy(f->client);
88 f->client = NULL;
89}
90
91static void end_test(grpc_end2end_test_fixture *f) {
92 shutdown_server(f);
93 shutdown_client(f);
94
Craig Tillerdad88a72015-05-26 17:01:09 -070095 grpc_completion_queue_shutdown(f->cq);
96 drain_cq(f->cq);
97 grpc_completion_queue_destroy(f->cq);
Craig Tiller7a290982015-05-19 12:49:54 -070098}
99
Muxi Yan56456c32016-10-30 23:11:11 -0700100static void simple_request_body(grpc_end2end_test_config config, grpc_end2end_test_fixture f) {
Craig Tiller7a290982015-05-19 12:49:54 -0700101 grpc_call *c;
102 grpc_call *s;
103 gpr_timespec deadline = five_seconds_time();
Craig Tillerdad88a72015-05-26 17:01:09 -0700104 cq_verifier *cqv = cq_verifier_create(f.cq);
Craig Tiller7a290982015-05-19 12:49:54 -0700105 grpc_op ops[6];
106 grpc_op *op;
107 grpc_metadata_array initial_metadata_recv;
108 grpc_metadata_array trailing_metadata_recv;
109 grpc_metadata_array request_metadata_recv;
110 grpc_call_details call_details;
111 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200112 grpc_call_error error;
Craig Tiller7a290982015-05-19 12:49:54 -0700113 char *details = NULL;
114 size_t details_capacity = 0;
115 int was_cancelled = 2;
116
Craig Tillere1b0e6e2015-07-31 17:07:31 -0700117 c = grpc_channel_create_call(f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq,
Muxi Yan56456c32016-10-30 23:11:11 -0700118 "/foo", get_host_override_string("foo.test.google.fr:1234", config), deadline, NULL);
Craig Tiller7a290982015-05-19 12:49:54 -0700119 GPR_ASSERT(c);
120
121 grpc_metadata_array_init(&initial_metadata_recv);
122 grpc_metadata_array_init(&trailing_metadata_recv);
123 grpc_metadata_array_init(&request_metadata_recv);
124 grpc_call_details_init(&call_details);
125
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700126 memset(ops, 0, sizeof(ops));
Craig Tiller7a290982015-05-19 12:49:54 -0700127 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;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200131 op->reserved = NULL;
Craig Tiller7a290982015-05-19 12:49:54 -0700132 op++;
133 op->op = GRPC_OP_RECV_INITIAL_METADATA;
134 op->data.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700135 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200136 op->reserved = NULL;
Craig Tiller7a290982015-05-19 12:49:54 -0700137 op++;
138 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
139 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
140 op->data.recv_status_on_client.status = &status;
141 op->data.recv_status_on_client.status_details = &details;
142 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700143 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200144 op->reserved = NULL;
Craig Tiller7a290982015-05-19 12:49:54 -0700145 op++;
Craig Tiller3121fd42015-09-10 09:56:20 -0700146 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200147 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller7a290982015-05-19 12:49:54 -0700148
Craig Tillerd6c98df2015-08-18 09:33:44 -0700149 error =
150 grpc_server_request_call(f.server, &s, &call_details,
151 &request_metadata_recv, f.cq, f.cq, tag(101));
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200152 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700153 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillerdad88a72015-05-26 17:01:09 -0700154 cq_verify(cqv);
Craig Tiller7a290982015-05-19 12:49:54 -0700155
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700156 memset(ops, 0, sizeof(ops));
Craig Tiller7a290982015-05-19 12:49:54 -0700157 op = ops;
158 op->op = GRPC_OP_SEND_INITIAL_METADATA;
159 op->data.send_initial_metadata.count = 0;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700160 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200161 op->reserved = NULL;
Craig Tiller7a290982015-05-19 12:49:54 -0700162 op++;
163 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
164 op->data.send_status_from_server.trailing_metadata_count = 0;
165 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
166 op->data.send_status_from_server.status_details = "xyz";
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700167 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200168 op->reserved = NULL;
Craig Tiller7a290982015-05-19 12:49:54 -0700169 op++;
170 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
171 op->data.recv_close_on_server.cancelled = &was_cancelled;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700172 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200173 op->reserved = NULL;
Craig Tiller7a290982015-05-19 12:49:54 -0700174 op++;
Craig Tiller3121fd42015-09-10 09:56:20 -0700175 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL);
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200176 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller7a290982015-05-19 12:49:54 -0700177
Mark D. Roth7187ab92016-08-24 13:49:22 -0700178 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
179 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillerdad88a72015-05-26 17:01:09 -0700180 cq_verify(cqv);
Craig Tiller7a290982015-05-19 12:49:54 -0700181
182 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
183 GPR_ASSERT(0 == strcmp(details, "xyz"));
184 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
Muxi Yan6d686452016-09-19 16:48:36 -0700185 if (authority) {
186 GPR_ASSERT(0 == strcmp(call_details.host, authority));
187 }
Craig Tilleraea081f2015-06-11 14:19:33 -0700188 GPR_ASSERT(was_cancelled == 1);
Craig Tiller7a290982015-05-19 12:49:54 -0700189
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
196 grpc_call_destroy(c);
197 grpc_call_destroy(s);
198
Craig Tillerdad88a72015-05-26 17:01:09 -0700199 cq_verifier_destroy(cqv);
Craig Tiller7a290982015-05-19 12:49:54 -0700200}
201
202static void test_invoke_simple_request(grpc_end2end_test_config config) {
203 grpc_end2end_test_fixture f;
204
Craig Tiller35696192015-05-24 15:00:37 -0700205 f = begin_test(config, "test_invoke_simple_request", NULL, NULL);
Muxi Yan56456c32016-10-30 23:11:11 -0700206 simple_request_body(config, f);
Craig Tiller7a290982015-05-19 12:49:54 -0700207 end_test(&f);
208 config.tear_down_data(&f);
209}
210
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800211void server_finishes_request(grpc_end2end_test_config config) {
Craig Tiller7a290982015-05-19 12:49:54 -0700212 test_invoke_simple_request(config);
213}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700214
215void server_finishes_request_pre_init(void) {}