Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: base/threading/sequenced_task_runner_handle_unittest.cc

Issue 2878703002: Use TaskScheduler instead of SequencedWorkerPool in sequenced_task_runner_handle_unittest.cc. (Closed)
Patch Set: CR gab Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/sequenced_task_runner_handle.h" 5 #include "base/threading/sequenced_task_runner_handle.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h" 13 #include "base/run_loop.h"
15 #include "base/sequence_checker_impl.h" 14 #include "base/sequence_checker_impl.h"
16 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
17 #include "base/synchronization/waitable_event.h" 16 #include "base/synchronization/waitable_event.h"
18 #include "base/test/sequenced_worker_pool_owner.h" 17 #include "base/task_scheduler/post_task.h"
18 #include "base/test/scoped_task_environment.h"
19 #include "base/test/test_simple_task_runner.h" 19 #include "base/test/test_simple_task_runner.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/threading/simple_thread.h"
22 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
23 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
24 22
25 namespace base { 23 namespace base {
26 namespace { 24 namespace {
27 25
28 class SequencedTaskRunnerHandleTest : public ::testing::Test { 26 class SequencedTaskRunnerHandleTest : public ::testing::Test {
29 protected: 27 protected:
30 // Verifies that the context it runs on has a SequencedTaskRunnerHandle 28 // Verifies that the context it runs on has a SequencedTaskRunnerHandle
31 // and that posting to it results in the posted task running in that same 29 // and that posting to it results in the posted task running in that same
32 // context (sequence). Runs |callback| on sequence when done. 30 // context (sequence).
33 static void VerifyCurrentSequencedTaskRunner(const Closure& callback) { 31 static void VerifyCurrentSequencedTaskRunner() {
34 ASSERT_TRUE(SequencedTaskRunnerHandle::IsSet()); 32 ASSERT_TRUE(SequencedTaskRunnerHandle::IsSet());
35 scoped_refptr<SequencedTaskRunner> task_runner = 33 scoped_refptr<SequencedTaskRunner> task_runner =
36 SequencedTaskRunnerHandle::Get(); 34 SequencedTaskRunnerHandle::Get();
37 ASSERT_TRUE(task_runner); 35 ASSERT_TRUE(task_runner);
38 36
39 // Use SequenceCheckerImpl to make sure it's not a no-op in Release builds. 37 // Use SequenceCheckerImpl to make sure it's not a no-op in Release builds.
40 std::unique_ptr<SequenceCheckerImpl> sequence_checker( 38 std::unique_ptr<SequenceCheckerImpl> sequence_checker(
41 new SequenceCheckerImpl); 39 new SequenceCheckerImpl);
42 task_runner->PostTask( 40 task_runner->PostTask(
43 FROM_HERE, 41 FROM_HERE,
44 base::BindOnce(&SequencedTaskRunnerHandleTest::CheckValidSequence, 42 base::BindOnce(&SequencedTaskRunnerHandleTest::CheckValidSequence,
45 base::Passed(&sequence_checker), callback)); 43 base::Passed(&sequence_checker)));
46 } 44 }
47 45
48 // Verifies that there is no SequencedTaskRunnerHandle in the context it runs. 46 static void CheckValidSequence(
49 // Runs |callback| when done. 47 std::unique_ptr<SequenceCheckerImpl> sequence_checker) {
50 static void VerifyNoSequencedTaskRunner(const Closure& callback) { 48 EXPECT_TRUE(sequence_checker->CalledOnValidSequence());
51 ASSERT_FALSE(SequencedTaskRunnerHandle::IsSet());
52 callback.Run();
53 } 49 }
54 50
55 private: 51 base::test::ScopedTaskEnvironment scoped_task_environment_;
56 static void CheckValidSequence(
57 std::unique_ptr<SequenceCheckerImpl> sequence_checker,
58 const Closure& callback) {
59 EXPECT_TRUE(sequence_checker->CalledOnValidSequence());
60 callback.Run();
61 }
62
63 MessageLoop message_loop_;
64 }; 52 };
65 53
66 TEST_F(SequencedTaskRunnerHandleTest, FromMessageLoop) { 54 TEST_F(SequencedTaskRunnerHandleTest, FromMessageLoop) {
67 RunLoop run_loop; 55 VerifyCurrentSequencedTaskRunner();
68 VerifyCurrentSequencedTaskRunner(run_loop.QuitClosure()); 56 RunLoop().RunUntilIdle();
69 run_loop.Run();
70 } 57 }
71 58
72 TEST_F(SequencedTaskRunnerHandleTest, FromSequencedWorkerPoolTask) { 59 TEST_F(SequencedTaskRunnerHandleTest, FromTaskSchedulerSequencedTask) {
73 // Wrap the SequencedWorkerPool to avoid leaks due to its asynchronous 60 base::CreateSequencedTaskRunnerWithTraits({})->PostTask(
74 // destruction. 61 FROM_HERE,
75 SequencedWorkerPoolOwner owner(3, "Test");
76 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC,
77 WaitableEvent::InitialState::NOT_SIGNALED);
78 owner.pool()->PostSequencedWorkerTask(
79 owner.pool()->GetSequenceToken(), FROM_HERE,
80 base::BindOnce( 62 base::BindOnce(
81 &SequencedTaskRunnerHandleTest::VerifyCurrentSequencedTaskRunner, 63 &SequencedTaskRunnerHandleTest::VerifyCurrentSequencedTaskRunner));
82 base::Bind(&WaitableEvent::Signal, base::Unretained(&event)))); 64 scoped_task_environment_.RunUntilIdle();
83 event.Wait();
84 } 65 }
85 66
86 TEST_F(SequencedTaskRunnerHandleTest, NoHandleFromUnsequencedTask) { 67 TEST_F(SequencedTaskRunnerHandleTest, NoHandleFromUnsequencedTask) {
87 // Wrap the SequencedWorkerPool to avoid leaks due to its asynchronous 68 base::PostTask(FROM_HERE, base::BindOnce([]() {
88 // destruction. 69 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet());
89 SequencedWorkerPoolOwner owner(3, "Test"); 70 }));
90 WaitableEvent event(WaitableEvent::ResetPolicy::AUTOMATIC, 71 scoped_task_environment_.RunUntilIdle();
91 WaitableEvent::InitialState::NOT_SIGNALED);
92 owner.pool()->PostWorkerTask(
93 FROM_HERE,
94 base::BindOnce(
95 &SequencedTaskRunnerHandleTest::VerifyNoSequencedTaskRunner,
96 base::Bind(&WaitableEvent::Signal, base::Unretained(&event))));
97 event.Wait();
98 } 72 }
99 73
100 TEST(SequencedTaskRunnerHandleTestWithoutMessageLoop, FromHandleInScope) { 74 TEST(SequencedTaskRunnerHandleTestWithoutMessageLoop, FromHandleInScope) {
101 scoped_refptr<SequencedTaskRunner> test_task_runner(new TestSimpleTaskRunner); 75 scoped_refptr<SequencedTaskRunner> test_task_runner(new TestSimpleTaskRunner);
102 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); 76 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet());
103 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); 77 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet());
104 { 78 {
105 SequencedTaskRunnerHandle handle(test_task_runner); 79 SequencedTaskRunnerHandle handle(test_task_runner);
106 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet()); 80 EXPECT_TRUE(SequencedTaskRunnerHandle::IsSet());
107 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); 81 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet());
108 EXPECT_EQ(test_task_runner, SequencedTaskRunnerHandle::Get()); 82 EXPECT_EQ(test_task_runner, SequencedTaskRunnerHandle::Get());
109 } 83 }
110 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet()); 84 EXPECT_FALSE(SequencedTaskRunnerHandle::IsSet());
111 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet()); 85 EXPECT_FALSE(ThreadTaskRunnerHandle::IsSet());
112 } 86 }
113 87
114 } // namespace 88 } // namespace
115 } // namespace base 89 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698