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/promise_mutex.h"
16
17 #include <memory>
18 #include <optional>
19
20 #include "absl/status/status.h"
21 #include "gtest/gtest.h"
22 #include "src/core/lib/promise/activity.h"
23 #include "src/core/lib/promise/join.h"
24 #include "src/core/lib/promise/promise.h"
25 #include "src/core/lib/promise/seq.h"
26 #include "test/core/promise/test_wakeup_schedulers.h"
27
28 namespace grpc_core {
29 namespace {
30
TEST(PromiseMutexTest,Basic)31 TEST(PromiseMutexTest, Basic) {
32 PromiseMutex<int> mutex{1};
33 bool done = false;
34 MakeActivity(
35 [&]() {
36 return Seq(
37 Join(Seq(mutex.Acquire(),
38 [](PromiseMutex<int>::Lock l) {
39 EXPECT_EQ(*l, 1);
40 *l = 2;
41 }),
42 Seq(mutex.Acquire(),
43 [](PromiseMutex<int>::Lock l) {
44 EXPECT_EQ(*l, 2);
45 *l = 3;
46 }),
47 Seq(mutex.Acquire(),
48 [](PromiseMutex<int>::Lock l) { EXPECT_EQ(*l, 3); })),
49 []() { return absl::OkStatus(); });
50 },
51 InlineWakeupScheduler(),
52 [&done](absl::Status status) {
53 EXPECT_TRUE(status.ok());
54 done = true;
55 });
56 EXPECT_TRUE(done);
57 EXPECT_EQ(**NowOrNever(mutex.Acquire()), 3);
58 }
59
60 } // namespace
61 } // namespace grpc_core
62
main(int argc,char ** argv)63 int main(int argc, char** argv) {
64 ::testing::InitGoogleTest(&argc, argv);
65 return RUN_ALL_TESTS();
66 }
67