| 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/pipe.h" |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
#include <tuple> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "absl/status/status.h" |
| 22 |
#include "gmock/gmock.h" |
| 23 |
#include "gtest/gtest.h" |
| 24 |
|
| 25 |
#include <grpc/event_engine/memory_allocator.h> |
| 26 |
#include <grpc/support/log.h> |
| 27 |
|
| 28 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 29 |
#include "src/core/lib/promise/activity.h" |
| 30 |
#include "src/core/lib/promise/detail/basic_join.h" |
| 31 |
#include "src/core/lib/promise/join.h" |
| 32 |
#include "src/core/lib/promise/map.h" |
| 33 |
#include "src/core/lib/promise/seq.h" |
| 34 |
#include "src/core/lib/resource_quota/memory_quota.h" |
| 35 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 36 |
#include "test/core/promise/test_wakeup_schedulers.h" |
| 37 |
|
| 38 |
using testing::MockFunction; |
| 39 |
using testing::StrictMock; |
| 40 |
|
| 41 |
namespace grpc_core { |
| 42 |
|
| 43 |
static auto* g_memory_allocator = new MemoryAllocator( |
| 44 |
ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test")); |
| 45 |
|
| 46 |
TEST(PipeTest, CanSendAndReceive) { |
| 47 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 48 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 49 |
MakeActivity( |
| 50 |
[] { |
| 51 |
Pipe<int> pipe; |
| 52 |
return Seq( |
| 53 |
// Concurrently: send 42 into the pipe, and receive from the pipe. |
| 54 |
Join(pipe.sender.Push(42), |
| 55 |
Map(pipe.receiver.Next(), |
| 56 |
[](NextResult<int> r) { return r.value(); })), |
| 57 |
// Once complete, verify successful sending and the received value |
| 58 |
// is 42. |
| 59 |
[](std::tuple<bool, int> result) { |
| 60 |
EXPECT_TRUE(std::get<0>(result)); |
| 61 |
EXPECT_EQ(42, std::get<1>(result)); |
| 62 |
return absl::OkStatus(); |
| 63 |
}); |
| 64 |
}, |
| 65 |
NoWakeupScheduler(), |
| 66 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 67 |
MakeScopedArena(1024, g_memory_allocator)); |
| 68 |
} |
| 69 |
|
| 70 |
TEST(PipeTest, CanReceiveAndSend) { |
| 71 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 72 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 73 |
MakeActivity( |
| 74 |
[] { |
| 75 |
Pipe<int> pipe; |
| 76 |
return Seq( |
| 77 |
// Concurrently: receive from the pipe, and send 42 into the pipe. |
| 78 |
Join(Map(pipe.receiver.Next(), |
| 79 |
[](NextResult<int> r) { return r.value(); }), |
| 80 |
pipe.sender.Push(42)), |
| 81 |
// Once complete, verify the received value is 42 and successful |
| 82 |
// sending. |
| 83 |
[](std::tuple<int, bool> result) { |
| 84 |
EXPECT_EQ(std::get<0>(result), 42); |
| 85 |
EXPECT_TRUE(std::get<1>(result)); |
| 86 |
return absl::OkStatus(); |
| 87 |
}); |
| 88 |
}, |
| 89 |
NoWakeupScheduler(), |
| 90 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 91 |
MakeScopedArena(1024, g_memory_allocator)); |
| 92 |
} |
| 93 |
|
| 94 |
TEST(PipeTest, CanSeeClosedOnSend) { |
| 95 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 96 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 97 |
MakeActivity( |
| 98 |
[] { |
| 99 |
Pipe<int> pipe; |
| 100 |
auto sender = std::move(pipe.sender); |
| 101 |
auto receiver = std::make_shared<std::unique_ptr<PipeReceiver<int>>>( |
| 102 |
std::make_unique<PipeReceiver<int>>(std::move(pipe.receiver))); |
| 103 |
return Seq( |
| 104 |
// Concurrently: |
| 105 |
// - push 43 into the sender, which will stall because there is no |
| 106 |
// reader |
| 107 |
// - and close the receiver, which will fail the pending send. |
| 108 |
Join(sender.Push(43), |
| 109 |
[receiver] { |
| 110 |
receiver->reset(); |
| 111 |
return absl::OkStatus(); |
| 112 |
}), |
| 113 |
// Verify both that the send failed and that we executed the close. |
| 114 |
[](const std::tuple<bool, absl::Status>& result) { |
| 115 |
EXPECT_EQ(result, std::make_tuple(false, absl::OkStatus())); |
| 116 |
return absl::OkStatus(); |
| 117 |
}); |
| 118 |
}, |
| 119 |
NoWakeupScheduler(), |
| 120 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 121 |
MakeScopedArena(1024, g_memory_allocator)); |
| 122 |
} |
| 123 |
|
| 124 |
TEST(PipeTest, CanSeeClosedOnReceive) { |
| 125 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 126 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 127 |
MakeActivity( |
| 128 |
[] { |
| 129 |
Pipe<int> pipe; |
| 130 |
auto sender = std::make_shared<std::unique_ptr<PipeSender<int>>>( |
| 131 |
std::make_unique<PipeSender<int>>(std::move(pipe.sender))); |
| 132 |
auto receiver = std::move(pipe.receiver); |
| 133 |
return Seq( |
| 134 |
// Concurrently: |
| 135 |
// - wait for a received value (will stall forever since we push |
| 136 |
// nothing into the queue) |
| 137 |
// - close the sender, which will signal the receiver to return an |
| 138 |
// end-of-stream. |
| 139 |
Join(receiver.Next(), |
| 140 |
[sender] { |
| 141 |
sender->reset(); |
| 142 |
return absl::OkStatus(); |
| 143 |
}), |
| 144 |
// Verify we received end-of-stream and closed the sender. |
| 145 |
[](std::tuple<NextResult<int>, absl::Status> result) { |
| 146 |
EXPECT_FALSE(std::get<0>(result).has_value()); |
| 147 |
EXPECT_EQ(std::get<1>(result), absl::OkStatus()); |
| 148 |
return absl::OkStatus(); |
| 149 |
}); |
| 150 |
}, |
| 151 |
NoWakeupScheduler(), |
| 152 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 153 |
MakeScopedArena(1024, g_memory_allocator)); |
| 154 |
} |
| 155 |
|
| 156 |
TEST(PipeTest, CanFlowControlThroughManyStages) { |
| 157 |
StrictMock<MockFunction<void(absl::Status)>> on_done; |
| 158 |
EXPECT_CALL(on_done, Call(absl::OkStatus())); |
| 159 |
auto done = std::make_shared<bool>(false); |
| 160 |
// Push a value through multiple pipes. |
| 161 |
// Ensure that it's possible to do so and get flow control throughout the |
| 162 |
// entire pipe: ie that the push down does not complete until the last pipe |
| 163 |
// completes. |
| 164 |
MakeActivity( |
| 165 |
[done] { |
| 166 |
Pipe<int> pipe1; |
| 167 |
Pipe<int> pipe2; |
| 168 |
Pipe<int> pipe3; |
| 169 |
auto sender1 = std::move(pipe1.sender); |
| 170 |
auto receiver1 = std::move(pipe1.receiver); |
| 171 |
auto sender2 = std::move(pipe2.sender); |
| 172 |
auto receiver2 = std::move(pipe2.receiver); |
| 173 |
auto sender3 = std::move(pipe3.sender); |
| 174 |
auto receiver3 = std::move(pipe3.receiver); |
| 175 |
return Seq( |
| 176 |
Join(Seq(sender1.Push(1), |
| 177 |
[done] { |
| 178 |
*done = true; |
| 179 |
return 1; |
| 180 |
}), |
| 181 |
Seq(receiver1.Next(), |
| 182 |
[sender2 = std::move(sender2)](NextResult<int> r) mutable { |
| 183 |
return sender2.Push(r.value()); |
| 184 |
}), |
| 185 |
Seq(receiver2.Next(), |
| 186 |
[sender3 = std::move(sender3)](NextResult<int> r) mutable { |
| 187 |
return sender3.Push(r.value()); |
| 188 |
}), |
| 189 |
Seq(receiver3.Next(), |
| 190 |
[done](NextResult<int> r) { |
| 191 |
EXPECT_EQ(r.value(), 1); |
| 192 |
EXPECT_FALSE(*done); |
| 193 |
return 2; |
| 194 |
})), |
| 195 |
[](std::tuple<int, bool, bool, int> result) { |
| 196 |
EXPECT_EQ(result, std::make_tuple(1, true, true, 2)); |
| 197 |
return absl::OkStatus(); |
| 198 |
}); |
| 199 |
}, |
| 200 |
NoWakeupScheduler(), |
| 201 |
[&on_done](absl::Status status) { on_done.Call(std::move(status)); }, |
| 202 |
MakeScopedArena(1024, g_memory_allocator)); |
| 203 |
ASSERT_TRUE(*done); |
| 204 |
} |
| 205 |
|
| 206 |
} // namespace grpc_core |
| 207 |
|
| 208 |
int main(int argc, char** argv) { |
| 209 |
gpr_log_verbosity_init(); |
| 210 |
::testing::InitGoogleTest(&argc, argv); |
| 211 |
return RUN_ALL_TESTS(); |
| 212 |
} |