1 // Copyright 2023 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/inter_activity_pipe.h"
16
17 #include <memory>
18
19 #include "absl/status/status.h"
20 #include "gtest/gtest.h"
21
22 #include "src/core/lib/promise/seq.h"
23 #include "test/core/promise/test_wakeup_schedulers.h"
24
25 namespace grpc_core {
26
27 template <typename F>
TestActivity(F f)28 ActivityPtr TestActivity(F f) {
29 return MakeActivity(std::move(f), InlineWakeupScheduler{},
30 [](absl::Status status) { EXPECT_TRUE(status.ok()); });
31 }
32
TEST(InterActivityPipe,CanSendAndReceive)33 TEST(InterActivityPipe, CanSendAndReceive) {
34 InterActivityPipe<int, 1> pipe;
35 bool done = false;
36 auto a = TestActivity(Seq(pipe.sender.Push(3), [](bool b) {
37 EXPECT_TRUE(b);
38 return absl::OkStatus();
39 }));
40 EXPECT_FALSE(done);
41 auto b = TestActivity(Seq(pipe.receiver.Next(),
42 [&done](InterActivityPipe<int, 1>::NextResult n) {
43 EXPECT_EQ(n.value(), 3);
44 done = true;
45 return absl::OkStatus();
46 }));
47 EXPECT_TRUE(done);
48 }
49
TEST(InterActivityPipe,CanSendTwiceAndReceive)50 TEST(InterActivityPipe, CanSendTwiceAndReceive) {
51 InterActivityPipe<int, 1> pipe;
52 bool done = false;
53 auto a = TestActivity(Seq(
54 pipe.sender.Push(3),
55 [&](bool b) {
56 EXPECT_TRUE(b);
57 return pipe.sender.Push(4);
58 },
59 [](bool b) {
60 EXPECT_TRUE(b);
61 return absl::OkStatus();
62 }));
63 EXPECT_FALSE(done);
64 auto b = TestActivity(Seq(
65 pipe.receiver.Next(),
66 [&pipe](InterActivityPipe<int, 1>::NextResult n) {
67 EXPECT_EQ(n.value(), 3);
68 return pipe.receiver.Next();
69 },
70 [&done](InterActivityPipe<int, 1>::NextResult n) {
71 EXPECT_EQ(n.value(), 4);
72 done = true;
73 return absl::OkStatus();
74 }));
75 EXPECT_TRUE(done);
76 }
77
TEST(InterActivityPipe,CanReceiveAndSend)78 TEST(InterActivityPipe, CanReceiveAndSend) {
79 InterActivityPipe<int, 1> pipe;
80 bool done = false;
81 auto b = TestActivity(Seq(pipe.receiver.Next(),
82 [&done](InterActivityPipe<int, 1>::NextResult n) {
83 EXPECT_EQ(n.value(), 3);
84 done = true;
85 return absl::OkStatus();
86 }));
87 EXPECT_FALSE(done);
88 auto a = TestActivity(Seq(pipe.sender.Push(3), [](bool b) {
89 EXPECT_TRUE(b);
90 return absl::OkStatus();
91 }));
92 EXPECT_TRUE(done);
93 }
94
TEST(InterActivityPipe,CanClose)95 TEST(InterActivityPipe, CanClose) {
96 InterActivityPipe<int, 1> pipe;
97 bool done = false;
98 auto b = TestActivity(Seq(pipe.receiver.Next(),
99 [&done](InterActivityPipe<int, 1>::NextResult n) {
100 EXPECT_FALSE(n.has_value());
101 done = true;
102 return absl::OkStatus();
103 }));
104 EXPECT_FALSE(done);
105 // Drop the sender
106 { auto x = std::move(pipe.sender); }
107 EXPECT_TRUE(done);
108 }
109
110 } // namespace grpc_core
111
main(int argc,char ** argv)112 int main(int argc, char** argv) {
113 ::testing::InitGoogleTest(&argc, argv);
114 return RUN_ALL_TESTS();
115 }
116