Debian Codegraph @

test/core/promise/map_pipe_test.cc

raw
1 // Copyright 2022 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
15 #include "src/core/lib/promise/map_pipe.h"
16
17 #include <stdint.h>
18
19 #include <memory>
20 #include <utility>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 #include <grpc/event_engine/memory_allocator.h>
26
27 #include "src/core/lib/gprpp/ref_counted_ptr.h"
28 #include "src/core/lib/promise/activity.h"
29 #include "src/core/lib/promise/detail/basic_join.h"
30 #include "src/core/lib/promise/detail/basic_seq.h"
31 #include "src/core/lib/promise/for_each.h"
32 #include "src/core/lib/promise/join.h"
33 #include "src/core/lib/promise/map.h"
34 #include "src/core/lib/promise/pipe.h"
35 #include "src/core/lib/promise/poll.h"
36 #include "src/core/lib/promise/seq.h"
37 #include "src/core/lib/promise/try_seq.h"
38 #include "src/core/lib/resource_quota/arena.h"
39 #include "src/core/lib/resource_quota/memory_quota.h"
40 #include "src/core/lib/resource_quota/resource_quota.h"
41 #include "test/core/promise/test_wakeup_schedulers.h"
42
43 using testing::Mock;
44 using testing::MockFunction;
45 using testing::StrictMock;
46
47 namespace grpc_core {
48
49 static auto* g_memory_allocator = new MemoryAllocator(
50 ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
51
52 template <typename T>
53 class Delayed {
54 public:
55 explicit Delayed(T x) : x_(x) {}
56
57 Poll<T> operator()() {
58 Activity::current()->ForceImmediateRepoll();
59 ++polls_;
60 if (polls_ == 10) return std::move(x_);
61 return Pending();
62 }
63
64 private:
65 int polls_ = 0;
66 T x_;
67 };
68
69 TEST(MapPipeTest, SendThriceWithPipeInterceptingReceive) {
70 int num_received = 0;
71 StrictMock<MockFunction<void(absl::Status)>> on_done;
72 EXPECT_CALL(on_done, Call(absl::OkStatus()));
73 MakeActivity(
74 [&num_received] {
75 Pipe<int> pipe;
76 auto filter =
77 PipeMapper<int>::Intercept(pipe.receiver).TakeAndRun([](int x) {
78 return Delayed<int>(x + 1);
79 });
80 auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>(
81 std::make_unique<PipeSender<int>>(std::move(pipe.sender)));
82 return Map(
83 Join(
84 std::move(filter),
85 // Push 3 things into a pipe -- 0, 1, then 2 -- then close.
86 Seq((*sender)->Push(0), [sender] { return (*sender)->Push(1); },
87 [sender] { return (*sender)->Push(2); },
88 [sender] {
89 sender->reset();
90 return absl::OkStatus();
91 }),
92 // Use a ForEach loop to read them out and verify all values are
93 // seen (but with 1 added).
94 ForEach(std::move(pipe.receiver),
95 [&num_received](int i) {
96 num_received++;
97 EXPECT_EQ(num_received, i);
98 return absl::OkStatus();
99 })),
100 JustElem<2>());
101 },
102 NoWakeupScheduler(),
103 [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
104 MakeScopedArena(1024, g_memory_allocator));
105 Mock::VerifyAndClearExpectations(&on_done);
106 EXPECT_EQ(num_received, 3);
107 }
108
109 TEST(MapPipeTest, SendThriceWithPipeInterceptingSend) {
110 int num_received = 0;
111 StrictMock<MockFunction<void(absl::Status)>> on_done;
112 EXPECT_CALL(on_done, Call(absl::OkStatus()));
113 MakeActivity(
114 [&num_received] {
115 Pipe<int> pipe;
116 auto filter =
117 PipeMapper<int>::Intercept(pipe.sender).TakeAndRun([](int x) {
118 return Delayed<int>(x + 1);
119 });
120 auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>(
121 std::make_unique<PipeSender<int>>(std::move(pipe.sender)));
122 return Map(
123 Join(
124 std::move(filter),
125 // Push 3 things into a pipe -- 0, 1, then 2 -- then close.
126 Seq((*sender)->Push(0), [sender] { return (*sender)->Push(1); },
127 [sender] { return (*sender)->Push(2); },
128 [sender] {
129 sender->reset();
130 return absl::OkStatus();
131 }),
132 // Use a ForEach loop to read them out and verify all values are
133 // seen (but with 1 added).
134 ForEach(std::move(pipe.receiver),
135 [&num_received](int i) {
136 num_received++;
137 EXPECT_EQ(num_received, i);
138 return absl::OkStatus();
139 })),
140 JustElem<2>());
141 },
142 NoWakeupScheduler(),
143 [&on_done](absl::Status status) { on_done.Call(std::move(status)); },
144 MakeScopedArena(1024, g_memory_allocator));
145 Mock::VerifyAndClearExpectations(&on_done);
146 EXPECT_EQ(num_received, 3);
147 }
148
149 } // namespace grpc_core
150
151 int main(int argc, char** argv) {
152 ::testing::InitGoogleTest(&argc, argv);
153 return RUN_ALL_TESTS();
154 }