• 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/compression.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/channel/channel_args.h"
31 #include "src/core/lib/surface/call.h"
32 #include "test/core/end2end/cq_verifier.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,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                                             grpc_channel_args* client_args,
39                                             grpc_channel_args* server_args) {
40   grpc_end2end_test_fixture f;
41   gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
42   f = config.create_fixture(client_args, server_args);
43   config.init_server(&f, server_args);
44   config.init_client(&f, client_args);
45   return f;
46 }
47 
n_seconds_from_now(int n)48 static gpr_timespec n_seconds_from_now(int n) {
49   return grpc_timeout_seconds_to_deadline(n);
50 }
51 
five_seconds_from_now(void)52 static gpr_timespec five_seconds_from_now(void) {
53   return n_seconds_from_now(5);
54 }
55 
drain_cq(grpc_completion_queue * cq)56 static void drain_cq(grpc_completion_queue* cq) {
57   grpc_event ev;
58   do {
59     ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
60   } while (ev.type != GRPC_QUEUE_SHUTDOWN);
61 }
62 
shutdown_server(grpc_end2end_test_fixture * f)63 static void shutdown_server(grpc_end2end_test_fixture* f) {
64   if (!f->server) return;
65   grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
66   GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
67                                          grpc_timeout_seconds_to_deadline(5),
68                                          nullptr)
69                  .type == GRPC_OP_COMPLETE);
70   grpc_server_destroy(f->server);
71   f->server = nullptr;
72 }
73 
shutdown_client(grpc_end2end_test_fixture * f)74 static void shutdown_client(grpc_end2end_test_fixture* f) {
75   if (!f->client) return;
76   grpc_channel_destroy(f->client);
77   f->client = nullptr;
78 }
79 
end_test(grpc_end2end_test_fixture * f)80 static void end_test(grpc_end2end_test_fixture* f) {
81   shutdown_server(f);
82   shutdown_client(f);
83 
84   grpc_completion_queue_shutdown(f->cq);
85   drain_cq(f->cq);
86   grpc_completion_queue_destroy(f->cq);
87   grpc_completion_queue_destroy(f->shutdown_cq);
88 }
89 
90 /* Client pings and server pongs. Repeat messages rounds before finishing. */
test_pingpong_streaming(grpc_end2end_test_config config,int messages)91 static void test_pingpong_streaming(grpc_end2end_test_config config,
92                                     int messages) {
93   grpc_channel_args* client_args = grpc_channel_args_set_compression_algorithm(
94       nullptr, GRPC_COMPRESS_STREAM_GZIP);
95   grpc_channel_args* server_args = grpc_channel_args_set_compression_algorithm(
96       nullptr, GRPC_COMPRESS_STREAM_GZIP);
97   grpc_end2end_test_fixture f =
98       begin_test(config, "test_pingpong_streaming", client_args, server_args);
99   grpc_call* c;
100   grpc_call* s;
101   cq_verifier* cqv = cq_verifier_create(f.cq);
102   grpc_op ops[6];
103   grpc_op* op;
104   grpc_metadata_array initial_metadata_recv;
105   grpc_metadata_array trailing_metadata_recv;
106   grpc_metadata_array request_metadata_recv;
107   grpc_call_details call_details;
108   grpc_status_code status;
109   grpc_call_error error;
110   grpc_slice details;
111   int was_cancelled = 2;
112   grpc_byte_buffer* request_payload;
113   grpc_byte_buffer* request_payload_recv;
114   grpc_byte_buffer* response_payload;
115   grpc_byte_buffer* response_payload_recv;
116   int i;
117   grpc_slice request_payload_slice =
118       grpc_slice_from_copied_string("hello world");
119   grpc_slice response_payload_slice =
120       grpc_slice_from_copied_string("hello you");
121 
122   gpr_timespec deadline = five_seconds_from_now();
123   c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
124                                grpc_slice_from_static_string("/foo"), nullptr,
125                                deadline, nullptr);
126   GPR_ASSERT(c);
127 
128   grpc_metadata_array_init(&initial_metadata_recv);
129   grpc_metadata_array_init(&trailing_metadata_recv);
130   grpc_metadata_array_init(&request_metadata_recv);
131   grpc_call_details_init(&call_details);
132 
133   memset(ops, 0, sizeof(ops));
134   op = ops;
135   op->op = GRPC_OP_SEND_INITIAL_METADATA;
136   op->data.send_initial_metadata.count = 0;
137   op->flags = 0;
138   op->reserved = nullptr;
139   op++;
140   op->op = GRPC_OP_RECV_INITIAL_METADATA;
141   op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
142   op->flags = 0;
143   op->reserved = nullptr;
144   op++;
145   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
146   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
147   op->data.recv_status_on_client.status = &status;
148   op->data.recv_status_on_client.status_details = &details;
149   op->flags = 0;
150   op->reserved = nullptr;
151   op++;
152   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
153                                 nullptr);
154   GPR_ASSERT(GRPC_CALL_OK == error);
155 
156   error =
157       grpc_server_request_call(f.server, &s, &call_details,
158                                &request_metadata_recv, f.cq, f.cq, tag(100));
159   GPR_ASSERT(GRPC_CALL_OK == error);
160   CQ_EXPECT_COMPLETION(cqv, tag(100), 1);
161   cq_verify(cqv);
162 
163   memset(ops, 0, sizeof(ops));
164   op = ops;
165   op->op = GRPC_OP_SEND_INITIAL_METADATA;
166   op->data.send_initial_metadata.count = 0;
167   op->flags = 0;
168   op->reserved = nullptr;
169   op++;
170   op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
171   op->data.recv_close_on_server.cancelled = &was_cancelled;
172   op->flags = 0;
173   op->reserved = nullptr;
174   op++;
175   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(101),
176                                 nullptr);
177   GPR_ASSERT(GRPC_CALL_OK == error);
178 
179   for (i = 0; i < messages; i++) {
180     request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
181     response_payload = grpc_raw_byte_buffer_create(&response_payload_slice, 1);
182 
183     memset(ops, 0, sizeof(ops));
184     op = ops;
185     op->op = GRPC_OP_SEND_MESSAGE;
186     op->data.send_message.send_message = request_payload;
187     op->flags = 0;
188     op->reserved = nullptr;
189     op++;
190     op->op = GRPC_OP_RECV_MESSAGE;
191     op->data.recv_message.recv_message = &response_payload_recv;
192     op->flags = 0;
193     op->reserved = nullptr;
194     op++;
195     error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
196                                   nullptr);
197     GPR_ASSERT(GRPC_CALL_OK == error);
198 
199     memset(ops, 0, sizeof(ops));
200     op = ops;
201     op->op = GRPC_OP_RECV_MESSAGE;
202     op->data.recv_message.recv_message = &request_payload_recv;
203     op->flags = 0;
204     op->reserved = nullptr;
205     op++;
206     error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
207                                   tag(102), nullptr);
208     GPR_ASSERT(GRPC_CALL_OK == error);
209     CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
210     cq_verify(cqv);
211 
212     memset(ops, 0, sizeof(ops));
213     op = ops;
214     op->op = GRPC_OP_SEND_MESSAGE;
215     op->data.send_message.send_message = response_payload;
216     op->flags = 0;
217     op->reserved = nullptr;
218     op++;
219     error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
220                                   tag(103), nullptr);
221     GPR_ASSERT(GRPC_CALL_OK == error);
222     CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
223     CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
224     cq_verify(cqv);
225 
226     grpc_byte_buffer_destroy(request_payload);
227     grpc_byte_buffer_destroy(response_payload);
228     grpc_byte_buffer_destroy(request_payload_recv);
229     grpc_byte_buffer_destroy(response_payload_recv);
230   }
231 
232   grpc_slice_unref(request_payload_slice);
233   grpc_slice_unref(response_payload_slice);
234 
235   memset(ops, 0, sizeof(ops));
236   op = ops;
237   op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
238   op->flags = 0;
239   op->reserved = nullptr;
240   op++;
241   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
242                                 nullptr);
243   GPR_ASSERT(GRPC_CALL_OK == error);
244 
245   memset(ops, 0, sizeof(ops));
246   op = ops;
247   op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
248   op->data.send_status_from_server.trailing_metadata_count = 0;
249   op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
250   grpc_slice status_details = grpc_slice_from_static_string("xyz");
251   op->data.send_status_from_server.status_details = &status_details;
252   op->flags = 0;
253   op->reserved = nullptr;
254   op++;
255   error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
256                                 nullptr);
257   GPR_ASSERT(GRPC_CALL_OK == error);
258 
259   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
260   CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
261   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
262   CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
263   cq_verify(cqv);
264 
265   grpc_call_unref(c);
266   grpc_call_unref(s);
267 
268   cq_verifier_destroy(cqv);
269 
270   grpc_metadata_array_destroy(&initial_metadata_recv);
271   grpc_metadata_array_destroy(&trailing_metadata_recv);
272   grpc_metadata_array_destroy(&request_metadata_recv);
273   grpc_call_details_destroy(&call_details);
274   grpc_slice_unref(details);
275 
276   end_test(&f);
277   config.tear_down_data(&f);
278   {
279     grpc_core::ExecCtx exec_ctx;
280     grpc_channel_args_destroy(client_args);
281     grpc_channel_args_destroy(server_args);
282   }
283 }
284 
stream_compression_ping_pong_streaming(grpc_end2end_test_config config)285 void stream_compression_ping_pong_streaming(grpc_end2end_test_config config) {
286   int i;
287 
288   for (i = 1; i < 10; i++) {
289     test_pingpong_streaming(config, i);
290   }
291 }
292 
stream_compression_ping_pong_streaming_pre_init(void)293 void stream_compression_ping_pong_streaming_pre_init(void) {}
294