byte_stream_test.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include "src/core/lib/transport/byte_stream.h"
  19. #include <gtest/gtest.h>
  20. #include <grpc/grpc.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include "src/core/lib/gpr/useful.h"
  24. #include "src/core/lib/iomgr/exec_ctx.h"
  25. #include "src/core/lib/slice/slice_internal.h"
  26. #include "test/core/util/test_config.h"
  27. namespace grpc_core {
  28. namespace {
  29. //
  30. // SliceBufferByteStream tests
  31. //
  32. void NotCalledClosure(void* /*arg*/, grpc_error_handle /*error*/) {
  33. GPR_ASSERT(false);
  34. }
  35. TEST(SliceBufferByteStream, Basic) {
  36. ExecCtx exec_ctx;
  37. // Create and populate slice buffer.
  38. grpc_slice_buffer buffer;
  39. grpc_slice_buffer_init(&buffer);
  40. grpc_slice input[] = {
  41. grpc_slice_from_static_string("foo"),
  42. grpc_slice_from_static_string("bar"),
  43. };
  44. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  45. grpc_slice_buffer_add(&buffer, input[i]);
  46. }
  47. // Create byte stream.
  48. SliceBufferByteStream stream(&buffer, 0);
  49. grpc_slice_buffer_destroy_internal(&buffer);
  50. EXPECT_EQ(6U, stream.length());
  51. grpc_closure closure;
  52. GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
  53. grpc_schedule_on_exec_ctx);
  54. // Read each slice. Note that Next() always returns synchronously.
  55. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  56. ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
  57. grpc_slice output;
  58. grpc_error_handle error = stream.Pull(&output);
  59. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  60. EXPECT_TRUE(grpc_slice_eq(input[i], output));
  61. grpc_slice_unref_internal(output);
  62. }
  63. // Clean up.
  64. stream.Orphan();
  65. }
  66. TEST(SliceBufferByteStream, Shutdown) {
  67. ExecCtx exec_ctx;
  68. // Create and populate slice buffer.
  69. grpc_slice_buffer buffer;
  70. grpc_slice_buffer_init(&buffer);
  71. grpc_slice input[] = {
  72. grpc_slice_from_static_string("foo"),
  73. grpc_slice_from_static_string("bar"),
  74. };
  75. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  76. grpc_slice_buffer_add(&buffer, input[i]);
  77. }
  78. // Create byte stream.
  79. SliceBufferByteStream stream(&buffer, 0);
  80. grpc_slice_buffer_destroy_internal(&buffer);
  81. EXPECT_EQ(6U, stream.length());
  82. grpc_closure closure;
  83. GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
  84. grpc_schedule_on_exec_ctx);
  85. // Read the first slice.
  86. ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
  87. grpc_slice output;
  88. grpc_error_handle error = stream.Pull(&output);
  89. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  90. EXPECT_TRUE(grpc_slice_eq(input[0], output));
  91. grpc_slice_unref_internal(output);
  92. // Now shutdown.
  93. grpc_error_handle shutdown_error =
  94. GRPC_ERROR_CREATE_FROM_STATIC_STRING("shutdown error");
  95. stream.Shutdown(GRPC_ERROR_REF(shutdown_error));
  96. // After shutdown, the next pull() should return the error.
  97. ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
  98. error = stream.Pull(&output);
  99. EXPECT_TRUE(error == shutdown_error);
  100. GRPC_ERROR_UNREF(error);
  101. GRPC_ERROR_UNREF(shutdown_error);
  102. // Clean up.
  103. stream.Orphan();
  104. }
  105. //
  106. // CachingByteStream tests
  107. //
  108. TEST(CachingByteStream, Basic) {
  109. ExecCtx exec_ctx;
  110. // Create and populate slice buffer byte stream.
  111. grpc_slice_buffer buffer;
  112. grpc_slice_buffer_init(&buffer);
  113. grpc_slice input[] = {
  114. grpc_slice_from_static_string("foo"),
  115. grpc_slice_from_static_string("bar"),
  116. };
  117. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  118. grpc_slice_buffer_add(&buffer, input[i]);
  119. }
  120. SliceBufferByteStream underlying_stream(&buffer, 0);
  121. grpc_slice_buffer_destroy_internal(&buffer);
  122. // Create cache and caching stream.
  123. ByteStreamCache cache((OrphanablePtr<ByteStream>(&underlying_stream)));
  124. ByteStreamCache::CachingByteStream stream(&cache);
  125. grpc_closure closure;
  126. GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
  127. grpc_schedule_on_exec_ctx);
  128. // Read each slice. Note that next() always returns synchronously,
  129. // because the underlying byte stream always does.
  130. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  131. ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
  132. grpc_slice output;
  133. grpc_error_handle error = stream.Pull(&output);
  134. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  135. EXPECT_TRUE(grpc_slice_eq(input[i], output));
  136. grpc_slice_unref_internal(output);
  137. }
  138. // Clean up.
  139. stream.Orphan();
  140. cache.Destroy();
  141. }
  142. TEST(CachingByteStream, Reset) {
  143. ExecCtx exec_ctx;
  144. // Create and populate slice buffer byte stream.
  145. grpc_slice_buffer buffer;
  146. grpc_slice_buffer_init(&buffer);
  147. grpc_slice input[] = {
  148. grpc_slice_from_static_string("foo"),
  149. grpc_slice_from_static_string("bar"),
  150. };
  151. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  152. grpc_slice_buffer_add(&buffer, input[i]);
  153. }
  154. SliceBufferByteStream underlying_stream(&buffer, 0);
  155. grpc_slice_buffer_destroy_internal(&buffer);
  156. // Create cache and caching stream.
  157. ByteStreamCache cache((OrphanablePtr<ByteStream>(&underlying_stream)));
  158. ByteStreamCache::CachingByteStream stream(&cache);
  159. grpc_closure closure;
  160. GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
  161. grpc_schedule_on_exec_ctx);
  162. // Read one slice.
  163. ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
  164. grpc_slice output;
  165. grpc_error_handle error = stream.Pull(&output);
  166. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  167. EXPECT_TRUE(grpc_slice_eq(input[0], output));
  168. grpc_slice_unref_internal(output);
  169. // Reset the caching stream. The reads should start over from the
  170. // first slice.
  171. stream.Reset();
  172. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  173. ASSERT_TRUE(stream.Next(~(size_t)0, &closure));
  174. error = stream.Pull(&output);
  175. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  176. EXPECT_TRUE(grpc_slice_eq(input[i], output));
  177. grpc_slice_unref_internal(output);
  178. }
  179. // Clean up.
  180. stream.Orphan();
  181. cache.Destroy();
  182. }
  183. TEST(CachingByteStream, SharedCache) {
  184. ExecCtx exec_ctx;
  185. // Create and populate slice buffer byte stream.
  186. grpc_slice_buffer buffer;
  187. grpc_slice_buffer_init(&buffer);
  188. grpc_slice input[] = {
  189. grpc_slice_from_static_string("foo"),
  190. grpc_slice_from_static_string("bar"),
  191. };
  192. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  193. grpc_slice_buffer_add(&buffer, input[i]);
  194. }
  195. SliceBufferByteStream underlying_stream(&buffer, 0);
  196. grpc_slice_buffer_destroy_internal(&buffer);
  197. // Create cache and two caching streams.
  198. ByteStreamCache cache((OrphanablePtr<ByteStream>(&underlying_stream)));
  199. ByteStreamCache::CachingByteStream stream1(&cache);
  200. ByteStreamCache::CachingByteStream stream2(&cache);
  201. grpc_closure closure;
  202. GRPC_CLOSURE_INIT(&closure, NotCalledClosure, nullptr,
  203. grpc_schedule_on_exec_ctx);
  204. // Read one slice from stream1.
  205. EXPECT_TRUE(stream1.Next(~(size_t)0, &closure));
  206. grpc_slice output;
  207. grpc_error_handle error = stream1.Pull(&output);
  208. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  209. EXPECT_TRUE(grpc_slice_eq(input[0], output));
  210. grpc_slice_unref_internal(output);
  211. // Read all slices from stream2.
  212. for (size_t i = 0; i < GPR_ARRAY_SIZE(input); ++i) {
  213. EXPECT_TRUE(stream2.Next(~(size_t)0, &closure));
  214. error = stream2.Pull(&output);
  215. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  216. EXPECT_TRUE(grpc_slice_eq(input[i], output));
  217. grpc_slice_unref_internal(output);
  218. }
  219. // Now read the second slice from stream1.
  220. EXPECT_TRUE(stream1.Next(~(size_t)0, &closure));
  221. error = stream1.Pull(&output);
  222. EXPECT_TRUE(error == GRPC_ERROR_NONE);
  223. EXPECT_TRUE(grpc_slice_eq(input[1], output));
  224. grpc_slice_unref_internal(output);
  225. // Clean up.
  226. stream1.Orphan();
  227. stream2.Orphan();
  228. cache.Destroy();
  229. }
  230. } // namespace
  231. } // namespace grpc_core
  232. int main(int argc, char** argv) {
  233. grpc::testing::TestEnvironment env(argc, argv);
  234. ::testing::InitGoogleTest(&argc, argv);
  235. grpc_init();
  236. int retval = RUN_ALL_TESTS();
  237. grpc_shutdown();
  238. return retval;
  239. }