xref: /aosp_15_r20/external/angle/src/common/WorkerThread_unittest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // WorkerThread_unittest:
7 //   Simple tests for the worker thread class.
8 
9 #include <gtest/gtest.h>
10 #include <array>
11 
12 #include "common/WorkerThread.h"
13 
14 using namespace angle;
15 
16 namespace
17 {
18 
19 // Tests simple worker pool application.
TEST(WorkerPoolTest,SimpleTask)20 TEST(WorkerPoolTest, SimpleTask)
21 {
22     class TestTask : public Closure
23     {
24       public:
25         void operator()() override { fired = true; }
26 
27         bool fired = false;
28     };
29 
30     std::array<std::shared_ptr<WorkerThreadPool>, 2> pools = {
31         {WorkerThreadPool::Create(1, ANGLEPlatformCurrent()),
32          WorkerThreadPool::Create(0, ANGLEPlatformCurrent())}};
33     for (auto &pool : pools)
34     {
35         std::array<std::shared_ptr<TestTask>, 4> tasks = {
36             {std::make_shared<TestTask>(), std::make_shared<TestTask>(),
37              std::make_shared<TestTask>(), std::make_shared<TestTask>()}};
38         std::array<std::shared_ptr<WaitableEvent>, 4> waitables = {
39             {pool->postWorkerTask(tasks[0]), pool->postWorkerTask(tasks[1]),
40              pool->postWorkerTask(tasks[2]), pool->postWorkerTask(tasks[3])}};
41 
42         WaitableEvent::WaitMany(&waitables);
43 
44         for (const auto &task : tasks)
45         {
46             EXPECT_TRUE(task->fired);
47         }
48     }
49 }
50 
51 }  // anonymous namespace
52