xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/tests/unittests/BackgroundExecutorTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 #include <condition_variable>
19 
20 #include "BackgroundExecutor.h"
21 
22 namespace android {
23 
24 class BackgroundExecutorTest : public testing::Test {};
25 
26 namespace {
27 
TEST_F(BackgroundExecutorTest,singleProducer)28 TEST_F(BackgroundExecutorTest, singleProducer) {
29     std::mutex mutex;
30     std::condition_variable condition_variable;
31     bool backgroundTaskComplete = false;
32 
33     BackgroundExecutor::getInstance().sendCallbacks(
34             {[&mutex, &condition_variable, &backgroundTaskComplete]() {
35                 std::lock_guard<std::mutex> lock{mutex};
36                 condition_variable.notify_one();
37                 backgroundTaskComplete = true;
38             }});
39 
40     std::unique_lock<std::mutex> lock{mutex};
41     condition_variable.wait(lock, [&backgroundTaskComplete]() { return backgroundTaskComplete; });
42     ASSERT_TRUE(backgroundTaskComplete);
43 }
44 
TEST_F(BackgroundExecutorTest,multipleProducers)45 TEST_F(BackgroundExecutorTest, multipleProducers) {
46     std::mutex mutex;
47     std::condition_variable condition_variable;
48     const int backgroundTaskCount = 10;
49     int backgroundTaskCompleteCount = 0;
50 
51     for (int i = 0; i < backgroundTaskCount; i++) {
52         std::thread([&mutex, &condition_variable, &backgroundTaskCompleteCount]() {
53             BackgroundExecutor::getInstance().sendCallbacks(
54                     {[&mutex, &condition_variable, &backgroundTaskCompleteCount]() {
55                         std::lock_guard<std::mutex> lock{mutex};
56                         backgroundTaskCompleteCount++;
57                         if (backgroundTaskCompleteCount == backgroundTaskCount) {
58                             condition_variable.notify_one();
59                         }
60                     }});
61         }).detach();
62     }
63 
64     std::unique_lock<std::mutex> lock{mutex};
65     condition_variable.wait(lock, [&backgroundTaskCompleteCount]() {
66         return backgroundTaskCompleteCount == backgroundTaskCount;
67     });
68     ASSERT_EQ(backgroundTaskCount, backgroundTaskCompleteCount);
69 }
70 
71 } // namespace
72 
73 } // namespace android
74