blob: 1e0e33b4e2f420ea0b7e7ff2d1be65d70be2e736 [file] [log] [blame]
Craig Tiller32bd81d2015-11-03 13:02:07 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller32bd81d2015-11-03 13:02:07 -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
Craig Tiller32bd81d2015-11-03 13:02:07 -080039#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 Tiller32bd81d2015-11-03 13:02:07 -080046#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 Tiller32bd81d2015-11-03 13:02:07 -080049
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;
Robbie Shade55a046a2017-01-25 15:14:28 -050055 gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
Craig Tiller32bd81d2015-11-03 13:02:07 -080056 f = config.create_fixture(client_args, server_args);
Craig Tiller32bd81d2015-11-03 13:02:07 -080057 config.init_server(&f, server_args);
Mark D. Rothe127a392016-10-27 08:27:15 -070058 config.init_client(&f, client_args);
Craig Tiller32bd81d2015-11-03 13:02:07 -080059 return f;
60}
61
Chris Evansed2a5472017-03-27 17:34:51 -050062static gpr_timespec n_seconds_from_now(int n) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050063 return grpc_timeout_seconds_to_deadline(n);
Craig Tiller32bd81d2015-11-03 13:02:07 -080064}
65
Chris Evansed2a5472017-03-27 17:34:51 -050066static gpr_timespec five_seconds_from_now(void) {
67 return n_seconds_from_now(5);
68}
Craig Tiller32bd81d2015-11-03 13:02:07 -080069
70static void drain_cq(grpc_completion_queue *cq) {
71 grpc_event ev;
72 do {
Chris Evansed2a5472017-03-27 17:34:51 -050073 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), NULL);
Craig Tiller32bd81d2015-11-03 13:02:07 -080074 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
75}
76
77static void shutdown_server(grpc_end2end_test_fixture *f) {
78 if (!f->server) return;
79 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
Craig Tillerf40df232016-03-25 13:38:14 -070080 GPR_ASSERT(grpc_completion_queue_pluck(
Robbie Shadeca7effc2017-01-17 09:14:29 -050081 f->cq, tag(1000), grpc_timeout_seconds_to_deadline(5), NULL)
Craig Tillerf40df232016-03-25 13:38:14 -070082 .type == GRPC_OP_COMPLETE);
Craig Tiller32bd81d2015-11-03 13:02:07 -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
97 grpc_completion_queue_shutdown(f->cq);
98 drain_cq(f->cq);
99 grpc_completion_queue_destroy(f->cq);
100}
101
Muxi Yan5ebd3272016-10-31 07:27:07 -0700102static void simple_request_body(grpc_end2end_test_config config,
103 grpc_end2end_test_fixture f, size_t num_ops) {
Craig Tiller32bd81d2015-11-03 13:02:07 -0800104 grpc_call *c;
Craig Tiller32bd81d2015-11-03 13:02:07 -0800105 cq_verifier *cqv = cq_verifier_create(f.cq);
106 grpc_op ops[6];
107 grpc_op *op;
108 grpc_metadata_array initial_metadata_recv;
109 grpc_metadata_array trailing_metadata_recv;
110 grpc_status_code status;
111 grpc_call_error error;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800112 grpc_slice details;
Craig Tiller32bd81d2015-11-03 13:02:07 -0800113
Yuchen Zeng64c0e8d2016-06-10 11:19:51 -0700114 gpr_log(GPR_DEBUG, "test with %" PRIuPTR " ops", num_ops);
Craig Tiller32bd81d2015-11-03 13:02:07 -0800115
Chris Evansed2a5472017-03-27 17:34:51 -0500116 gpr_timespec deadline = five_seconds_from_now();
Muxi Yan5ebd3272016-10-31 07:27:07 -0700117 c = grpc_channel_create_call(
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800118 f.client, NULL, GRPC_PROPAGATE_DEFAULTS, f.cq,
119 grpc_slice_from_static_string("/foo"),
120 get_host_override_slice("foo.test.google.fr:1234", config), deadline,
Muxi Yan5ebd3272016-10-31 07:27:07 -0700121 NULL);
Craig Tiller32bd81d2015-11-03 13:02:07 -0800122 GPR_ASSERT(c);
123
124 grpc_metadata_array_init(&initial_metadata_recv);
125 grpc_metadata_array_init(&trailing_metadata_recv);
126
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700127 memset(ops, 0, sizeof(ops));
Craig Tiller32bd81d2015-11-03 13:02:07 -0800128 op = ops;
129 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
130 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
131 op->data.recv_status_on_client.status = &status;
132 op->data.recv_status_on_client.status_details = &details;
Craig Tiller32bd81d2015-11-03 13:02:07 -0800133 op->flags = 0;
134 op->reserved = NULL;
135 op++;
136 op->op = GRPC_OP_RECV_INITIAL_METADATA;
Mark D. Roth435f9f22017-01-25 12:53:54 -0800137 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
Craig Tiller32bd81d2015-11-03 13:02:07 -0800138 op->flags = 0;
139 op->reserved = NULL;
140 op++;
141 op->op = GRPC_OP_SEND_INITIAL_METADATA;
142 op->data.send_initial_metadata.count = 0;
143 op->flags = 0;
144 op->reserved = NULL;
145 op++;
146 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
147 op->flags = 0;
148 op->reserved = NULL;
149 op++;
150 GPR_ASSERT(num_ops <= (size_t)(op - ops));
151 error = grpc_call_start_batch(c, ops, num_ops, tag(1), NULL);
152 GPR_ASSERT(GRPC_CALL_OK == error);
153
154 grpc_call_cancel_with_status(c, GRPC_STATUS_UNIMPLEMENTED, "xyz", NULL);
155
Mark D. Roth7187ab92016-08-24 13:49:22 -0700156 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tiller32bd81d2015-11-03 13:02:07 -0800157 cq_verify(cqv);
158
159 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800160 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
Craig Tiller32bd81d2015-11-03 13:02:07 -0800161
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800162 grpc_slice_unref(details);
Craig Tiller32bd81d2015-11-03 13:02:07 -0800163 grpc_metadata_array_destroy(&initial_metadata_recv);
164 grpc_metadata_array_destroy(&trailing_metadata_recv);
165
166 grpc_call_destroy(c);
167
168 cq_verifier_destroy(cqv);
169}
170
Craig Tillerb774be42015-11-19 07:56:13 -0800171static void test_invoke_simple_request(grpc_end2end_test_config config,
172 size_t num_ops) {
Craig Tiller32bd81d2015-11-03 13:02:07 -0800173 grpc_end2end_test_fixture f;
174
175 f = begin_test(config, "test_invoke_simple_request", NULL, NULL);
Muxi Yan56456c32016-10-30 23:11:11 -0700176 simple_request_body(config, f, num_ops);
Craig Tiller32bd81d2015-11-03 13:02:07 -0800177 end_test(&f);
178 config.tear_down_data(&f);
179}
180
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800181void cancel_with_status(grpc_end2end_test_config config) {
Craig Tiller32bd81d2015-11-03 13:02:07 -0800182 size_t i;
183 for (i = 1; i <= 4; i++) {
184 test_invoke_simple_request(config, i);
185 }
186}
Craig Tiller9e9edbc2016-04-04 10:38:49 -0700187
188void cancel_with_status_pre_init(void) {}