Debian Codegraph @

test/core/end2end/tests/negative_deadline.cc

raw
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 <inttypes.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <grpc/grpc.h>
24 #include <grpc/impl/codegen/propagation_bits.h>
25 #include <grpc/slice.h>
26 #include <grpc/status.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29
30 #include "test/core/end2end/cq_verifier.h"
31 #include "test/core/end2end/end2end_tests.h"
32 #include "test/core/util/test_config.h"
33
34 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
35
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
48 static gpr_timespec n_seconds_from_now(int n) {
49 return grpc_timeout_seconds_to_deadline(n);
50 }
51
52 static gpr_timespec five_seconds_from_now(void) {
53 return n_seconds_from_now(5);
54 }
55
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
63 static void shutdown_server(grpc_end2end_test_fixture* f) {
64 if (!f->server) return;
65 grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
66 grpc_event ev;
67 do {
68 ev = grpc_completion_queue_next(f->cq, grpc_timeout_seconds_to_deadline(5),
69 nullptr);
70 } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
71 grpc_server_destroy(f->server);
72 f->server = nullptr;
73 }
74
75 static void shutdown_client(grpc_end2end_test_fixture* f) {
76 if (!f->client) return;
77 grpc_channel_destroy(f->client);
78 f->client = nullptr;
79 }
80
81 static void end_test(grpc_end2end_test_fixture* f) {
82 shutdown_server(f);
83 shutdown_client(f);
84
85 grpc_completion_queue_shutdown(f->cq);
86 drain_cq(f->cq);
87 grpc_completion_queue_destroy(f->cq);
88 }
89
90 static void simple_request_body(grpc_end2end_test_config /*config*/,
91 grpc_end2end_test_fixture f, size_t num_ops) {
92 grpc_call* c;
93 grpc_core::CqVerifier cqv(f.cq);
94 grpc_op ops[6];
95 grpc_op* op;
96 grpc_metadata_array initial_metadata_recv;
97 grpc_metadata_array trailing_metadata_recv;
98 grpc_status_code status;
99 grpc_call_error error;
100 grpc_slice details;
101
102 gpr_log(GPR_DEBUG, "test with %" PRIuPTR " ops", num_ops);
103
104 gpr_timespec deadline = gpr_inf_past(GPR_CLOCK_REALTIME);
105 c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
106 grpc_slice_from_static_string("/foo"), nullptr,
107 deadline, nullptr);
108 GPR_ASSERT(c);
109
110 grpc_metadata_array_init(&initial_metadata_recv);
111 grpc_metadata_array_init(&trailing_metadata_recv);
112
113 memset(ops, 0, sizeof(ops));
114 op = ops;
115 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
116 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
117 op->data.recv_status_on_client.status = &status;
118 op->data.recv_status_on_client.status_details = &details;
119 op->flags = 0;
120 op->reserved = nullptr;
121 op++;
122 op->op = GRPC_OP_RECV_INITIAL_METADATA;
123 op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
124 op->flags = 0;
125 op->reserved = nullptr;
126 op++;
127 op->op = GRPC_OP_SEND_INITIAL_METADATA;
128 op->data.send_initial_metadata.count = 0;
129 op->flags = 0;
130 op->reserved = nullptr;
131 op++;
132 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
133 op->flags = 0;
134 op->reserved = nullptr;
135 op++;
136 GPR_ASSERT(num_ops <= (size_t)(op - ops));
137 error = grpc_call_start_batch(c, ops, num_ops, tag(1), nullptr);
138 GPR_ASSERT(GRPC_CALL_OK == error);
139
140 cqv.Expect(tag(1), true);
141 cqv.Verify();
142
143 GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED);
144
145 grpc_slice_unref(details);
146 grpc_metadata_array_destroy(&initial_metadata_recv);
147 grpc_metadata_array_destroy(&trailing_metadata_recv);
148
149 grpc_call_unref(c);
150 }
151
152 static void test_invoke_simple_request(grpc_end2end_test_config config,
153 size_t num_ops) {
154 grpc_end2end_test_fixture f;
155
156 f = begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
157 simple_request_body(config, f, num_ops);
158 end_test(&f);
159 config.tear_down_data(&f);
160 }
161
162 void negative_deadline(grpc_end2end_test_config config) {
163 size_t i;
164 for (i = 1; i <= 4; i++) {
165 test_invoke_simple_request(config, i);
166 }
167 }
168
169 void negative_deadline_pre_init(void) {}