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 <stdio.h>
22 #include <string.h>
23
24 #include <grpc/byte_buffer.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/time.h>
28 #include "test/core/end2end/cq_verifier.h"
29
tag(intptr_t t)30 static void* tag(intptr_t t) { return (void*)t; }
31
n_seconds_from_now(int n)32 static gpr_timespec n_seconds_from_now(int n) {
33 return grpc_timeout_seconds_to_deadline(n);
34 }
35
five_seconds_from_now(void)36 static gpr_timespec five_seconds_from_now(void) {
37 return n_seconds_from_now(5);
38 }
39
drain_cq(grpc_completion_queue * cq)40 static void drain_cq(grpc_completion_queue* cq) {
41 grpc_event ev;
42 do {
43 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
44 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
45 }
46
shutdown_server(grpc_end2end_test_fixture * f)47 static void shutdown_server(grpc_end2end_test_fixture* f) {
48 if (!f->server) return;
49 grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
50 GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
51 grpc_timeout_seconds_to_deadline(5),
52 nullptr)
53 .type == GRPC_OP_COMPLETE);
54 grpc_server_destroy(f->server);
55 f->server = nullptr;
56 }
57
shutdown_client(grpc_end2end_test_fixture * f)58 static void shutdown_client(grpc_end2end_test_fixture* f) {
59 if (!f->client) return;
60 grpc_channel_destroy(f->client);
61 f->client = nullptr;
62 }
63
end_test(grpc_end2end_test_fixture * f)64 static void end_test(grpc_end2end_test_fixture* f) {
65 shutdown_server(f);
66 shutdown_client(f);
67
68 grpc_completion_queue_shutdown(f->cq);
69 drain_cq(f->cq);
70 grpc_completion_queue_destroy(f->cq);
71 grpc_completion_queue_destroy(f->shutdown_cq);
72 }
73
simple_delayed_request_body(grpc_end2end_test_config config,grpc_end2end_test_fixture * f,grpc_channel_args * client_args,grpc_channel_args * server_args,long delay_us)74 static void simple_delayed_request_body(grpc_end2end_test_config config,
75 grpc_end2end_test_fixture* f,
76 grpc_channel_args* client_args,
77 grpc_channel_args* server_args,
78 long delay_us) {
79 grpc_call* c;
80 grpc_call* s;
81 cq_verifier* cqv = cq_verifier_create(f->cq);
82 grpc_op ops[6];
83 grpc_op* op;
84 grpc_metadata_array initial_metadata_recv;
85 grpc_metadata_array trailing_metadata_recv;
86 grpc_metadata_array request_metadata_recv;
87 grpc_call_details call_details;
88 grpc_status_code status;
89 grpc_call_error error;
90 grpc_slice details;
91 int was_cancelled = 2;
92
93 config.init_client(f, client_args);
94 config.init_server(f, server_args);
95
96 gpr_timespec deadline = five_seconds_from_now();
97 c = grpc_channel_create_call(f->client, nullptr, GRPC_PROPAGATE_DEFAULTS,
98 f->cq, grpc_slice_from_static_string("/foo"),
99 nullptr, deadline, nullptr);
100 GPR_ASSERT(c);
101
102 grpc_metadata_array_init(&initial_metadata_recv);
103 grpc_metadata_array_init(&trailing_metadata_recv);
104 grpc_metadata_array_init(&request_metadata_recv);
105 grpc_call_details_init(&call_details);
106
107 memset(ops, 0, sizeof(ops));
108 op = ops;
109 op->op = GRPC_OP_SEND_INITIAL_METADATA;
110 op->data.send_initial_metadata.count = 0;
111 op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY;
112 op->reserved = nullptr;
113 op++;
114 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
115 op->flags = 0;
116 op->reserved = nullptr;
117 op++;
118 op->op = GRPC_OP_RECV_INITIAL_METADATA;
119 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
120 op->flags = 0;
121 op->reserved = nullptr;
122 op++;
123 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
124 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
125 op->data.recv_status_on_client.status = &status;
126 op->data.recv_status_on_client.status_details = &details;
127 op->flags = 0;
128 op->reserved = nullptr;
129 op++;
130 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
131 nullptr);
132 GPR_ASSERT(GRPC_CALL_OK == error);
133
134 error =
135 grpc_server_request_call(f->server, &s, &call_details,
136 &request_metadata_recv, f->cq, f->cq, tag(101));
137 GPR_ASSERT(GRPC_CALL_OK == error);
138 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
139 cq_verify(cqv);
140
141 memset(ops, 0, sizeof(ops));
142 op = ops;
143 op->op = GRPC_OP_SEND_INITIAL_METADATA;
144 op->data.send_initial_metadata.count = 0;
145 op->flags = 0;
146 op->reserved = nullptr;
147 op++;
148 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
149 op->data.send_status_from_server.trailing_metadata_count = 0;
150 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
151 grpc_slice status_details = grpc_slice_from_static_string("xyz");
152 op->data.send_status_from_server.status_details = &status_details;
153 op->flags = 0;
154 op->reserved = nullptr;
155 op++;
156 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
157 op->data.recv_close_on_server.cancelled = &was_cancelled;
158 op->flags = 0;
159 op->reserved = nullptr;
160 op++;
161 error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
162 nullptr);
163 GPR_ASSERT(GRPC_CALL_OK == error);
164
165 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
166 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
167 cq_verify(cqv);
168
169 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
170 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
171 GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
172 GPR_ASSERT(was_cancelled == 1);
173
174 grpc_slice_unref(details);
175 grpc_metadata_array_destroy(&initial_metadata_recv);
176 grpc_metadata_array_destroy(&trailing_metadata_recv);
177 grpc_metadata_array_destroy(&request_metadata_recv);
178 grpc_call_details_destroy(&call_details);
179
180 grpc_call_unref(c);
181 grpc_call_unref(s);
182
183 cq_verifier_destroy(cqv);
184 }
185
test_simple_delayed_request_short(grpc_end2end_test_config config)186 static void test_simple_delayed_request_short(grpc_end2end_test_config config) {
187 grpc_end2end_test_fixture f;
188 grpc_channel_args client_args;
189 grpc_arg arg_array[1];
190 arg_array[0].type = GRPC_ARG_INTEGER;
191 arg_array[0].key =
192 const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
193 arg_array[0].value.integer = 1000;
194 client_args.args = arg_array;
195 client_args.num_args = 1;
196
197 gpr_log(GPR_INFO, "Running test: %s/%s", "test_simple_delayed_request_short",
198 config.name);
199 f = config.create_fixture(nullptr, nullptr);
200
201 simple_delayed_request_body(config, &f, &client_args, nullptr, 100000);
202 end_test(&f);
203 config.tear_down_data(&f);
204 }
205
test_simple_delayed_request_long(grpc_end2end_test_config config)206 static void test_simple_delayed_request_long(grpc_end2end_test_config config) {
207 grpc_end2end_test_fixture f;
208 grpc_channel_args client_args;
209 grpc_arg arg_array[1];
210 arg_array[0].type = GRPC_ARG_INTEGER;
211 arg_array[0].key =
212 const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
213 arg_array[0].value.integer = 1000;
214 client_args.args = arg_array;
215 client_args.num_args = 1;
216
217 gpr_log(GPR_INFO, "Running test: %s/%s", "test_simple_delayed_request_long",
218 config.name);
219 f = config.create_fixture(nullptr, nullptr);
220 /* This timeout should be longer than a single retry */
221 simple_delayed_request_body(config, &f, &client_args, nullptr, 1500000);
222 end_test(&f);
223 config.tear_down_data(&f);
224 }
225
simple_delayed_request(grpc_end2end_test_config config)226 void simple_delayed_request(grpc_end2end_test_config config) {
227 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
228 test_simple_delayed_request_short(config);
229 test_simple_delayed_request_long(config);
230 }
231
simple_delayed_request_pre_init(void)232 void simple_delayed_request_pre_init(void) {}
233