xref: /aosp_15_r20/external/federated-compute/fcp/base/reentrancy_guard_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 // Copyright 2021 Google LLC
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 "fcp/base/reentrancy_guard.h"
16 
17 #include <atomic>
18 
19 #include "gtest/gtest.h"
20 #include "absl/synchronization/notification.h"
21 #include "fcp/base/monitoring.h"
22 #include "fcp/base/scheduler.h"
23 #include "fcp/testing/testing.h"
24 
25 namespace fcp {
26 namespace {
27 
28 class ReentrancyGuardTest : public testing::Test {
29  protected:
SimpleMethod()30   Status SimpleMethod() {
31     ReentrancyGuard guard;
32     return guard.Check(&in_use_);
33   }
34 
ReentrantMethod()35   Status ReentrantMethod() {
36     ReentrancyGuard guard;
37     FCP_RETURN_IF_ERROR((guard.Check(&in_use_)));
38     return ReentrantMethod();
39   }
40 
LongRunningMethod(absl::Notification * method_entered,absl::Notification * resume)41   Status LongRunningMethod(absl::Notification* method_entered,
42                            absl::Notification* resume) {
43     ReentrancyGuard guard;
44     FCP_RETURN_IF_ERROR((guard.Check(&in_use_)));
45     method_entered->Notify();
46     resume->WaitForNotification();
47     return FCP_STATUS(OK);
48   }
49 
50  private:
51   std::atomic<bool> in_use_ = false;
52 };
53 
TEST_F(ReentrancyGuardTest,SequentialCallsSucceed)54 TEST_F(ReentrancyGuardTest, SequentialCallsSucceed) {
55   ASSERT_THAT(SimpleMethod(), IsOk());
56   ASSERT_THAT(SimpleMethod(), IsOk());
57 }
58 
TEST_F(ReentrancyGuardTest,ReentrantCallsFail)59 TEST_F(ReentrancyGuardTest, ReentrantCallsFail) {
60   ASSERT_THAT(ReentrantMethod(), IsCode(FAILED_PRECONDITION));
61 }
62 
TEST_F(ReentrancyGuardTest,ConcurrentCallsFail)63 TEST_F(ReentrancyGuardTest, ConcurrentCallsFail) {
64   absl::Notification long_running_method_entered;
65   absl::Notification resume;
66 
67   auto pool = fcp::CreateThreadPoolScheduler(1);
68   pool->Schedule([&] {
69     ASSERT_THAT(LongRunningMethod(&long_running_method_entered, &resume),
70                 IsOk());
71   });
72 
73   // This signals that LongRunningMethod() has been entered and waits there
74   // to be resumed.
75   long_running_method_entered.WaitForNotification();
76 
77   // Make a concurrent call, which is expected to fail.
78   ASSERT_THAT(SimpleMethod(), IsCode(FAILED_PRECONDITION));
79 
80   // Resume LongRunningMethod() and wait for the thread to finish.
81   resume.Notify();
82   pool->WaitUntilIdle();
83 }
84 
85 }  // namespace
86 }  // namespace fcp
87