write_buffering.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2017 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 <grpc/byte_buffer.h>
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
26 
29 
30 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
31 
33  const char* test_name,
34  grpc_channel_args* client_args,
35  grpc_channel_args* server_args) {
37  gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
38  f = config.create_fixture(client_args, server_args);
39  config.init_server(&f, server_args);
40  config.init_client(&f, client_args);
41  return f;
42 }
43 
46 }
47 
49  return n_seconds_from_now(5);
50 }
51 
53  grpc_event ev;
54  do {
56  } while (ev.type != GRPC_QUEUE_SHUTDOWN);
57 }
58 
60  if (!f->server) return;
61  grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
62  grpc_event ev;
63  do {
65  nullptr);
66  } while (ev.type != GRPC_OP_COMPLETE || ev.tag != tag(1000));
67  grpc_server_destroy(f->server);
68  f->server = nullptr;
69 }
70 
72  if (!f->client) return;
73  grpc_channel_destroy(f->client);
74  f->client = nullptr;
75 }
76 
80 
82  drain_cq(f->cq);
84 }
85 
86 /* Client sends a request with payload, server reads then returns status. */
88  grpc_call* c;
89  grpc_call* s;
90  grpc_slice request_payload_slice1 =
91  grpc_slice_from_copied_string("hello world");
92  grpc_byte_buffer* request_payload1 =
93  grpc_raw_byte_buffer_create(&request_payload_slice1, 1);
94  grpc_slice request_payload_slice2 = grpc_slice_from_copied_string("abc123");
95  grpc_byte_buffer* request_payload2 =
96  grpc_raw_byte_buffer_create(&request_payload_slice2, 1);
98  begin_test(config, "test_invoke_request_with_payload", nullptr, nullptr);
99  cq_verifier* cqv = cq_verifier_create(f.cq);
100  grpc_op ops[6];
101  grpc_op* op;
105  grpc_byte_buffer* request_payload_recv1 = nullptr;
106  grpc_byte_buffer* request_payload_recv2 = nullptr;
111  int was_cancelled = 2;
112 
113  gpr_timespec deadline = five_seconds_from_now();
114  c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
115  grpc_slice_from_static_string("/foo"), nullptr,
116  deadline, nullptr);
117  GPR_ASSERT(c);
118 
123 
124  memset(ops, 0, sizeof(ops));
125  op = ops;
128  op++;
129  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
130  nullptr);
132 
133  memset(ops, 0, sizeof(ops));
134  op = ops;
137  op->flags = 0;
138  op->reserved = nullptr;
139  op++;
140  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(2),
141  nullptr);
143 
145  f.server, &s, &call_details,
146  &request_metadata_recv, f.cq, f.cq, tag(101)));
147  CQ_EXPECT_COMPLETION(cqv, tag(1), true); /* send message is buffered */
148  CQ_EXPECT_COMPLETION(cqv, tag(101), true);
149  cq_verify(cqv);
150 
151  memset(ops, 0, sizeof(ops));
152  op = ops;
154  op->data.send_message.send_message = request_payload1;
156  op++;
157  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
158  nullptr);
160 
161  memset(ops, 0, sizeof(ops));
162  op = ops;
165  op++;
166  error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
167  nullptr);
169 
170  /* recv message should not succeed yet - it's buffered at the client still */
171  memset(ops, 0, sizeof(ops));
172  op = ops;
174  op->data.recv_message.recv_message = &request_payload_recv1;
175  op++;
176  error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(103),
177  nullptr);
179 
180  CQ_EXPECT_COMPLETION(cqv, tag(2), true);
181  CQ_EXPECT_COMPLETION(cqv, tag(3), true);
182  CQ_EXPECT_COMPLETION(cqv, tag(102), true);
183  cq_verify(cqv);
184 
185  /* send another message, this time not buffered */
186  memset(ops, 0, sizeof(ops));
187  op = ops;
189  op->data.send_message.send_message = request_payload2;
190  op->flags = 0;
191  op++;
192  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(4),
193  nullptr);
195 
196  /* now the first send should match up with the first recv */
197  CQ_EXPECT_COMPLETION(cqv, tag(103), true);
198  CQ_EXPECT_COMPLETION(cqv, tag(4), true);
199  cq_verify(cqv);
200 
201  /* and the next recv should be ready immediately also */
202  memset(ops, 0, sizeof(ops));
203  op = ops;
205  op->data.recv_message.recv_message = &request_payload_recv2;
206  op++;
207  error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
208  nullptr);
210 
211  CQ_EXPECT_COMPLETION(cqv, tag(104), true);
212  cq_verify(cqv);
213 
214  memset(ops, 0, sizeof(ops));
215  op = ops;
217  op->flags = 0;
218  op->reserved = nullptr;
219  op++;
224  op->flags = 0;
225  op->reserved = nullptr;
226  op++;
227  error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(4),
228  nullptr);
229 
230  memset(ops, 0, sizeof(ops));
231  op = ops;
234  op->flags = 0;
235  op->reserved = nullptr;
236  op++;
240  grpc_slice status_details = grpc_slice_from_static_string("xyz");
241  op->data.send_status_from_server.status_details = &status_details;
242  op->flags = 0;
243  op->reserved = nullptr;
244  op++;
245  error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(105),
246  nullptr);
248 
249  CQ_EXPECT_COMPLETION(cqv, tag(105), 1);
250  CQ_EXPECT_COMPLETION(cqv, tag(4), 1);
251  cq_verify(cqv);
252 
254  GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
257  GPR_ASSERT(byte_buffer_eq_string(request_payload_recv1, "hello world"));
258  GPR_ASSERT(byte_buffer_eq_string(request_payload_recv2, "abc123"));
259 
265 
267  grpc_call_unref(s);
268 
269  cq_verifier_destroy(cqv);
270 
271  grpc_byte_buffer_destroy(request_payload1);
272  grpc_byte_buffer_destroy(request_payload_recv1);
273  grpc_byte_buffer_destroy(request_payload2);
274  grpc_byte_buffer_destroy(request_payload_recv2);
275 
276  end_test(&f);
277  config.tear_down_data(&f);
278 }
279 
282 }
283 
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::grpc_op_data::grpc_op_send_message::send_message
struct grpc_byte_buffer * send_message
Definition: grpc_types.h:668
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
grpc_raw_byte_buffer_create
GRPCAPI grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slices, size_t nslices)
Definition: byte_buffer.cc:34
memset
return memset(p, 0, total)
grpc_slice_from_copied_string
GPRAPI grpc_slice grpc_slice_from_copied_string(const char *source)
Definition: slice/slice.cc:177
grpc_op::grpc_op_data::send_initial_metadata
struct grpc_op::grpc_op_data::grpc_op_send_initial_metadata send_initial_metadata
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
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: write_buffering.cc:32
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
shutdown_server
static void shutdown_server(grpc_end2end_test_fixture *f)
Definition: write_buffering.cc:59
GRPC_CALL_OK
@ GRPC_CALL_OK
Definition: grpc_types.h:466
status
absl::Status status
Definition: rls.cc:251
write_buffering_pre_init
void write_buffering_pre_init(void)
Definition: write_buffering.cc:284
time.h
grpc_call_details::method
grpc_slice method
Definition: grpc_types.h:586
grpc_end2end_test_config
Definition: end2end_tests.h:53
byte_buffer_eq_string
int byte_buffer_eq_string(grpc_byte_buffer *bb, const char *str)
Definition: cq_verifier.cc:186
five_seconds_from_now
static gpr_timespec five_seconds_from_now(void)
Definition: write_buffering.cc:48
grpc_channel_args
Definition: grpc_types.h:132
grpc_op::grpc_op_data::recv_message
struct grpc_op::grpc_op_data::grpc_op_recv_message recv_message
grpc_op::data
union grpc_op::grpc_op_data data
grpc_end2end_test_fixture
Definition: end2end_tests.h:46
grpc_metadata_array_destroy
GRPCAPI void grpc_metadata_array_destroy(grpc_metadata_array *array)
Definition: metadata_array.cc:35
grpc_op::grpc_op_data::grpc_op_recv_message::recv_message
struct grpc_byte_buffer ** recv_message
Definition: grpc_types.h:693
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
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
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
n_seconds_from_now
static gpr_timespec n_seconds_from_now(int n)
Definition: write_buffering.cc:44
GRPC_OP_SEND_STATUS_FROM_SERVER
@ GRPC_OP_SEND_STATUS_FROM_SERVER
Definition: grpc_types.h:612
write_buffering
void write_buffering(grpc_end2end_test_config config)
Definition: write_buffering.cc:280
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
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_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
grpc_byte_buffer
Definition: grpc_types.h:43
GRPC_WRITE_BUFFER_HINT
#define GRPC_WRITE_BUFFER_HINT
Definition: grpc_types.h:510
grpc_op
Definition: grpc_types.h:640
GRPC_OP_SEND_MESSAGE
@ GRPC_OP_SEND_MESSAGE
Definition: grpc_types.h:602
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_empty_slice
GPRAPI grpc_slice grpc_empty_slice(void)
Definition: slice/slice.cc:42
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
end_test
static void end_test(grpc_end2end_test_fixture *f)
Definition: write_buffering.cc:77
tag
static void * tag(intptr_t t)
Definition: write_buffering.cc:30
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
drain_cq
static void drain_cq(grpc_completion_queue *cq)
Definition: write_buffering.cc:52
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
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
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_OP_RECV_MESSAGE
@ GRPC_OP_RECV_MESSAGE
Definition: grpc_types.h:621
test_invoke_request_with_payload
static void test_invoke_request_with_payload(grpc_end2end_test_config config)
Definition: write_buffering.cc:87
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
grpc_op::grpc_op_data::send_message
struct grpc_op::grpc_op_data::grpc_op_send_message send_message
alloc.h
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_byte_buffer_destroy
GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)
Definition: byte_buffer.cc:81
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
shutdown_client
static void shutdown_client(grpc_end2end_test_fixture *f)
Definition: write_buffering.cc:71
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_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
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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:53