pipe_test.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2021 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "src/core/lib/promise/pipe.h"
  15. #include <gmock/gmock.h>
  16. #include <gtest/gtest.h>
  17. #include "absl/memory/memory.h"
  18. #include "src/core/lib/promise/join.h"
  19. #include "src/core/lib/promise/promise.h"
  20. #include "src/core/lib/promise/seq.h"
  21. #include "src/core/lib/resource_quota/resource_quota.h"
  22. #include "test/core/promise/test_wakeup_schedulers.h"
  23. using testing::MockFunction;
  24. using testing::StrictMock;
  25. namespace grpc_core {
  26. static auto* g_memory_allocator = new MemoryAllocator(
  27. ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
  28. TEST(PipeTest, CanSendAndReceive) {
  29. StrictMock<MockFunction<void(absl::Status)>> on_done;
  30. EXPECT_CALL(on_done, Call(absl::OkStatus()));
  31. MakeActivity(
  32. [] {
  33. Pipe<int> pipe;
  34. return Seq(
  35. // Concurrently: send 42 into the pipe, and receive from the pipe.
  36. Join(pipe.sender.Push(42), pipe.receiver.Next()),
  37. // Once complete, verify successful sending and the received value
  38. // is 42.
  39. [](std::tuple<bool, absl::optional<int>> result) {
  40. EXPECT_EQ(result, std::make_tuple(true, absl::optional<int>(42)));
  41. return absl::OkStatus();
  42. });
  43. },
  44. NoWakeupScheduler(),
  45. [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
  46. MakeScopedArena(1024, g_memory_allocator));
  47. }
  48. TEST(PipeTest, CanReceiveAndSend) {
  49. StrictMock<MockFunction<void(absl::Status)>> on_done;
  50. EXPECT_CALL(on_done, Call(absl::OkStatus()));
  51. MakeActivity(
  52. [] {
  53. Pipe<int> pipe;
  54. return Seq(
  55. // Concurrently: receive from the pipe, and send 42 into the pipe.
  56. Join(pipe.receiver.Next(), pipe.sender.Push(42)),
  57. // Once complete, verify the received value is 42 and successful
  58. // sending.
  59. [](std::tuple<absl::optional<int>, bool> result) {
  60. EXPECT_EQ(result, std::make_tuple(absl::optional<int>(42), true));
  61. return absl::OkStatus();
  62. });
  63. },
  64. NoWakeupScheduler(),
  65. [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
  66. MakeScopedArena(1024, g_memory_allocator));
  67. }
  68. TEST(PipeTest, CanSeeClosedOnSend) {
  69. StrictMock<MockFunction<void(absl::Status)>> on_done;
  70. EXPECT_CALL(on_done, Call(absl::OkStatus()));
  71. MakeActivity(
  72. [] {
  73. Pipe<int> pipe;
  74. auto sender = std::move(pipe.sender);
  75. // Push 42 onto the pipe - this will the pipe's one-deep send buffer.
  76. EXPECT_TRUE(NowOrNever(sender.Push(42)).has_value());
  77. auto receiver = std::make_shared<std::unique_ptr<PipeReceiver<int>>>(
  78. absl::make_unique<PipeReceiver<int>>(std::move(pipe.receiver)));
  79. return Seq(
  80. // Concurrently:
  81. // - push 43 into the sender, which will stall because the buffer is
  82. // full
  83. // - and close the receiver, which will fail the pending send.
  84. Join(sender.Push(43),
  85. [receiver] {
  86. receiver->reset();
  87. return absl::OkStatus();
  88. }),
  89. // Verify both that the send failed and that we executed the close.
  90. [](std::tuple<bool, absl::Status> result) {
  91. EXPECT_EQ(result, std::make_tuple(false, absl::OkStatus()));
  92. return absl::OkStatus();
  93. });
  94. },
  95. NoWakeupScheduler(),
  96. [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
  97. MakeScopedArena(1024, g_memory_allocator));
  98. }
  99. TEST(PipeTest, CanSeeClosedOnReceive) {
  100. StrictMock<MockFunction<void(absl::Status)>> on_done;
  101. EXPECT_CALL(on_done, Call(absl::OkStatus()));
  102. MakeActivity(
  103. [] {
  104. Pipe<int> pipe;
  105. auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>(
  106. absl::make_unique<PipeSender<int>>(std::move(pipe.sender)));
  107. auto receiver = std::move(pipe.receiver);
  108. return Seq(
  109. // Concurrently:
  110. // - wait for a received value (will stall forever since we push
  111. // nothing into the queue)
  112. // - close the sender, which will signal the receiver to return an
  113. // end-of-stream.
  114. Join(receiver.Next(),
  115. [sender] {
  116. sender->reset();
  117. return absl::OkStatus();
  118. }),
  119. // Verify we received end-of-stream and closed the sender.
  120. [](std::tuple<absl::optional<int>, absl::Status> result) {
  121. EXPECT_EQ(result, std::make_tuple(absl::optional<int>(),
  122. absl::OkStatus()));
  123. return absl::OkStatus();
  124. });
  125. },
  126. NoWakeupScheduler(),
  127. [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
  128. MakeScopedArena(1024, g_memory_allocator));
  129. }
  130. } // namespace grpc_core
  131. int main(int argc, char** argv) {
  132. ::testing::InitGoogleTest(&argc, argv);
  133. return RUN_ALL_TESTS();
  134. }