123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- /*
- *
- * Copyright 2017 gRPC authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- #include "src/core/lib/transport/byte_stream.h"
- #include <gtest/gtest.h>
- #include <grpc/grpc.h>
- #include <grpc/support/alloc.h>
- #include <grpc/support/log.h>
- #include "src/core/lib/gpr/useful.h"
- #include "src/core/lib/iomgr/exec_ctx.h"
- #include "src/core/lib/slice/slice_internal.h"
- #include "test/core/util/test_config.h"
- namespace grpc_core {
- namespace {
- //
- // SliceBufferByteStream tests
- //
- void NotCalledClosure(void* /*arg*/, grpc_error_handle /*error*/) {
- GPR_ASSERT(false);
- }
- TEST(SliceBufferByteStream, Basic) {
- ExecCtx exec_ctx;
- // Create and populate slice buffer.
- grpc_slice_buffer buffer;
- grpc_slice_buffer_init(&buffer);
- grpc_slice input[] = {
- grpc_slice_from_static_string("foo"),
- grpc_slice_from_static_string("bar"),
- };
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- grpc_slice_buffer_add(&buffer, input[i]);
- }
- // Create byte stream.
- SliceBufferByteStream stream(&buffer, 0);
- grpc_slice_buffer_destroy_internal(&buffer);
- EXPECT_EQ(6U, stream.length());
- grpc_closure closure;
- GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
- grpc_schedule_on_exec_ctx);
- // Read each slice. Note that Next() always returns synchronously.
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
- grpc_slice output;
- grpc_error_handle error = stream.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[i], output));
- grpc_slice_unref_internal(output);
- }
- // Clean up.
- stream.Orphan();
- }
- TEST(SliceBufferByteStream, Shutdown) {
- ExecCtx exec_ctx;
- // Create and populate slice buffer.
- grpc_slice_buffer buffer;
- grpc_slice_buffer_init(&buffer);
- grpc_slice input[] = {
- grpc_slice_from_static_string("foo"),
- grpc_slice_from_static_string("bar"),
- };
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- grpc_slice_buffer_add(&buffer, input[i]);
- }
- // Create byte stream.
- SliceBufferByteStream stream(&buffer, 0);
- grpc_slice_buffer_destroy_internal(&buffer);
- EXPECT_EQ(6U, stream.length());
- grpc_closure closure;
- GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
- grpc_schedule_on_exec_ctx);
- // Read the first slice.
- ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
- grpc_slice output;
- grpc_error_handle error = stream.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[0], output));
- grpc_slice_unref_internal(output);
- // Now shutdown.
- grpc_error_handle shutdown_error =
- GRPC_ERROR_CREATE_FROM_STATIC_STRING("shutdown error");
- stream.Shutdown(GRPC_ERROR_REF(shutdown_error));
- // After shutdown, the next pull() should return the error.
- ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
- error = stream.Pull(&output);
- EXPECT_TRUE(error == shutdown_error);
- GRPC_ERROR_UNREF(error);
- GRPC_ERROR_UNREF(shutdown_error);
- // Clean up.
- stream.Orphan();
- }
- //
- // CachingByteStream tests
- //
- TEST(CachingByteStream, Basic) {
- ExecCtx exec_ctx;
- // Create and populate slice buffer byte stream.
- grpc_slice_buffer buffer;
- grpc_slice_buffer_init(&buffer);
- grpc_slice input[] = {
- grpc_slice_from_static_string("foo"),
- grpc_slice_from_static_string("bar"),
- };
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- grpc_slice_buffer_add(&buffer, input[i]);
- }
- SliceBufferByteStream underlying_stream(&buffer, 0);
- grpc_slice_buffer_destroy_internal(&buffer);
- // Create cache and caching stream.
- ByteStreamCache cache((OrphanablePtr<ByteStream>(&underlying_stream)));
- ByteStreamCache::CachingByteStream stream(&cache);
- grpc_closure closure;
- GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
- grpc_schedule_on_exec_ctx);
- // Read each slice. Note that next() always returns synchronously,
- // because the underlying byte stream always does.
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
- grpc_slice output;
- grpc_error_handle error = stream.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[i], output));
- grpc_slice_unref_internal(output);
- }
- // Clean up.
- stream.Orphan();
- cache.Destroy();
- }
- TEST(CachingByteStream, Reset) {
- ExecCtx exec_ctx;
- // Create and populate slice buffer byte stream.
- grpc_slice_buffer buffer;
- grpc_slice_buffer_init(&buffer);
- grpc_slice input[] = {
- grpc_slice_from_static_string("foo"),
- grpc_slice_from_static_string("bar"),
- };
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- grpc_slice_buffer_add(&buffer, input[i]);
- }
- SliceBufferByteStream underlying_stream(&buffer, 0);
- grpc_slice_buffer_destroy_internal(&buffer);
- // Create cache and caching stream.
- ByteStreamCache cache((OrphanablePtr<ByteStream>(&underlying_stream)));
- ByteStreamCache::CachingByteStream stream(&cache);
- grpc_closure closure;
- GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
- grpc_schedule_on_exec_ctx);
- // Read one slice.
- ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
- grpc_slice output;
- grpc_error_handle error = stream.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[0], output));
- grpc_slice_unref_internal(output);
- // Reset the caching stream. The reads should start over from the
- // first slice.
- stream.Reset();
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
- error = stream.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[i], output));
- grpc_slice_unref_internal(output);
- }
- // Clean up.
- stream.Orphan();
- cache.Destroy();
- }
- TEST(CachingByteStream, SharedCache) {
- ExecCtx exec_ctx;
- // Create and populate slice buffer byte stream.
- grpc_slice_buffer buffer;
- grpc_slice_buffer_init(&buffer);
- grpc_slice input[] = {
- grpc_slice_from_static_string("foo"),
- grpc_slice_from_static_string("bar"),
- };
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- grpc_slice_buffer_add(&buffer, input[i]);
- }
- SliceBufferByteStream underlying_stream(&buffer, 0);
- grpc_slice_buffer_destroy_internal(&buffer);
- // Create cache and two caching streams.
- ByteStreamCache cache((OrphanablePtr<ByteStream>(&underlying_stream)));
- ByteStreamCache::CachingByteStream stream1(&cache);
- ByteStreamCache::CachingByteStream stream2(&cache);
- grpc_closure closure;
- GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
- grpc_schedule_on_exec_ctx);
- // Read one slice from stream1.
- EXPECT_TRUE(stream1.Next(~(size_t)0, &closure));
- grpc_slice output;
- grpc_error_handle error = stream1.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[0], output));
- grpc_slice_unref_internal(output);
- // Read all slices from stream2.
- for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
- EXPECT_TRUE(stream2.Next(~(size_t)0, &closure));
- error = stream2.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[i], output));
- grpc_slice_unref_internal(output);
- }
- // Now read the second slice from stream1.
- EXPECT_TRUE(stream1.Next(~(size_t)0, &closure));
- error = stream1.Pull(&output);
- EXPECT_TRUE(error == GRPC_ERROR_NONE);
- EXPECT_TRUE(grpc_slice_eq(input[1], output));
- grpc_slice_unref_internal(output);
- // Clean up.
- stream1.Orphan();
- stream2.Orphan();
- cache.Destroy();
- }
- } // namespace
- } // namespace grpc_core
- int main(int argc, char** argv) {
- grpc::testing::TestEnvironment env(argc, argv);
- ::testing::InitGoogleTest(&argc, argv);
- grpc_init();
- int retval = RUN_ALL_TESTS();
- grpc_shutdown();
- return retval;
- }
|