• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2017 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 <limits.h>
22 #include <string.h>
23 
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 #include <grpc/support/sync.h>
27 #include <grpc/support/time.h>
28 
29 #include "src/core/lib/gpr/useful.h"
30 #include "test/core/end2end/cq_verifier.h"
31 
32 #define MAX_CONNECTION_IDLE_MS 500
33 #define MAX_CONNECTION_AGE_MS 9999
34 
tag(intptr_t t)35 static void* tag(intptr_t t) { return (void*)t; }
36 
drain_cq(grpc_completion_queue * cq)37 static void drain_cq(grpc_completion_queue* cq) {
38   grpc_event ev;
39   do {
40     ev = grpc_completion_queue_next(cq, grpc_timeout_seconds_to_deadline(5),
41                                     nullptr);
42   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
43 }
44 
simple_request_body(grpc_end2end_test_config config,grpc_end2end_test_fixture * f)45 static void simple_request_body(grpc_end2end_test_config config,
46                                 grpc_end2end_test_fixture* f) {
47   grpc_call* c;
48   grpc_call* s;
49   cq_verifier* cqv = cq_verifier_create(f->cq);
50   grpc_op ops[6];
51   grpc_op* op;
52   grpc_metadata_array initial_metadata_recv;
53   grpc_metadata_array trailing_metadata_recv;
54   grpc_metadata_array request_metadata_recv;
55   grpc_call_details call_details;
56   grpc_status_code status;
57   grpc_call_error error;
58   grpc_slice details;
59   int was_cancelled = 2;
60   char* peer;
61 
62   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
63   c = grpc_channel_create_call(f->client, nullptr, GRPC_PROPAGATE_DEFAULTS,
64                                f->cq, grpc_slice_from_static_string("/foo"),
65                                nullptr, deadline, nullptr);
66   GPR_ASSERT(c);
67 
68   peer = grpc_call_get_peer(c);
69   GPR_ASSERT(peer != nullptr);
70   gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
71   gpr_free(peer);
72 
73   grpc_metadata_array_init(&initial_metadata_recv);
74   grpc_metadata_array_init(&trailing_metadata_recv);
75   grpc_metadata_array_init(&request_metadata_recv);
76   grpc_call_details_init(&call_details);
77 
78   memset(ops, 0, sizeof(ops));
79   op = ops;
80   op->op = GRPC_OP_SEND_INITIAL_METADATA;
81   op->data.send_initial_metadata.count = 0;
82   op->flags = 0;
83   op->reserved = nullptr;
84   op++;
85   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
86   op->flags = 0;
87   op->reserved = nullptr;
88   op++;
89   op->op = GRPC_OP_RECV_INITIAL_METADATA;
90   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
91   op->flags = 0;
92   op->reserved = nullptr;
93   op++;
94   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
95   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
96   op->data.recv_status_on_client.status = &status;
97   op->data.recv_status_on_client.status_details = &details;
98   op->flags = 0;
99   op->reserved = nullptr;
100   op++;
101   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
102                                 nullptr);
103   GPR_ASSERT(GRPC_CALL_OK == error);
104 
105   error =
106       grpc_server_request_call(f->server, &s, &call_details,
107                                &request_metadata_recv, f->cq, f->cq, tag(101));
108   GPR_ASSERT(GRPC_CALL_OK == error);
109   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
110   cq_verify(cqv);
111 
112   peer = grpc_call_get_peer(s);
113   GPR_ASSERT(peer != nullptr);
114   gpr_log(GPR_DEBUG, "server_peer=%s", peer);
115   gpr_free(peer);
116   peer = grpc_call_get_peer(c);
117   GPR_ASSERT(peer != nullptr);
118   gpr_log(GPR_DEBUG, "client_peer=%s", peer);
119   gpr_free(peer);
120 
121   memset(ops, 0, sizeof(ops));
122   op = ops;
123   op->op = GRPC_OP_SEND_INITIAL_METADATA;
124   op->data.send_initial_metadata.count = 0;
125   op->flags = 0;
126   op->reserved = nullptr;
127   op++;
128   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
129   op->data.send_status_from_server.trailing_metadata_count = 0;
130   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
131   grpc_slice status_details = grpc_slice_from_static_string("xyz");
132   op->data.send_status_from_server.status_details = &status_details;
133   op->flags = 0;
134   op->reserved = nullptr;
135   op++;
136   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
137   op->data.recv_close_on_server.cancelled = &was_cancelled;
138   op->flags = 0;
139   op->reserved = nullptr;
140   op++;
141   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
142                                 nullptr);
143   GPR_ASSERT(GRPC_CALL_OK == error);
144 
145   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
146   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
147   cq_verify(cqv);
148 
149   GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
150   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
151   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
152   GPR_ASSERT(0 == call_details.flags);
153   GPR_ASSERT(was_cancelled == 1);
154 
155   grpc_slice_unref(details);
156   grpc_metadata_array_destroy(&initial_metadata_recv);
157   grpc_metadata_array_destroy(&trailing_metadata_recv);
158   grpc_metadata_array_destroy(&request_metadata_recv);
159   grpc_call_details_destroy(&call_details);
160 
161   grpc_call_unref(c);
162   grpc_call_unref(s);
163 
164   cq_verifier_destroy(cqv);
165 }
166 
test_max_connection_idle(grpc_end2end_test_config config)167 static void test_max_connection_idle(grpc_end2end_test_config config) {
168   grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
169   grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
170   cq_verifier* cqv = cq_verifier_create(f.cq);
171 
172   grpc_arg client_a[1];
173   client_a[0].type = GRPC_ARG_INTEGER;
174   client_a[0].key =
175       const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
176   client_a[0].value.integer = 1000;
177   grpc_arg server_a[2];
178   server_a[0].type = GRPC_ARG_INTEGER;
179   server_a[0].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_IDLE_MS);
180   server_a[0].value.integer = MAX_CONNECTION_IDLE_MS;
181   server_a[1].type = GRPC_ARG_INTEGER;
182   server_a[1].key = const_cast<char*>(GRPC_ARG_MAX_CONNECTION_AGE_MS);
183   server_a[1].value.integer = MAX_CONNECTION_AGE_MS;
184   grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a};
185   grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
186 
187   config.init_client(&f, &client_args);
188   config.init_server(&f, &server_args);
189 
190   /* check that we're still in idle, and start connecting */
191   GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 1) ==
192              GRPC_CHANNEL_IDLE);
193   /* we'll go through some set of transitions (some might be missed), until
194      READY is reached */
195   while (state != GRPC_CHANNEL_READY) {
196     grpc_channel_watch_connectivity_state(
197         f.client, state, grpc_timeout_seconds_to_deadline(3), f.cq, tag(99));
198     CQ_EXPECT_COMPLETION(cqv, tag(99), 1);
199     cq_verify(cqv);
200     state = grpc_channel_check_connectivity_state(f.client, 0);
201     GPR_ASSERT(state == GRPC_CHANNEL_READY ||
202                state == GRPC_CHANNEL_CONNECTING ||
203                state == GRPC_CHANNEL_TRANSIENT_FAILURE);
204   }
205 
206   /* Use a simple request to cancel and reset the max idle timer */
207   simple_request_body(config, &f);
208 
209   /* wait for the channel to reach its maximum idle time */
210   grpc_channel_watch_connectivity_state(
211       f.client, GRPC_CHANNEL_READY,
212       grpc_timeout_milliseconds_to_deadline(MAX_CONNECTION_IDLE_MS + 3000),
213       f.cq, tag(99));
214   CQ_EXPECT_COMPLETION(cqv, tag(99), 1);
215   cq_verify(cqv);
216   state = grpc_channel_check_connectivity_state(f.client, 0);
217   GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
218              state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_IDLE);
219 
220   grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
221   CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
222   cq_verify(cqv);
223 
224   grpc_server_destroy(f.server);
225   grpc_channel_destroy(f.client);
226   grpc_completion_queue_shutdown(f.cq);
227   drain_cq(f.cq);
228   grpc_completion_queue_destroy(f.cq);
229   grpc_completion_queue_destroy(f.shutdown_cq);
230   config.tear_down_data(&f);
231 
232   cq_verifier_destroy(cqv);
233 }
234 
max_connection_idle(grpc_end2end_test_config config)235 void max_connection_idle(grpc_end2end_test_config config) {
236   test_max_connection_idle(config);
237 }
238 
max_connection_idle_pre_init(void)239 void max_connection_idle_pre_init(void) {}
240