Make a copy of ByteBuffer when writing

pull/5892/head
yang-g 9 years ago
parent fb6e13b1b5
commit f64befd27f
  1. 4
      include/grpc++/support/byte_buffer.h
  2. 5
      test/cpp/end2end/generic_end2end_test.cc
  3. 20
      test/cpp/util/byte_buffer_test.cc

@ -99,8 +99,8 @@ class SerializationTraits<ByteBuffer, void> {
}
static Status Serialize(const ByteBuffer& source, grpc_byte_buffer** buffer,
bool* own_buffer) {
*buffer = source.buffer();
*own_buffer = false;
*buffer = grpc_byte_buffer_copy(source.buffer());
*own_buffer = true;
return Status::OK;
}
};

@ -135,6 +135,8 @@ class GenericEnd2endTest : public ::testing::Test {
std::unique_ptr<ByteBuffer> send_buffer =
SerializeToByteBuffer(&send_request);
call->Write(*send_buffer, tag(2));
// Send ByteBuffer can be destroyed after calling Write.
send_buffer.reset();
client_ok(2);
call->WritesDone(tag(3));
client_ok(3);
@ -154,6 +156,7 @@ class GenericEnd2endTest : public ::testing::Test {
send_response.set_message(recv_request.message());
send_buffer = SerializeToByteBuffer(&send_response);
stream.Write(*send_buffer, tag(6));
send_buffer.reset();
server_ok(6);
stream.Finish(Status::OK, tag(7));
@ -223,6 +226,7 @@ TEST_F(GenericEnd2endTest, SimpleBidiStreaming) {
std::unique_ptr<ByteBuffer> send_buffer =
SerializeToByteBuffer(&send_request);
cli_stream->Write(*send_buffer, tag(3));
send_buffer.reset();
client_ok(3);
ByteBuffer recv_buffer;
@ -234,6 +238,7 @@ TEST_F(GenericEnd2endTest, SimpleBidiStreaming) {
send_response.set_message(recv_request.message());
send_buffer = SerializeToByteBuffer(&send_response);
srv_stream.Write(*send_buffer, tag(5));
send_buffer.reset();
server_ok(5);
cli_stream->Read(&recv_buffer, tag(6));

@ -1,6 +1,6 @@
/*
*
* Copyright 2015, Google Inc.
* Copyright 2015-2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -105,6 +105,24 @@ TEST_F(ByteBufferTest, Dump) {
EXPECT_TRUE(SliceEqual(slices[1], world));
}
TEST_F(ByteBufferTest, SerializationMakesCopy) {
gpr_slice hello = gpr_slice_from_copied_string(kContent1);
gpr_slice world = gpr_slice_from_copied_string(kContent2);
std::vector<Slice> slices;
slices.push_back(Slice(hello, Slice::STEAL_REF));
slices.push_back(Slice(world, Slice::STEAL_REF));
grpc_byte_buffer* send_buffer = nullptr;
bool owned = false;
ByteBuffer buffer(&slices[0], 2);
slices.clear();
auto status = SerializationTraits<ByteBuffer, void>::Serialize(
buffer, &send_buffer, &owned);
EXPECT_TRUE(status.ok());
EXPECT_TRUE(owned);
EXPECT_TRUE(send_buffer != nullptr);
grpc_byte_buffer_destroy(send_buffer);
}
} // namespace
} // namespace grpc

Loading…
Cancel
Save