blob: 074170d2f0ac8a340db3c95aa132d6a129d356b7 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "test/core/end2end/end2end_tests.h"
35
36#include <stdio.h>
37#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
39#include <grpc/byte_buffer.h>
40#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
42#include <grpc/support/time.h>
43#include <grpc/support/useful.h>
44#include "test/core/end2end/cq_verifier.h"
45
46enum { TIMEOUT = 200000 };
47
48static void *tag(gpr_intptr t) { return (void *)t; }
49
50static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
51 const char *test_name,
52 grpc_channel_args *client_args,
53 grpc_channel_args *server_args) {
54 grpc_end2end_test_fixture f;
55 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
56 f = config.create_fixture(client_args, server_args);
57 config.init_client(&f, client_args);
58 config.init_server(&f, server_args);
59 return f;
60}
61
62static gpr_timespec n_seconds_time(int n) {
Craig Tiller8ad8a412015-02-25 08:36:40 -080063 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064}
65
Craig Tiller32946d32015-01-15 11:37:30 -080066static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067
68static void drain_cq(grpc_completion_queue *cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070069 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 do {
71 ev = grpc_completion_queue_next(cq, five_seconds_time());
Craig Tiller64be9f72015-05-04 14:53:51 -070072 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073}
74
75static void shutdown_server(grpc_end2end_test_fixture *f) {
76 if (!f->server) return;
77 grpc_server_shutdown(f->server);
78 grpc_server_destroy(f->server);
79 f->server = NULL;
80}
81
82static void shutdown_client(grpc_end2end_test_fixture *f) {
83 if (!f->client) return;
84 grpc_channel_destroy(f->client);
85 f->client = NULL;
86}
87
88static void end_test(grpc_end2end_test_fixture *f) {
89 shutdown_server(f);
90 shutdown_client(f);
91
Craig Tillerbc0ec332015-05-11 12:11:32 -070092 grpc_completion_queue_shutdown(f->cq);
93 drain_cq(f->cq);
94 grpc_completion_queue_destroy(f->cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095}
96
97/* Client sends a request with payload, server reads then returns status. */
98static void test_invoke_request_with_payload(grpc_end2end_test_config config) {
99 grpc_call *c;
100 grpc_call *s;
Craig Tillercff2d152015-02-05 13:22:49 -0800101 gpr_slice request_payload_slice = gpr_slice_from_copied_string("hello world");
102 grpc_byte_buffer *request_payload =
103 grpc_byte_buffer_create(&request_payload_slice, 1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104 gpr_timespec deadline = five_seconds_time();
Craig Tiller35696192015-05-24 15:00:37 -0700105 grpc_end2end_test_fixture f = begin_test(config, "test_invoke_request_with_payload", NULL, NULL);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700106 cq_verifier *cqv = cq_verifier_create(f.cq);
Craig Tillercff2d152015-02-05 13:22:49 -0800107 grpc_op ops[6];
108 grpc_op *op;
109 grpc_metadata_array initial_metadata_recv;
110 grpc_metadata_array trailing_metadata_recv;
111 grpc_metadata_array request_metadata_recv;
112 grpc_byte_buffer *request_payload_recv = NULL;
113 grpc_call_details call_details;
114 grpc_status_code status;
115 char *details = NULL;
116 size_t details_capacity = 0;
117 int was_cancelled = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800118
Craig Tillerbc0ec332015-05-11 12:11:32 -0700119 c = grpc_channel_create_call(f.client, f.cq, "/foo",
Julien Boeuf597a4f22015-02-23 15:57:14 -0800120 "foo.test.google.fr", deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121 GPR_ASSERT(c);
122
Craig Tillercff2d152015-02-05 13:22:49 -0800123 grpc_metadata_array_init(&initial_metadata_recv);
124 grpc_metadata_array_init(&trailing_metadata_recv);
125 grpc_metadata_array_init(&request_metadata_recv);
126 grpc_call_details_init(&call_details);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127
Craig Tillercff2d152015-02-05 13:22:49 -0800128 op = ops;
129 op->op = GRPC_OP_SEND_INITIAL_METADATA;
130 op->data.send_initial_metadata.count = 0;
131 op++;
132 op->op = GRPC_OP_SEND_MESSAGE;
133 op->data.send_message = request_payload;
134 op++;
135 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
136 op++;
137 op->op = GRPC_OP_RECV_INITIAL_METADATA;
138 op->data.recv_initial_metadata = &initial_metadata_recv;
139 op++;
140 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
141 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
142 op->data.recv_status_on_client.status = &status;
143 op->data.recv_status_on_client.status_details = &details;
144 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
145 op++;
146 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700148 GPR_ASSERT(GRPC_CALL_OK ==
149 grpc_server_request_call(f.server, &s, &call_details,
Craig Tillerbc0ec332015-05-11 12:11:32 -0700150 &request_metadata_recv, f.cq,
151 f.cq, tag(101)));
Craig Tillera0a6b122015-05-12 14:42:27 -0700152 cq_expect_completion(cqv, tag(101), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700153 cq_verify(cqv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800154
Craig Tillercff2d152015-02-05 13:22:49 -0800155 op = ops;
156 op->op = GRPC_OP_SEND_INITIAL_METADATA;
157 op->data.send_initial_metadata.count = 0;
158 op++;
Craig Tiller4541f332015-05-19 16:26:33 -0700159 op->op = GRPC_OP_RECV_MESSAGE;
160 op->data.recv_message = &request_payload_recv;
161 op++;
162 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
163
Craig Tillerdad88a72015-05-26 17:01:09 -0700164 cq_expect_completion(cqv, tag(102), 1);
165 cq_verify(cqv);
166
Craig Tiller4541f332015-05-19 16:26:33 -0700167 op = ops;
168 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
169 op->data.recv_close_on_server.cancelled = &was_cancelled;
170 op++;
Craig Tillercff2d152015-02-05 13:22:49 -0800171 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
172 op->data.send_status_from_server.trailing_metadata_count = 0;
Craig Tillerd1abc812015-05-06 14:35:19 -0700173 op->data.send_status_from_server.status = GRPC_STATUS_OK;
Craig Tillercff2d152015-02-05 13:22:49 -0800174 op->data.send_status_from_server.status_details = "xyz";
175 op++;
Craig Tiller4541f332015-05-19 16:26:33 -0700176 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(103)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177
Craig Tillerdad88a72015-05-26 17:01:09 -0700178 cq_expect_completion(cqv, tag(103), 1);
Craig Tillera0a6b122015-05-12 14:42:27 -0700179 cq_expect_completion(cqv, tag(1), 1);
Craig Tillerbc0ec332015-05-11 12:11:32 -0700180 cq_verify(cqv);
Craig Tillercff2d152015-02-05 13:22:49 -0800181
Craig Tillerd1abc812015-05-06 14:35:19 -0700182 GPR_ASSERT(status == GRPC_STATUS_OK);
Craig Tillercff2d152015-02-05 13:22:49 -0800183 GPR_ASSERT(0 == strcmp(details, "xyz"));
184 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
Julien Boeuf597a4f22015-02-23 15:57:14 -0800185 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr"));
Craig Tillere801eb32015-02-10 14:01:14 -0800186 GPR_ASSERT(was_cancelled == 0);
Craig Tillercff2d152015-02-05 13:22:49 -0800187 GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
188
189 gpr_free(details);
190 grpc_metadata_array_destroy(&initial_metadata_recv);
191 grpc_metadata_array_destroy(&trailing_metadata_recv);
192 grpc_metadata_array_destroy(&request_metadata_recv);
193 grpc_call_details_destroy(&call_details);
194
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195 grpc_call_destroy(c);
196 grpc_call_destroy(s);
197
Craig Tillerbc0ec332015-05-11 12:11:32 -0700198 cq_verifier_destroy(cqv);
Craig Tillercff2d152015-02-05 13:22:49 -0800199
200 grpc_byte_buffer_destroy(request_payload);
201 grpc_byte_buffer_destroy(request_payload_recv);
202
203 end_test(&f);
204 config.tear_down_data(&f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205}
206
207void grpc_end2end_tests(grpc_end2end_test_config config) {
208 test_invoke_request_with_payload(config);
Craig Tiller190d3602015-02-18 09:23:38 -0800209}