end2end/tests/simple_request.cc
Go to the documentation of this file.
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 <stdio.h>
20 #include <string.h>
21 
22 #include <string>
23 
24 #include <grpc/byte_buffer.h>
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29 
34 
35 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
36 
38  const char* test_name,
39  grpc_channel_args* client_args,
40  grpc_channel_args* server_args) {
42  gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
43  f = config.create_fixture(client_args, server_args);
44  config.init_server(&f, server_args);
45  config.init_client(&f, client_args);
46  return f;
47 }
48 
51 }
52 
54  return n_seconds_from_now(5);
55 }
56 
58  grpc_event ev;
59  do {
61  } while (ev.type != GRPC_QUEUE_SHUTDOWN);
62 }
63 
65  if (!f->server) return;
66  grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
67  grpc_event ev;
68  do {
70  nullptr);
71  } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
72  grpc_server_destroy(f->server);
73  f->server = nullptr;
74 }
75 
77  if (!f->client) return;
78  grpc_channel_destroy(f->client);
79  f->client = nullptr;
80 }
81 
85 
87  drain_cq(f->cq);
89 }
90 
91 static void check_peer(char* peer_name) {
92  // If the peer name is a uds path, then check if it is filled
93  if (strncmp(peer_name, "unix:/", strlen("unix:/")) == 0) {
94  GPR_ASSERT(strncmp(peer_name, "unix:/tmp/grpc_fullstack_test.",
95  strlen("unix:/tmp/grpc_fullstack_test.")) == 0);
96  }
97 }
98 
101  grpc_call* c;
102  grpc_call* s;
103  cq_verifier* cqv = cq_verifier_create(f.cq);
104  grpc_op ops[6];
105  grpc_op* op;
111  const char* error_string;
114  int was_cancelled = 2;
115  char* peer;
117  static_cast<grpc_stats_data*>(gpr_malloc(sizeof(grpc_stats_data)));
119  static_cast<grpc_stats_data*>(gpr_malloc(sizeof(grpc_stats_data)));
120 
121 #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
123 #endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */
124 
125  gpr_timespec deadline = five_seconds_from_now();
126  c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
127  grpc_slice_from_static_string("/foo"), nullptr,
128  deadline, nullptr);
129  GPR_ASSERT(c);
130 
131  peer = grpc_call_get_peer(c);
132  GPR_ASSERT(peer != nullptr);
133  gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer);
134  gpr_free(peer);
135 
140 
141  memset(ops, 0, sizeof(ops));
142  op = ops;
145  op->flags = 0;
146  op->reserved = nullptr;
147  op++;
149  op->flags = 0;
150  op->reserved = nullptr;
151  op++;
154  op->flags = 0;
155  op->reserved = nullptr;
156  op++;
161  op->data.recv_status_on_client.error_string = &error_string;
162  op->flags = 0;
163  op->reserved = nullptr;
164  op++;
165  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
166  nullptr);
168 
169  error =
171  &request_metadata_recv, f.cq, f.cq, tag(101));
173  CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
174  cq_verify(cqv);
175 
176  peer = grpc_call_get_peer(s);
177  GPR_ASSERT(peer != nullptr);
178  gpr_log(GPR_DEBUG, "server_peer=%s", peer);
179  check_peer(peer);
180  gpr_free(peer);
181  peer = grpc_call_get_peer(c);
182  GPR_ASSERT(peer != nullptr);
183  gpr_log(GPR_DEBUG, "client_peer=%s", peer);
184  check_peer(peer);
185  gpr_free(peer);
186 
187  memset(ops, 0, sizeof(ops));
188  op = ops;
191  op->flags = 0;
192  op->reserved = nullptr;
193  op++;
197  grpc_slice status_details = grpc_slice_from_static_string("xyz");
198  op->data.send_status_from_server.status_details = &status_details;
199  op->flags = 0;
200  op->reserved = nullptr;
201  op++;
204  op->flags = 0;
205  op->reserved = nullptr;
206  op++;
207  error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
208  nullptr);
210 
211  CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
212  CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
213  cq_verify(cqv);
214 
216  GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
217  // the following sanity check makes sure that the requested error string is
218  // correctly populated by the core. It looks for certain substrings that are
219  // not likely to change much. Some parts of the error, like time created,
220  // obviously are not checked.
221  GPR_ASSERT(nullptr != strstr(error_string, "xyz"));
222  GPR_ASSERT(nullptr != strstr(error_string, "Error received from peer"));
223  GPR_ASSERT(nullptr != strstr(error_string, "grpc_message"));
224  GPR_ASSERT(nullptr != strstr(error_string, "grpc_status"));
228 
230  gpr_free(const_cast<char*>(error_string));
235 
237  grpc_call_unref(s);
238 
239  cq_verifier_destroy(cqv);
240 
241 #if defined(GRPC_COLLECT_STATS) || !defined(NDEBUG)
242  int expected_calls = 1;
243  if (config.feature_mask & FEATURE_MASK_SUPPORTS_REQUEST_PROXYING) {
244  expected_calls *= 2;
245  }
246 
248 
250 
253  expected_calls);
256  expected_calls);
257 #else
258  (void)config;
259 #endif /* defined(GRPC_COLLECT_STATS) || !defined(NDEBUG) */
260  gpr_free(before);
261  gpr_free(after);
262 }
263 
266 
267  f = begin_test(config, "test_invoke_simple_request", nullptr, nullptr);
269  end_test(&f);
270  config.tear_down_data(&f);
271 }
272 
274  int i;
276  begin_test(config, "test_invoke_10_simple_requests", nullptr, nullptr);
277  for (i = 0; i < 10; i++) {
279  gpr_log(GPR_INFO, "Running test: Passed simple request %d", i);
280  }
281  end_test(&f);
282  config.tear_down_data(&f);
283 }
284 
286  int i;
287  for (i = 0; i < 10; i++) {
289  }
291 }
292 
test_invoke_10_simple_requests
static void test_invoke_10_simple_requests(grpc_end2end_test_config config)
Definition: end2end/tests/simple_request.cc:273
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_op::flags
uint32_t flags
Definition: grpc_types.h:644
grpc_call_error
grpc_call_error
Definition: grpc_types.h:464
grpc_call_details_destroy
GRPCAPI void grpc_call_details_destroy(grpc_call_details *details)
Definition: call_details.cc:36
grpc_call_details_init
GRPCAPI void grpc_call_details_init(grpc_call_details *details)
Definition: call_details.cc:30
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::trailing_metadata
grpc_metadata_array * trailing_metadata
Definition: grpc_types.h:701
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
log.h
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::status
grpc_status_code * status
Definition: grpc_types.h:702
test_invoke_simple_request
static void test_invoke_simple_request(grpc_end2end_test_config config)
Definition: end2end/tests/simple_request.cc:264
memset
return memset(p, 0, total)
grpc_call_details::flags
uint32_t flags
Definition: grpc_types.h:589
grpc_op::grpc_op_data::send_initial_metadata
struct grpc_op::grpc_op_data::grpc_op_send_initial_metadata send_initial_metadata
simple_request_pre_init
void simple_request_pre_init(void)
Definition: end2end/tests/simple_request.cc:293
grpc_call_get_peer
GRPCAPI char * grpc_call_get_peer(grpc_call *call)
Definition: call.cc:1774
grpc_metadata_array
Definition: grpc_types.h:579
grpc_call_details
Definition: grpc_types.h:585
grpc_op::reserved
void * reserved
Definition: grpc_types.h:646
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_stats_data_as_json
std::string grpc_stats_data_as_json(const grpc_stats_data *data)
Definition: stats.cc:148
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
GRPC_QUEUE_SHUTDOWN
@ GRPC_QUEUE_SHUTDOWN
Definition: grpc_types.h:554
GRPC_OP_COMPLETE
@ GRPC_OP_COMPLETE
Definition: grpc_types.h:558
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
GRPC_CALL_OK
@ GRPC_CALL_OK
Definition: grpc_types.h:466
status
absl::Status status
Definition: rls.cc:251
time.h
grpc_call_details::method
grpc_slice method
Definition: grpc_types.h:586
grpc_end2end_test_config
Definition: end2end_tests.h:53
grpc_channel_args
Definition: grpc_types.h:132
stats.h
GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED
@ GRPC_STATS_COUNTER_CLIENT_CALLS_CREATED
Definition: stats_data.h:27
grpc_op::data
union grpc_op::grpc_op_data data
grpc_end2end_test_fixture
Definition: end2end_tests.h:46
before
IntBeforeRegisterTypedTestSuiteP before
Definition: googletest/googletest/test/gtest-typed-test_test.cc:376
grpc_metadata_array_destroy
GRPCAPI void grpc_metadata_array_destroy(grpc_metadata_array *array)
Definition: metadata_array.cc:35
shutdown_server
static void shutdown_server(grpc_end2end_test_fixture *f)
Definition: end2end/tests/simple_request.cc:64
grpc_server_request_call
GRPCAPI grpc_call_error grpc_server_request_call(grpc_server *server, grpc_call **call, grpc_call_details *details, grpc_metadata_array *request_metadata, grpc_completion_queue *cq_bound_to_call, grpc_completion_queue *cq_for_notification, void *tag_new)
Definition: src/core/lib/surface/server.cc:1526
trailing_metadata_recv
static grpc_metadata_array trailing_metadata_recv
Definition: test/core/fling/client.cc:43
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
tag
static void * tag(intptr_t t)
Definition: end2end/tests/simple_request.cc:35
GRPC_OP_RECV_INITIAL_METADATA
@ GRPC_OP_RECV_INITIAL_METADATA
Definition: grpc_types.h:617
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
end_test
static void end_test(grpc_end2end_test_fixture *f)
Definition: end2end/tests/simple_request.cc:82
GRPC_STATS_COUNTER_SERVER_CALLS_CREATED
@ GRPC_STATS_COUNTER_SERVER_CALLS_CREATED
Definition: stats_data.h:28
simple_request_body
static void simple_request_body(grpc_end2end_test_config config, grpc_end2end_test_fixture f)
Definition: end2end/tests/simple_request.cc:99
drain_cq
static void drain_cq(grpc_completion_queue *cq)
Definition: end2end/tests/simple_request.cc:57
GRPC_OP_SEND_STATUS_FROM_SERVER
@ GRPC_OP_SEND_STATUS_FROM_SERVER
Definition: grpc_types.h:612
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
FEATURE_MASK_SUPPORTS_REQUEST_PROXYING
#define FEATURE_MASK_SUPPORTS_REQUEST_PROXYING
Definition: end2end_tests.h:36
grpc_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Definition: call.cc:1770
grpc_op::grpc_op_data::grpc_op_send_status_from_server::status
grpc_status_code status
Definition: grpc_types.h:673
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::error_string
const char ** error_string
Definition: grpc_types.h:707
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_event
Definition: grpc_types.h:564
grpc_completion_queue
Definition: completion_queue.cc:347
cq_verifier_destroy
void cq_verifier_destroy(cq_verifier *v)
Definition: cq_verifier.cc:92
grpc.h
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc_op
Definition: grpc_types.h:640
shutdown_client
static void shutdown_client(grpc_end2end_test_fixture *f)
Definition: end2end/tests/simple_request.cc:76
grpc_slice_from_static_string
GPRAPI grpc_slice grpc_slice_from_static_string(const char *source)
Definition: slice/slice.cc:89
cq_verifier_create
cq_verifier * cq_verifier_create(grpc_completion_queue *cq)
Definition: cq_verifier.cc:86
was_cancelled
static int was_cancelled
Definition: test/core/fling/server.cc:58
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
cq_verifier
Definition: cq_verifier.cc:76
request_metadata_recv
static grpc_metadata_array request_metadata_recv
Definition: test/core/fling/server.cc:48
grpc_server_destroy
GRPCAPI void grpc_server_destroy(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1519
grpc_stats_collect
void grpc_stats_collect(grpc_stats_data *output)
Definition: stats.cc:48
CQ_EXPECT_COMPLETION
#define CQ_EXPECT_COMPLETION(v, tag, success)
Definition: cq_verifier.h:58
end2end_tests.h
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
after
IntAfterTypedTestSuiteP after
Definition: googletest/googletest/test/gtest-typed-test_test.cc:375
grpc_op::op
grpc_op_type op
Definition: grpc_types.h:642
grpc_op::grpc_op_data::grpc_op_send_initial_metadata::count
size_t count
Definition: grpc_types.h:653
grpc_op::grpc_op_data::grpc_op_recv_status_on_client::status_details
grpc_slice * status_details
Definition: grpc_types.h:703
details
static grpc_slice details
Definition: test/core/fling/client.cc:46
grpc_channel_create_call
GRPCAPI grpc_call * grpc_channel_create_call(grpc_channel *channel, grpc_call *parent_call, uint32_t propagation_mask, grpc_completion_queue *completion_queue, grpc_slice method, const grpc_slice *host, gpr_timespec deadline, void *reserved)
Definition: channel.cc:311
grpc_op::grpc_op_data::recv_close_on_server
struct grpc_op::grpc_op_data::grpc_op_recv_close_on_server recv_close_on_server
GRPC_PROPAGATE_DEFAULTS
#define GRPC_PROPAGATE_DEFAULTS
Definition: propagation_bits.h:45
grpc_op::grpc_op_data::send_status_from_server
struct grpc_op::grpc_op_data::grpc_op_send_status_from_server send_status_from_server
cq_verifier.h
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Definition: completion_queue.cc:1424
GRPC_OP_SEND_INITIAL_METADATA
@ GRPC_OP_SEND_INITIAL_METADATA
Definition: grpc_types.h:598
check_peer
static void check_peer(char *peer_name)
Definition: end2end/tests/simple_request.cc:91
alloc.h
grpc_stats_data
Definition: src/core/lib/debug/stats.h:33
grpc_op::grpc_op_data::recv_status_on_client
struct grpc_op::grpc_op_data::grpc_op_recv_status_on_client recv_status_on_client
grpc_op::grpc_op_data::grpc_op_send_status_from_server::trailing_metadata_count
size_t trailing_metadata_count
Definition: grpc_types.h:671
grpc_server_shutdown_and_notify
GRPCAPI void grpc_server_shutdown_and_notify(grpc_server *server, grpc_completion_queue *cq, void *tag)
Definition: src/core/lib/surface/server.cc:1503
grpc_completion_queue_next
GRPCAPI grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1133
cq_verify
void cq_verify(cq_verifier *v, int timeout_sec)
Definition: cq_verifier.cc:268
grpc_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Definition: completion_queue.cc:1416
grpc_channel_destroy
GRPCAPI void grpc_channel_destroy(grpc_channel *channel)
Definition: channel.cc:437
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
GRPC_OP_RECV_CLOSE_ON_SERVER
@ GRPC_OP_RECV_CLOSE_ON_SERVER
Definition: grpc_types.h:633
GRPC_STATUS_UNIMPLEMENTED
@ GRPC_STATUS_UNIMPLEMENTED
Definition: include/grpc/impl/codegen/status.h:124
simple_request
void simple_request(grpc_end2end_test_config config)
Definition: end2end/tests/simple_request.cc:285
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
five_seconds_from_now
static gpr_timespec five_seconds_from_now(void)
Definition: end2end/tests/simple_request.cc:53
n_seconds_from_now
static gpr_timespec n_seconds_from_now(int n)
Definition: end2end/tests/simple_request.cc:49
grpc_op::grpc_op_data::recv_initial_metadata
struct grpc_op::grpc_op_data::grpc_op_recv_initial_metadata recv_initial_metadata
grpc_op::grpc_op_data::grpc_op_send_status_from_server::status_details
grpc_slice * status_details
Definition: grpc_types.h:677
gpr_timespec
Definition: gpr_types.h:50
grpc_event::type
grpc_completion_type type
Definition: grpc_types.h:566
GRPC_OP_RECV_STATUS_ON_CLIENT
@ GRPC_OP_RECV_STATUS_ON_CLIENT
Definition: grpc_types.h:627
grpc_op::grpc_op_data::grpc_op_recv_initial_metadata::recv_initial_metadata
grpc_metadata_array * recv_initial_metadata
Definition: grpc_types.h:685
grpc_slice_str_cmp
GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b)
Definition: slice/slice.cc:426
op
static grpc_op * op
Definition: test/core/fling/client.cc:47
ops
static grpc_op ops[6]
Definition: test/core/fling/client.cc:39
initial_metadata_recv
static grpc_metadata_array initial_metadata_recv
Definition: test/core/fling/client.cc:42
grpc_call_start_batch
GRPCAPI grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, size_t nops, void *tag, void *reserved)
Definition: call.cc:1831
grpc_event::tag
void * tag
Definition: grpc_types.h:576
grpc_op::grpc_op_data::grpc_op_recv_close_on_server::cancelled
int * cancelled
Definition: grpc_types.h:714
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
GRPC_OP_SEND_CLOSE_FROM_CLIENT
@ GRPC_OP_SEND_CLOSE_FROM_CLIENT
Definition: grpc_types.h:607
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
call_details
static grpc_call_details call_details
Definition: test/core/fling/server.cc:47
grpc_metadata_array_init
GRPCAPI void grpc_metadata_array_init(grpc_metadata_array *array)
Definition: metadata_array.cc:30
begin_test
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, const char *test_name, grpc_channel_args *client_args, grpc_channel_args *server_args)
Definition: end2end/tests/simple_request.cc:37


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13