for_each_test.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/for_each.h"
  15. #include <gmock/gmock.h>
  16. #include <gtest/gtest.h>
  17. #include "src/core/lib/promise/join.h"
  18. #include "src/core/lib/promise/map.h"
  19. #include "src/core/lib/promise/observable.h"
  20. #include "src/core/lib/promise/pipe.h"
  21. #include "src/core/lib/promise/seq.h"
  22. #include "src/core/lib/resource_quota/resource_quota.h"
  23. #include "test/core/promise/test_wakeup_schedulers.h"
  24. using testing::Mock;
  25. using testing::MockFunction;
  26. using testing::StrictMock;
  27. namespace grpc_core {
  28. static auto* g_memory_allocator = new MemoryAllocator(
  29. ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
  30. TEST(ForEachTest, SendThriceWithPipe) {
  31. int num_received = 0;
  32. StrictMock<MockFunction<void(absl::Status)>> on_done;
  33. EXPECT_CALL(on_done, Call(absl::OkStatus()));
  34. MakeActivity(
  35. [&num_received] {
  36. Pipe<int> pipe;
  37. auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>(
  38. absl::make_unique<PipeSender<int>>(std::move(pipe.sender)));
  39. return Map(
  40. Join(
  41. // Push 3 things into a pipe -- 1, 2, then 3 -- then close.
  42. Seq((*sender)->Push(1), [sender] { return (*sender)->Push(2); },
  43. [sender] { return (*sender)->Push(3); },
  44. [sender] {
  45. sender->reset();
  46. return absl::OkStatus();
  47. }),
  48. // Use a ForEach loop to read them out and verify all values are
  49. // seen.
  50. ForEach(std::move(pipe.receiver),
  51. [&num_received](int i) {
  52. num_received++;
  53. EXPECT_EQ(num_received, i);
  54. return absl::OkStatus();
  55. })),
  56. JustElem<1>());
  57. },
  58. NoWakeupScheduler(),
  59. [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
  60. MakeScopedArena(1024, g_memory_allocator));
  61. Mock::VerifyAndClearExpectations(&on_done);
  62. EXPECT_EQ(num_received, 3);
  63. }
  64. } // namespace grpc_core
  65. int main(int argc, char** argv) {
  66. ::testing::InitGoogleTest(&argc, argv);
  67. return RUN_ALL_TESTS();
  68. }