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 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "base/task/post_job.h"
11
12 #include <atomic>
13 #include <iterator>
14 #include <numeric>
15
16 #include "base/barrier_closure.h"
17 #include "base/test/bind.h"
18 #include "base/test/gtest_util.h"
19 #include "base/test/task_environment.h"
20 #include "base/test/test_timeouts.h"
21 #include "base/test/test_waitable_event.h"
22 #include "base/threading/platform_thread.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace base {
27
TEST(PostJobTest,PostJobSimple)28 TEST(PostJobTest, PostJobSimple) {
29 test::TaskEnvironment task_environment;
30 std::atomic_size_t num_tasks_to_run(4);
31 auto handle = PostJob(
32 FROM_HERE, {},
33 BindLambdaForTesting([&](JobDelegate* delegate) { --num_tasks_to_run; }),
34 BindLambdaForTesting(
35 [&](size_t /*worker_count*/) -> size_t { return num_tasks_to_run; }));
36 handle.Join();
37 EXPECT_EQ(num_tasks_to_run, 0U);
38 }
39
TEST(PostJobTest,CreateJobSimple)40 TEST(PostJobTest, CreateJobSimple) {
41 test::TaskEnvironment task_environment;
42 std::atomic_size_t num_tasks_to_run(4);
43 TestWaitableEvent threads_continue;
44 RepeatingClosure barrier = BarrierClosure(
45 num_tasks_to_run,
46 BindLambdaForTesting([&threads_continue] { threads_continue.Signal(); }));
47 bool job_started = false;
48 auto handle =
49 CreateJob(FROM_HERE, {}, BindLambdaForTesting([&](JobDelegate* delegate) {
50 EXPECT_TRUE(job_started);
51 barrier.Run();
52 threads_continue.Wait();
53 --num_tasks_to_run;
54 }),
55 BindLambdaForTesting([&](size_t /*worker_count*/) -> size_t {
56 EXPECT_TRUE(job_started);
57 return num_tasks_to_run;
58 }));
59
60 PlatformThread::Sleep(TestTimeouts::tiny_timeout());
61 EXPECT_EQ(num_tasks_to_run, 4U);
62 job_started = true;
63 handle.Join();
64 EXPECT_EQ(num_tasks_to_run, 0U);
65 }
66
67 // Verify that concurrent accesses with task_id as the only form of
68 // synchronisation doesn't trigger a race.
TEST(PostJobTest,TaskIds)69 TEST(PostJobTest, TaskIds) {
70 static constexpr size_t kNumConcurrentThreads = 2;
71 static constexpr size_t kNumTasksToRun = 1000;
72 base::test::TaskEnvironment task_environment;
73
74 size_t concurrent_array[kNumConcurrentThreads] = {0};
75 std::atomic_size_t remaining_tasks{kNumTasksToRun};
76 base::JobHandle handle = base::PostJob(
77 FROM_HERE, {}, BindLambdaForTesting([&](base::JobDelegate* job) {
78 uint8_t id = job->GetTaskId();
79 size_t& slot = concurrent_array[id];
80 slot++;
81 --remaining_tasks;
82 }),
83 BindLambdaForTesting([&remaining_tasks](size_t) {
84 return std::min(remaining_tasks.load(), kNumConcurrentThreads);
85 }));
86 handle.Join();
87 EXPECT_EQ(kNumTasksToRun, std::accumulate(std::begin(concurrent_array),
88 std::end(concurrent_array), 0U));
89 }
90
91 } // namespace base
92