1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "test/core/end2end/end2end_tests.h"
20
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <grpc/byte_buffer.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29
30 #include "src/core/lib/gpr/useful.h"
31 #include "test/core/end2end/cq_verifier.h"
32 #include "test/core/end2end/tests/cancel_test_helpers.h"
33
tag(intptr_t t)34 static void* tag(intptr_t t) { return (void*)t; }
35
begin_test(grpc_end2end_test_config config,const char * test_name,cancellation_mode mode,size_t test_ops,grpc_channel_args * client_args,grpc_channel_args * server_args)36 static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
37 const char* test_name,
38 cancellation_mode mode,
39 size_t test_ops,
40 grpc_channel_args* client_args,
41 grpc_channel_args* server_args) {
42 grpc_end2end_test_fixture f;
43 gpr_log(GPR_INFO, "Running test: %s/%s/%s [%" PRIdPTR " ops]", test_name,
44 config.name, mode.name, test_ops);
45 f = config.create_fixture(client_args, server_args);
46 config.init_server(&f, server_args);
47 config.init_client(&f, client_args);
48 return f;
49 }
50
n_seconds_from_now(int n)51 static gpr_timespec n_seconds_from_now(int n) {
52 return grpc_timeout_seconds_to_deadline(n);
53 }
54
five_seconds_from_now(void)55 static gpr_timespec five_seconds_from_now(void) {
56 return n_seconds_from_now(5);
57 }
58
drain_cq(grpc_completion_queue * cq)59 static void drain_cq(grpc_completion_queue* cq) {
60 grpc_event ev;
61 do {
62 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
63 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
64 }
65
shutdown_server(grpc_end2end_test_fixture * f)66 static void shutdown_server(grpc_end2end_test_fixture* f) {
67 if (!f->server) return;
68 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
69 grpc_event ev = grpc_completion_queue_next(
70 f->cq, grpc_timeout_seconds_to_deadline(5), nullptr);
71 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
72 GPR_ASSERT(ev.tag == tag(1000));
73 grpc_server_destroy(f->server);
74 f->server = nullptr;
75 }
76
shutdown_client(grpc_end2end_test_fixture * f)77 static void shutdown_client(grpc_end2end_test_fixture* f) {
78 if (!f->client) return;
79 grpc_channel_destroy(f->client);
80 f->client = nullptr;
81 }
82
end_test(grpc_end2end_test_fixture * f)83 static void end_test(grpc_end2end_test_fixture* f) {
84 shutdown_server(f);
85 shutdown_client(f);
86
87 grpc_completion_queue_shutdown(f->cq);
88 drain_cq(f->cq);
89 grpc_completion_queue_destroy(f->cq);
90 grpc_completion_queue_destroy(f->shutdown_cq);
91 }
92
93 /* Cancel after invoke, no payload */
test_cancel_after_invoke(grpc_end2end_test_config config,cancellation_mode mode,size_t test_ops)94 static void test_cancel_after_invoke(grpc_end2end_test_config config,
95 cancellation_mode mode, size_t test_ops) {
96 grpc_op ops[6];
97 grpc_op* op;
98 grpc_call* c;
99 grpc_end2end_test_fixture f = begin_test(config, "test_cancel_after_invoke",
100 mode, test_ops, nullptr, nullptr);
101 cq_verifier* cqv = cq_verifier_create(f.cq);
102 grpc_metadata_array initial_metadata_recv;
103 grpc_metadata_array trailing_metadata_recv;
104 grpc_metadata_array request_metadata_recv;
105 grpc_call_details call_details;
106 grpc_status_code status;
107 grpc_call_error error;
108 grpc_slice details;
109 grpc_byte_buffer* response_payload_recv = nullptr;
110 grpc_slice request_payload_slice =
111 grpc_slice_from_copied_string("hello world");
112 grpc_byte_buffer* request_payload =
113 grpc_raw_byte_buffer_create(&request_payload_slice, 1);
114
115 gpr_timespec deadline = five_seconds_from_now();
116 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
117 grpc_slice_from_static_string("/foo"), nullptr,
118 deadline, nullptr);
119 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
126 memset(ops, 0, sizeof(ops));
127 op = ops;
128 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
129 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
130 op->data.recv_status_on_client.status = &status;
131 op->data.recv_status_on_client.status_details = &details;
132 op->flags = 0;
133 op->reserved = nullptr;
134 op++;
135 op->op = GRPC_OP_RECV_INITIAL_METADATA;
136 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
137 op->flags = 0;
138 op->reserved = nullptr;
139 op++;
140 op->op = GRPC_OP_SEND_INITIAL_METADATA;
141 op->data.send_initial_metadata.count = 0;
142 op->flags = 0;
143 op->reserved = nullptr;
144 op++;
145 op->op = GRPC_OP_SEND_MESSAGE;
146 op->data.send_message.send_message = request_payload;
147 op->flags = 0;
148 op->reserved = nullptr;
149 op++;
150 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
151 op->flags = 0;
152 op->reserved = nullptr;
153 op++;
154 op->op = GRPC_OP_RECV_MESSAGE;
155 op->data.recv_message.recv_message = &response_payload_recv;
156 op->flags = 0;
157 op->reserved = nullptr;
158 op++;
159 error = grpc_call_start_batch(c, ops, test_ops, tag(1), nullptr);
160 GPR_ASSERT(GRPC_CALL_OK == error);
161
162 GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr));
163
164 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
165 cq_verify(cqv);
166
167 GPR_ASSERT(status == mode.expect_status || status == GRPC_STATUS_INTERNAL);
168
169 grpc_metadata_array_destroy(&initial_metadata_recv);
170 grpc_metadata_array_destroy(&trailing_metadata_recv);
171 grpc_metadata_array_destroy(&request_metadata_recv);
172 grpc_call_details_destroy(&call_details);
173
174 grpc_byte_buffer_destroy(request_payload);
175 grpc_byte_buffer_destroy(response_payload_recv);
176 grpc_slice_unref(details);
177
178 grpc_call_unref(c);
179
180 cq_verifier_destroy(cqv);
181 end_test(&f);
182 config.tear_down_data(&f);
183 }
184
cancel_after_invoke(grpc_end2end_test_config config)185 void cancel_after_invoke(grpc_end2end_test_config config) {
186 unsigned i, j;
187
188 for (j = 3; j < 6; j++) {
189 for (i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); i++) {
190 test_cancel_after_invoke(config, cancellation_modes[i], j);
191 }
192 }
193 }
194
cancel_after_invoke_pre_init(void)195 void cancel_after_invoke_pre_init(void) {}
196