From 589c940195f82738a95ff1f78a4f4d6a43566a41 Mon Sep 17 00:00:00 2001 From: ncteisen Date: Tue, 14 Nov 2017 18:22:54 -0800 Subject: [PATCH] add test --- src/core/lib/iomgr/tcp_posix.cc | 4 +-- src/core/lib/slice/slice_buffer.cc | 9 ++++--- src/core/lib/slice/slice_internal.h | 6 ++--- test/core/slice/slice_buffer_test.cc | 39 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/core/lib/iomgr/tcp_posix.cc b/src/core/lib/iomgr/tcp_posix.cc index 016ca87219b..0864cd594d2 100644 --- a/src/core/lib/iomgr/tcp_posix.cc +++ b/src/core/lib/iomgr/tcp_posix.cc @@ -576,8 +576,8 @@ static bool tcp_flush(grpc_exec_ctx* exec_ctx, grpc_tcp* tcp, if (errno == EAGAIN) { tcp->outgoing_slice_idx = unwind_slice_idx; tcp->outgoing_byte_idx = unwind_byte_idx; - grpc_slice_buffer_partial_reset_and_unref_internal( - exec_ctx, tcp->outgoing_buffer, unwind_slice_idx); + grpc_slice_buffer_partial_unref_internal(exec_ctx, tcp->outgoing_buffer, + unwind_slice_idx); return false; } else if (errno == EPIPE) { *error = grpc_error_set_int(GRPC_OS_ERROR(errno, "sendmsg"), diff --git a/src/core/lib/slice/slice_buffer.cc b/src/core/lib/slice/slice_buffer.cc index cd5040fbcb5..8fdd8cf2257 100644 --- a/src/core/lib/slice/slice_buffer.cc +++ b/src/core/lib/slice/slice_buffer.cc @@ -176,10 +176,11 @@ void grpc_slice_buffer_reset_and_unref_internal(grpc_exec_ctx* exec_ctx, sb->idx_of_first_valid_slice = 0; } -void grpc_slice_buffer_partial_reset_and_unref_internal(grpc_exec_ctx* exec_ctx, - grpc_slice_buffer* sb, - size_t idx) { - GPR_ASSERT(idx <= sb->count); +void grpc_slice_buffer_partial_unref_internal(grpc_exec_ctx* exec_ctx, + grpc_slice_buffer* sb, + size_t idx) { + GPR_ASSERT(idx < sb->count); // if idx == count, then partial is not needed + GPR_ASSERT(sb->idx_of_first_valid_slice <= idx); size_t i; for (i = sb->idx_of_first_valid_slice; i < idx; i++) { diff --git a/src/core/lib/slice/slice_internal.h b/src/core/lib/slice/slice_internal.h index 2c616950d7f..10527dcdeb5 100644 --- a/src/core/lib/slice/slice_internal.h +++ b/src/core/lib/slice/slice_internal.h @@ -32,9 +32,9 @@ grpc_slice grpc_slice_ref_internal(grpc_slice slice); void grpc_slice_unref_internal(grpc_exec_ctx* exec_ctx, grpc_slice slice); void grpc_slice_buffer_reset_and_unref_internal(grpc_exec_ctx* exec_ctx, grpc_slice_buffer* sb); -void grpc_slice_buffer_partial_reset_and_unref_internal(grpc_exec_ctx* exec_ctx, - grpc_slice_buffer* sb, - size_t idx); +void grpc_slice_buffer_partial_unref_internal(grpc_exec_ctx* exec_ctx, + grpc_slice_buffer* sb, + size_t idx); void grpc_slice_buffer_destroy_internal(grpc_exec_ctx* exec_ctx, grpc_slice_buffer* sb); diff --git a/test/core/slice/slice_buffer_test.cc b/test/core/slice/slice_buffer_test.cc index 338e8079dc8..696251536f5 100644 --- a/test/core/slice/slice_buffer_test.cc +++ b/test/core/slice/slice_buffer_test.cc @@ -18,6 +18,7 @@ #include #include +#include "src/core/lib/slice/slice_internal.h" #include "test/core/util/test_config.h" void test_slice_buffer_add() { @@ -104,11 +105,49 @@ void test_slice_buffer_move_first() { GPR_ASSERT(dst.length == dst_len); } +static void populate_slice_buffer(grpc_slice_buffer* sb) { + grpc_slice slices[4]; + int idx = 0; + + slices[0] = grpc_slice_from_copied_string("aaa"); + slices[1] = grpc_slice_from_copied_string("bbbb"); + slices[2] = grpc_slice_from_copied_string("ccc"); + slices[3] = grpc_slice_from_copied_string("ddddd"); + + for (idx = 0; idx < 4; idx++) { + grpc_slice_ref(slices[idx]); + grpc_slice_buffer_add_indexed(sb, slices[idx]); + } +} + +void test_slice_buffer_unref() { + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + grpc_slice_buffer sb; + + grpc_slice_buffer_init(&sb); + + // regular init and unref + populate_slice_buffer(&sb); + grpc_slice_buffer_reset_and_unref_internal(&exec_ctx, &sb); + + // inits, then unrefs partial, then unrefs the rest + populate_slice_buffer(&sb); + grpc_slice_buffer_partial_unref_internal(&exec_ctx, &sb, 1); + grpc_slice_buffer_reset_and_unref_internal(&exec_ctx, &sb); + + // two partial unrefs + populate_slice_buffer(&sb); + grpc_slice_buffer_partial_unref_internal(&exec_ctx, &sb, 1); + grpc_slice_buffer_partial_unref_internal(&exec_ctx, &sb, 3); + grpc_slice_buffer_reset_and_unref_internal(&exec_ctx, &sb); +} + int main(int argc, char** argv) { grpc_test_init(argc, argv); test_slice_buffer_add(); test_slice_buffer_move_first(); + test_slice_buffer_unref(); return 0; }