| 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 |
|
| 15 |
#include "src/core/lib/promise/for_each.h" |
| 16 |
|
| 17 |
#include <stdint.h> |
| 18 |
|
| 19 |
#include <memory> |
| 20 |
|
| 21 |
#include "gmock/gmock.h" |
| 22 |
#include "gtest/gtest.h" |
| 23 |
|
| 24 |
#include <grpc/event_engine/memory_allocator.h> |
| 25 |
|
| 26 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 27 |
#include "src/core/lib/promise/activity.h" |
| 28 |
#include "src/core/lib/promise/detail/basic_join.h" |
| 29 |
#include "src/core/lib/promise/detail/basic_seq.h" |
| 30 |
#include "src/core/lib/promise/join.h" |
| 31 |
#include "src/core/lib/promise/map.h" |
| 32 |
#include "src/core/lib/promise/pipe.h" |
| 33 |
#include "src/core/lib/promise/seq.h" |
| 34 |
#include "src/core/lib/promise/try_seq.h" |
| 35 |
#include "src/core/lib/resource_quota/arena.h" |
| 36 |
#include "src/core/lib/resource_quota/memory_quota.h" |
| 37 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 38 |
#include "test/core/promise/test_wakeup_schedulers.h" |
| 39 |
|
| 40 |
using testing::Mock; |
| 41 |
using testing::MockFunction; |
| 42 |
using testing::StrictMock; |
| 43 |
|
| 44 |
namespace grpc_core { |
| 45 |
|
| 46 |
static auto* g_memory_allocator = new MemoryAllocator( |
| 47 |
ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test")); |
| 48 |
|
| 49 |
TEST(ForEachTest, SendThriceWithPipe) { |
| 50 |
int num_received = 0; |
| 51 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 52 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 53 |
MakeActivity( |
| 54 |
[&num_received] { |
| 55 |
Pipe<int> pipe; |
| 56 |
auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>( |
| 57 |
std::make_unique<PipeSender<int>>(std::move(pipe.sender))); |
| 58 |
return Map( |
| 59 |
Join( |
| 60 |
// Push 3 things into a pipe -- 1, 2, then 3 -- then close. |
| 61 |
Seq((*sender)->Push(1), [sender] { return (*sender)->Push(2); }, |
| 62 |
[sender] { return (*sender)->Push(3); }, |
| 63 |
[sender] { |
| 64 |
sender->reset(); |
| 65 |
return absl::OkStatus(); |
| 66 |
}), |
| 67 |
// Use a ForEach loop to read them out and verify all values are |
| 68 |
// seen. |
| 69 |
ForEach(std::move(pipe.receiver), |
| 70 |
[&num_received](int i) { |
| 71 |
num_received++; |
| 72 |
EXPECT_EQ(num_received, i); |
| 73 |
return absl::OkStatus(); |
| 74 |
})), |
| 75 |
JustElem<1>()); |
| 76 |
}, |
| 77 |
NoWakeupScheduler(), |
| 78 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 79 |
MakeScopedArena(1024, g_memory_allocator)); |
| 80 |
Mock::VerifyAndClearExpectations(&on_done); |
| 81 |
EXPECT_EQ(num_received, 3); |
| 82 |
} |
| 83 |
|
| 84 |
// Pollable type that stays movable until it's polled, then causes the test to |
| 85 |
// fail if it's moved again. |
| 86 |
// Promises have the property that they can be moved until polled, and this |
| 87 |
// helps us check that the internals of ForEach respect this rule. |
| 88 |
class MoveableUntilPolled { |
| 89 |
public: |
| 90 |
MoveableUntilPolled() = default; |
| 91 |
MoveableUntilPolled(const MoveableUntilPolled&) = delete; |
| 92 |
MoveableUntilPolled& operator=(const MoveableUntilPolled&) = delete; |
| 93 |
MoveableUntilPolled(MoveableUntilPolled&& other) noexcept : polls_(0) { |
| 94 |
EXPECT_EQ(other.polls_, 0); |
| 95 |
} |
| 96 |
MoveableUntilPolled& operator=(MoveableUntilPolled&& other) noexcept { |
| 97 |
EXPECT_EQ(other.polls_, 0); |
| 98 |
polls_ = 0; |
| 99 |
return *this; |
| 100 |
} |
| 101 |
|
| 102 |
Poll<absl::Status> operator()() { |
| 103 |
Activity::current()->ForceImmediateRepoll(); |
| 104 |
++polls_; |
| 105 |
if (polls_ == 10) return absl::OkStatus(); |
| 106 |
return Pending(); |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
int polls_ = 0; |
| 111 |
}; |
| 112 |
|
| 113 |
TEST(ForEachTest, NoMoveAfterPoll) { |
| 114 |
int num_received = 0; |
| 115 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 116 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 117 |
MakeActivity( |
| 118 |
[&num_received] { |
| 119 |
Pipe<int> pipe; |
| 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 |
// Push one things into a pipe, then close. |
| 125 |
Seq((*sender)->Push(1), |
| 126 |
[sender] { |
| 127 |
sender->reset(); |
| 128 |
return absl::OkStatus(); |
| 129 |
}), |
| 130 |
// Use a ForEach loop to read them out and verify all |
| 131 |
// values are seen. |
| 132 |
// Inject a MoveableUntilPolled into the loop to ensure that |
| 133 |
// ForEach doesn't internally move a promise post-polling. |
| 134 |
ForEach(std::move(pipe.receiver), |
| 135 |
[&num_received](int i) { |
| 136 |
num_received++; |
| 137 |
EXPECT_EQ(num_received, i); |
| 138 |
return MoveableUntilPolled(); |
| 139 |
})), |
| 140 |
JustElem<1>()); |
| 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, 1); |
| 147 |
} |
| 148 |
|
| 149 |
TEST(ForEachTest, NextResultHeldThroughCallback) { |
| 150 |
int num_received = 0; |
| 151 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 152 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 153 |
MakeActivity( |
| 154 |
[&num_received] { |
| 155 |
Pipe<int> pipe; |
| 156 |
auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>( |
| 157 |
std::make_unique<PipeSender<int>>(std::move(pipe.sender))); |
| 158 |
return Map( |
| 159 |
Join( |
| 160 |
// Push one things into a pipe, then close. |
| 161 |
Seq((*sender)->Push(1), |
| 162 |
[sender] { |
| 163 |
sender->reset(); |
| 164 |
return absl::OkStatus(); |
| 165 |
}), |
| 166 |
// Use a ForEach loop to read them out and verify all |
| 167 |
// values are seen. |
| 168 |
ForEach(std::move(pipe.receiver), |
| 169 |
[&num_received, sender](int i) { |
| 170 |
// While we're processing a value NextResult |
| 171 |
// should be held disallowing new items to be |
| 172 |
// pushed. |
| 173 |
// We also should not have reached the |
| 174 |
// sender->reset() line above yet either, as |
| 175 |
// the Push() should block until this code |
| 176 |
// completes. |
| 177 |
EXPECT_TRUE(absl::holds_alternative<Pending>( |
| 178 |
(*sender)->Push(2)())); |
| 179 |
num_received++; |
| 180 |
EXPECT_EQ(num_received, i); |
| 181 |
return TrySeq( |
| 182 |
// has the side effect of stalling for some |
| 183 |
// iterations |
| 184 |
MoveableUntilPolled(), [sender] { |
| 185 |
// Perform the same test verifying the same |
| 186 |
// properties for NextResult holding: all should |
| 187 |
// still be true. |
| 188 |
EXPECT_TRUE(absl::holds_alternative<Pending>( |
| 189 |
(*sender)->Push(2)())); |
| 190 |
return absl::OkStatus(); |
| 191 |
}); |
| 192 |
})), |
| 193 |
JustElem<1>()); |
| 194 |
}, |
| 195 |
NoWakeupScheduler(), |
| 196 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 197 |
MakeScopedArena(1024, g_memory_allocator)); |
| 198 |
Mock::VerifyAndClearExpectations(&on_done); |
| 199 |
EXPECT_EQ(num_received, 1); |
| 200 |
} |
| 201 |
|
| 202 |
} // namespace grpc_core |
| 203 |
|
| 204 |
int main(int argc, char** argv) { |
| 205 |
::testing::InitGoogleTest(&argc, argv); |
| 206 |
return RUN_ALL_TESTS(); |
| 207 |
} |