blob: 7455c1cb3d944e86352ca099c64f4e9d986295a9 [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
39#include <grpc/byte_buffer.h>
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/time.h>
43#include <grpc/support/useful.h>
44#include "test/core/end2end/cq_verifier.h"
45
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046enum { TIMEOUT = 200000 };
47
48static void *tag(gpr_intptr t) { return (void *)t; }
49
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);
57 config.init_client(&f, client_args);
58 config.init_server(&f, server_args);
59 return f;
60}
61
62static gpr_timespec n_seconds_time(int n) {
Craig Tiller8ad8a412015-02-25 08:36:40 -080063 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064}
65
Craig Tiller32946d32015-01-15 11:37:30 -080066static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067
68static void drain_cq(grpc_completion_queue *cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070069 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 do {
71 ev = grpc_completion_queue_next(cq, five_seconds_time());
Craig Tiller64be9f72015-05-04 14:53:51 -070072 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073}
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));
78 GPR_ASSERT(grpc_completion_queue_pluck(f->cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5)).type == GRPC_OP_COMPLETE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079 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
Craig Tiller5cef1b32015-05-11 11:00:42 -070093 grpc_completion_queue_shutdown(f->cq);
94 drain_cq(f->cq);
95 grpc_completion_queue_destroy(f->cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080096}
97
98/* Cancel before invoke */
Craig Tiller8ad8a412015-02-25 08:36:40 -080099static void test_cancel_before_invoke(grpc_end2end_test_config config,
100 int test_ops) {
Craig Tillerfee91b42015-02-05 16:03:13 -0800101 grpc_op ops[6];
102 grpc_op *op;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103 grpc_call *c;
Craig Tiller35696192015-05-24 15:00:37 -0700104 grpc_end2end_test_fixture f = begin_test(config, "cancel_before_invoke", NULL, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105 gpr_timespec deadline = five_seconds_time();
Craig Tiller5cef1b32015-05-11 11:00:42 -0700106 cq_verifier *cqv = cq_verifier_create(f.cq);
Craig Tillerfee91b42015-02-05 16:03:13 -0800107 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;
112 char *details = NULL;
113 size_t details_capacity = 0;
114 grpc_byte_buffer *response_payload_recv = NULL;
115 gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world");
116 grpc_byte_buffer *request_payload =
117 grpc_byte_buffer_create(&request_payload_slice, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800118
Craig Tiller5cef1b32015-05-11 11:00:42 -0700119 c = grpc_channel_create_call(f.client, f.cq, "/foo",
Julien Boeuf597a4f22015-02-23 15:57:14 -0800120 "foo.test.google.fr", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121 GPR_ASSERT(c);
122
123 GPR_ASSERT(GRPC_CALL_OK == grpc_call_cancel(c));
124
Craig Tillerfee91b42015-02-05 16:03:13 -0800125 grpc_metadata_array_init(&initial_metadata_recv);
126 grpc_metadata_array_init(&trailing_metadata_recv);
127 grpc_metadata_array_init(&request_metadata_recv);
128 grpc_call_details_init(&call_details);
129
130 op = ops;
131 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
132 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
133 op->data.recv_status_on_client.status = &status;
134 op->data.recv_status_on_client.status_details = &details;
135 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
136 op++;
137 op->op = GRPC_OP_SEND_INITIAL_METADATA;
138 op->data.send_initial_metadata.count = 0;
139 op++;
140 op->op = GRPC_OP_SEND_MESSAGE;
141 op->data.send_message = request_payload;
142 op++;
143 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
144 op++;
145 op->op = GRPC_OP_RECV_INITIAL_METADATA;
146 op->data.recv_initial_metadata = &initial_metadata_recv;
147 op++;
148 op->op = GRPC_OP_RECV_MESSAGE;
149 op->data.recv_message = &response_payload_recv;
150 op++;
151 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, test_ops, tag(1)));
152
Craig Tillera0a6b122015-05-12 14:42:27 -0700153 cq_expect_completion(cqv, tag(1), 1);
Craig Tiller5cef1b32015-05-11 11:00:42 -0700154 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800155
Craig Tillerfee91b42015-02-05 16:03:13 -0800156 GPR_ASSERT(status == GRPC_STATUS_CANCELLED);
157
158 grpc_metadata_array_destroy(&initial_metadata_recv);
159 grpc_metadata_array_destroy(&trailing_metadata_recv);
160 grpc_metadata_array_destroy(&request_metadata_recv);
161 grpc_call_details_destroy(&call_details);
162
163 grpc_byte_buffer_destroy(request_payload);
164 grpc_byte_buffer_destroy(response_payload_recv);
165 gpr_free(details);
166
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800167 grpc_call_destroy(c);
168
Craig Tiller5cef1b32015-05-11 11:00:42 -0700169 cq_verifier_destroy(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170 end_test(&f);
171 config.tear_down_data(&f);
172}
173
174void grpc_end2end_tests(grpc_end2end_test_config config) {
Craig Tillerfee91b42015-02-05 16:03:13 -0800175 int i;
176 for (i = 1; i <= 6; i++) {
177 test_cancel_before_invoke(config, i);
178 }
Craig Tiller190d3602015-02-18 09:23:38 -0800179}