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 <inttypes.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <grpc/byte_buffer.h>
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/string_util.h>
30 #include <grpc/support/time.h>
31 #include "src/core/lib/gpr/string.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,size_t num_ops,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 size_t num_ops,
39 grpc_channel_args* client_args,
40 grpc_channel_args* server_args) {
41 grpc_end2end_test_fixture f;
42 gpr_log(GPR_INFO, "Running test: %s/%s [%" PRIdPTR " ops]", test_name,
43 config.name, num_ops);
44 f = config.create_fixture(client_args, server_args);
45 config.init_server(&f, server_args);
46 config.init_client(&f, client_args);
47 return f;
48 }
49
n_seconds_from_now(int n)50 static gpr_timespec n_seconds_from_now(int n) {
51 return grpc_timeout_seconds_to_deadline(n);
52 }
53
five_seconds_from_now(void)54 static gpr_timespec five_seconds_from_now(void) {
55 return n_seconds_from_now(5);
56 }
57
drain_cq(grpc_completion_queue * cq)58 static void drain_cq(grpc_completion_queue* cq) {
59 grpc_event ev;
60 do {
61 ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
62 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
63 }
64
shutdown_server(grpc_end2end_test_fixture * f)65 static void shutdown_server(grpc_end2end_test_fixture* f) {
66 if (!f->server) return;
67 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
68 grpc_event ev = grpc_completion_queue_next(
69 f->cq, grpc_timeout_seconds_to_deadline(5), nullptr);
70 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
71 GPR_ASSERT(ev.tag == tag(1000));
72 grpc_server_destroy(f->server);
73 f->server = nullptr;
74 }
75
shutdown_client(grpc_end2end_test_fixture * f)76 static void shutdown_client(grpc_end2end_test_fixture* f) {
77 if (!f->client) return;
78 grpc_channel_destroy(f->client);
79 f->client = nullptr;
80 }
81
end_test(grpc_end2end_test_fixture * f)82 static void end_test(grpc_end2end_test_fixture* f) {
83 shutdown_server(f);
84 shutdown_client(f);
85
86 grpc_completion_queue_shutdown(f->cq);
87 drain_cq(f->cq);
88 grpc_completion_queue_destroy(f->cq);
89 grpc_completion_queue_destroy(f->shutdown_cq);
90 }
91
simple_request_body(grpc_end2end_test_config config,grpc_end2end_test_fixture f,size_t num_ops)92 static void simple_request_body(grpc_end2end_test_config config,
93 grpc_end2end_test_fixture f, size_t num_ops) {
94 grpc_call* c;
95 cq_verifier* cqv = cq_verifier_create(f.cq);
96 grpc_op ops[6];
97 grpc_op* op;
98 grpc_metadata_array initial_metadata_recv;
99 grpc_metadata_array trailing_metadata_recv;
100 grpc_status_code status;
101 grpc_call_error error;
102 grpc_slice details;
103
104 gpr_log(GPR_DEBUG, "test with %" PRIuPTR " ops", num_ops);
105
106 gpr_timespec deadline = five_seconds_from_now();
107 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
108 grpc_slice_from_static_string("/foo"), nullptr,
109 deadline, nullptr);
110 GPR_ASSERT(c);
111
112 grpc_metadata_array_init(&initial_metadata_recv);
113 grpc_metadata_array_init(&trailing_metadata_recv);
114
115 memset(ops, 0, sizeof(ops));
116 op = ops;
117 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
118 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
119 op->data.recv_status_on_client.status = &status;
120 op->data.recv_status_on_client.status_details = &details;
121 op->flags = 0;
122 op->reserved = nullptr;
123 op++;
124 op->op = GRPC_OP_RECV_INITIAL_METADATA;
125 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
126 op->flags = 0;
127 op->reserved = nullptr;
128 op++;
129 op->op = GRPC_OP_SEND_INITIAL_METADATA;
130 op->data.send_initial_metadata.count = 0;
131 op->flags = 0;
132 op->reserved = nullptr;
133 op++;
134 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
135 op->flags = 0;
136 op->reserved = nullptr;
137 op++;
138 GPR_ASSERT(num_ops <= (size_t)(op - ops));
139 error = grpc_call_start_batch(c, ops, num_ops, tag(1), nullptr);
140 GPR_ASSERT(GRPC_CALL_OK == error);
141
142 char* dynamic_string = gpr_strdup("xyz");
143 grpc_call_cancel_with_status(c, GRPC_STATUS_UNIMPLEMENTED,
144 (const char*)dynamic_string, nullptr);
145 // The API of \a description allows for it to be a dynamic/non-const
146 // string, test this guarantee.
147 gpr_free(dynamic_string);
148
149 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
150 cq_verify(cqv);
151
152 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
153 GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
154
155 grpc_slice_unref(details);
156 grpc_metadata_array_destroy(&initial_metadata_recv);
157 grpc_metadata_array_destroy(&trailing_metadata_recv);
158
159 grpc_call_unref(c);
160
161 cq_verifier_destroy(cqv);
162 }
163
test_invoke_simple_request(grpc_end2end_test_config config,size_t num_ops)164 static void test_invoke_simple_request(grpc_end2end_test_config config,
165 size_t num_ops) {
166 grpc_end2end_test_fixture f;
167
168 f = begin_test(config, "test_invoke_simple_request", num_ops, nullptr,
169 nullptr);
170 simple_request_body(config, f, num_ops);
171 end_test(&f);
172 config.tear_down_data(&f);
173 }
174
cancel_with_status(grpc_end2end_test_config config)175 void cancel_with_status(grpc_end2end_test_config config) {
176 size_t i;
177 for (i = 1; i <= 4; i++) {
178 test_invoke_simple_request(config, i);
179 }
180 }
181
cancel_with_status_pre_init(void)182 void cancel_with_status_pre_init(void) {}
183