xref: /aosp_15_r20/external/cronet/base/task/post_job_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/task/post_job.h"
6 
7 #include <atomic>
8 #include <iterator>
9 #include <numeric>
10 
11 #include "base/barrier_closure.h"
12 #include "base/test/bind.h"
13 #include "base/test/gtest_util.h"
14 #include "base/test/task_environment.h"
15 #include "base/test/test_timeouts.h"
16 #include "base/test/test_waitable_event.h"
17 #include "base/threading/platform_thread.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 
21 namespace base {
22 
TEST(PostJobTest,PostJobSimple)23 TEST(PostJobTest, PostJobSimple) {
24   test::TaskEnvironment task_environment;
25   std::atomic_size_t num_tasks_to_run(4);
26   auto handle = PostJob(
27       FROM_HERE, {},
28       BindLambdaForTesting([&](JobDelegate* delegate) { --num_tasks_to_run; }),
29       BindLambdaForTesting(
30           [&](size_t /*worker_count*/) -> size_t { return num_tasks_to_run; }));
31   handle.Join();
32   EXPECT_EQ(num_tasks_to_run, 0U);
33 }
34 
TEST(PostJobTest,CreateJobSimple)35 TEST(PostJobTest, CreateJobSimple) {
36   test::TaskEnvironment task_environment;
37   std::atomic_size_t num_tasks_to_run(4);
38   TestWaitableEvent threads_continue;
39   RepeatingClosure barrier = BarrierClosure(
40       num_tasks_to_run, BindLambdaForTesting([&threads_continue]() {
41         threads_continue.Signal();
42       }));
43   bool job_started = false;
44   auto handle =
45       CreateJob(FROM_HERE, {}, BindLambdaForTesting([&](JobDelegate* delegate) {
46                   EXPECT_TRUE(job_started);
47                   barrier.Run();
48                   threads_continue.Wait();
49                   --num_tasks_to_run;
50                 }),
51                 BindLambdaForTesting([&](size_t /*worker_count*/) -> size_t {
52                   EXPECT_TRUE(job_started);
53                   return num_tasks_to_run;
54                 }));
55 
56   PlatformThread::Sleep(TestTimeouts::tiny_timeout());
57   EXPECT_EQ(num_tasks_to_run, 4U);
58   job_started = true;
59   handle.Join();
60   EXPECT_EQ(num_tasks_to_run, 0U);
61 }
62 
63 // Verify that concurrent accesses with task_id as the only form of
64 // synchronisation doesn't trigger a race.
TEST(PostJobTest,TaskIds)65 TEST(PostJobTest, TaskIds) {
66   static constexpr size_t kNumConcurrentThreads = 2;
67   static constexpr size_t kNumTasksToRun = 1000;
68   base::test::TaskEnvironment task_environment;
69 
70   size_t concurrent_array[kNumConcurrentThreads] = {0};
71   std::atomic_size_t remaining_tasks{kNumTasksToRun};
72   base::JobHandle handle = base::PostJob(
73       FROM_HERE, {}, BindLambdaForTesting([&](base::JobDelegate* job) {
74         uint8_t id = job->GetTaskId();
75         size_t& slot = concurrent_array[id];
76         slot++;
77         --remaining_tasks;
78       }),
79       BindLambdaForTesting([&remaining_tasks](size_t) {
80         return std::min(remaining_tasks.load(), kNumConcurrentThreads);
81       }));
82   handle.Join();
83   EXPECT_EQ(kNumTasksToRun, std::accumulate(std::begin(concurrent_array),
84                                             std::end(concurrent_array), 0U));
85 }
86 
87 }  // namespace base
88