blob: 09b3c864fdd491f471b0159883b493843935d77c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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>
38#include <unistd.h>
39
40#include <grpc/byte_buffer.h>
41#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
43#include <grpc/support/time.h>
44#include <grpc/support/useful.h>
45#include "test/core/end2end/cq_verifier.h"
46
47enum { TIMEOUT = 200000 };
48
49static void *tag(gpr_intptr t) { return (void *)t; }
50
51static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
52 const char *test_name,
53 grpc_channel_args *client_args,
54 grpc_channel_args *server_args) {
55 grpc_end2end_test_fixture f;
56 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
57 f = config.create_fixture(client_args, server_args);
58 config.init_client(&f, client_args);
59 config.init_server(&f, server_args);
60 return f;
61}
62
63static gpr_timespec n_seconds_time(int n) {
64 return gpr_time_add(gpr_now(), gpr_time_from_micros(GPR_US_PER_SEC * n));
65}
66
Craig Tiller32946d32015-01-15 11:37:30 -080067static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068
69static void drain_cq(grpc_completion_queue *cq) {
70 grpc_event *ev;
71 grpc_completion_type type;
72 do {
73 ev = grpc_completion_queue_next(cq, five_seconds_time());
74 GPR_ASSERT(ev);
75 type = ev->type;
76 grpc_event_finish(ev);
77 } while (type != GRPC_QUEUE_SHUTDOWN);
78}
79
80static void shutdown_server(grpc_end2end_test_fixture *f) {
81 if (!f->server) return;
82 grpc_server_shutdown(f->server);
83 grpc_server_destroy(f->server);
84 f->server = NULL;
85}
86
87static void shutdown_client(grpc_end2end_test_fixture *f) {
88 if (!f->client) return;
89 grpc_channel_destroy(f->client);
90 f->client = NULL;
91}
92
93static void end_test(grpc_end2end_test_fixture *f) {
94 shutdown_server(f);
95 shutdown_client(f);
96
97 grpc_completion_queue_shutdown(f->server_cq);
98 drain_cq(f->server_cq);
99 grpc_completion_queue_destroy(f->server_cq);
100 grpc_completion_queue_shutdown(f->client_cq);
101 drain_cq(f->client_cq);
102 grpc_completion_queue_destroy(f->client_cq);
103}
104
105/* Client sends a request with payload, server reads then returns status. */
106static void test_invoke_request_with_payload(grpc_end2end_test_config config) {
107 grpc_call *c;
108 grpc_call *s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 gpr_slice payload_slice = gpr_slice_from_copied_string("hello world");
110 grpc_byte_buffer *payload = grpc_byte_buffer_create(&payload_slice, 1);
111 gpr_timespec deadline = five_seconds_time();
112 grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL);
113 cq_verifier *v_client = cq_verifier_create(f.client_cq);
114 cq_verifier *v_server = cq_verifier_create(f.server_cq);
115
116 /* byte buffer holds the slice, we can unref it already */
117 gpr_slice_unref(payload_slice);
118
119 c = grpc_channel_create_call(f.client, "/foo", "test.google.com", deadline);
120 GPR_ASSERT(c);
121
122 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f.server, tag(100)));
123
124 GPR_ASSERT(GRPC_CALL_OK ==
125 grpc_call_start_invoke(c, f.client_cq, tag(1), tag(2), tag(3), 0));
126 cq_expect_invoke_accepted(v_client, tag(1), GRPC_OP_OK);
127 cq_verify(v_client);
128
129 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write(c, payload, tag(4), 0));
130 /* destroy byte buffer early to ensure async code keeps track of its contents
131 correctly */
132 grpc_byte_buffer_destroy(payload);
133 cq_expect_write_accepted(v_client, tag(4), GRPC_OP_OK);
134 cq_verify(v_client);
135
136 cq_expect_server_rpc_new(v_server, &s, tag(100), "/foo", "test.google.com",
137 deadline, NULL);
138 cq_verify(v_server);
139
140 grpc_call_accept(s, f.server_cq, tag(102), 0);
141 cq_expect_client_metadata_read(v_client, tag(2), NULL);
142 cq_verify(v_client);
143
144 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_read(s, tag(4)));
145 cq_expect_read(v_server, tag(4), gpr_slice_from_copied_string("hello world"));
146
147 GPR_ASSERT(GRPC_CALL_OK == grpc_call_writes_done(c, tag(5)));
ctiller2845cad2014-12-15 15:14:12 -0800148 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_write_status(
149 s, GRPC_STATUS_UNIMPLEMENTED, "xyz", tag(6)));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150 cq_expect_finish_accepted(v_client, tag(5), GRPC_OP_OK);
ctiller2845cad2014-12-15 15:14:12 -0800151 cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_UNIMPLEMENTED,
152 "xyz", NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153 cq_verify(v_client);
154
155 cq_expect_finish_accepted(v_server, tag(6), GRPC_OP_OK);
156 cq_expect_finished(v_server, tag(102), NULL);
157 cq_verify(v_server);
158
159 grpc_call_destroy(c);
160 grpc_call_destroy(s);
161
162 end_test(&f);
163 config.tear_down_data(&f);
164
165 cq_verifier_destroy(v_client);
166 cq_verifier_destroy(v_server);
167}
168
169void grpc_end2end_tests(grpc_end2end_test_config config) {
170 test_invoke_request_with_payload(config);
171}