• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_destroy(f->server);
50   f->server = nullptr;
51 }
52 
shutdown_client(grpc_end2end_test_fixture * f)53 static void shutdown_client(grpc_end2end_test_fixture* f) {
54   if (!f->client) return;
55   grpc_channel_destroy(f->client);
56   f->client = nullptr;
57 }
58 
end_test(grpc_end2end_test_fixture * f)59 static void end_test(grpc_end2end_test_fixture* f) {
60   shutdown_server(f);
61   shutdown_client(f);
62 
63   grpc_completion_queue_shutdown(f->cq);
64   drain_cq(f->cq);
65   grpc_completion_queue_destroy(f->cq);
66 
67   /* Note: shutdown_cq was unused in this test */
68   grpc_completion_queue_destroy(f->shutdown_cq);
69 }
70 
do_request_and_shutdown_server(grpc_end2end_test_config config,grpc_end2end_test_fixture * f,cq_verifier * cqv)71 static void do_request_and_shutdown_server(grpc_end2end_test_config config,
72                                            grpc_end2end_test_fixture* f,
73                                            cq_verifier* cqv) {
74   grpc_call* c;
75   grpc_call* s;
76   grpc_op ops[6];
77   grpc_op* op;
78   grpc_metadata_array initial_metadata_recv;
79   grpc_metadata_array trailing_metadata_recv;
80   grpc_metadata_array request_metadata_recv;
81   grpc_call_details call_details;
82   grpc_status_code status;
83   grpc_call_error error;
84   grpc_slice details;
85   int was_cancelled = 2;
86 
87   gpr_timespec deadline = five_seconds_from_now();
88   c = grpc_channel_create_call(f->client, nullptr, GRPC_PROPAGATE_DEFAULTS,
89                                f->cq, grpc_slice_from_static_string("/foo"),
90                                nullptr, deadline, nullptr);
91   GPR_ASSERT(c);
92 
93   grpc_metadata_array_init(&initial_metadata_recv);
94   grpc_metadata_array_init(&trailing_metadata_recv);
95   grpc_metadata_array_init(&request_metadata_recv);
96   grpc_call_details_init(&call_details);
97 
98   memset(ops, 0, sizeof(ops));
99   op = ops;
100   op->op = GRPC_OP_SEND_INITIAL_METADATA;
101   op->data.send_initial_metadata.count = 0;
102   op->flags = 0;
103   op->reserved = nullptr;
104   op++;
105   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
106   op->flags = 0;
107   op->reserved = nullptr;
108   op++;
109   op->op = GRPC_OP_RECV_INITIAL_METADATA;
110   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
111   op->flags = 0;
112   op->reserved = nullptr;
113   op++;
114   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
115   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
116   op->data.recv_status_on_client.status = &status;
117   op->data.recv_status_on_client.status_details = &details;
118   op->flags = 0;
119   op->reserved = nullptr;
120   op++;
121   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
122                                 nullptr);
123   GPR_ASSERT(GRPC_CALL_OK == error);
124 
125   error =
126       grpc_server_request_call(f->server, &s, &call_details,
127                                &request_metadata_recv, f->cq, f->cq, tag(101));
128   GPR_ASSERT(GRPC_CALL_OK == error);
129   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
130   cq_verify(cqv);
131 
132   /* should be able to shut down the server early
133      - and still complete the request */
134   grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
135 
136   memset(ops, 0, sizeof(ops));
137   op = ops;
138   op->op = GRPC_OP_SEND_INITIAL_METADATA;
139   op->data.send_initial_metadata.count = 0;
140   op->flags = 0;
141   op->reserved = nullptr;
142   op++;
143   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
144   op->data.send_status_from_server.trailing_metadata_count = 0;
145   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
146   grpc_slice status_details = grpc_slice_from_static_string("xyz");
147   op->data.send_status_from_server.status_details = &status_details;
148   op->flags = 0;
149   op->reserved = nullptr;
150   op++;
151   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
152   op->data.recv_close_on_server.cancelled = &was_cancelled;
153   op->flags = 0;
154   op->reserved = nullptr;
155   op++;
156   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
157                                 nullptr);
158   GPR_ASSERT(GRPC_CALL_OK == error);
159 
160   CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
161   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
162   CQ_EXPECT_COMPLETION(cqv, tag(1000), 1);
163   cq_verify(cqv);
164 
165   GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
166   GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
167   GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
168   GPR_ASSERT(was_cancelled == 1);
169 
170   grpc_slice_unref(details);
171   grpc_metadata_array_destroy(&initial_metadata_recv);
172   grpc_metadata_array_destroy(&trailing_metadata_recv);
173   grpc_metadata_array_destroy(&request_metadata_recv);
174   grpc_call_details_destroy(&call_details);
175 
176   grpc_call_unref(c);
177   grpc_call_unref(s);
178 }
179 
disappearing_server_test(grpc_end2end_test_config config)180 static void disappearing_server_test(grpc_end2end_test_config config) {
181   grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
182   cq_verifier* cqv = cq_verifier_create(f.cq);
183 
184   gpr_log(GPR_INFO, "Running test: %s/%s", "disappearing_server_test",
185           config.name);
186 
187   config.init_client(&f, nullptr);
188   config.init_server(&f, nullptr);
189 
190   do_request_and_shutdown_server(config, &f, cqv);
191 
192   /* now destroy and recreate the server */
193   config.init_server(&f, nullptr);
194 
195   do_request_and_shutdown_server(config, &f, cqv);
196 
197   cq_verifier_destroy(cqv);
198 
199   end_test(&f);
200   config.tear_down_data(&f);
201 }
202 
disappearing_server(grpc_end2end_test_config config)203 void disappearing_server(grpc_end2end_test_config config) {
204   GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
205   disappearing_server_test(config);
206 }
207 
disappearing_server_pre_init(void)208 void disappearing_server_pre_init(void) {}
209