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

Side by Side Diff: third_party/WebKit/Source/platform/WebTaskRunnerTest.cpp

Issue 2550373005: Make WebTaskRunner ThreadSafeRefCounted (Closed)
Patch Set: +DISALLOW_COPY_AND_ASSIGN for win build fix Created 3 years, 11 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "platform/WebTaskRunner.h" 5 #include "platform/WebTaskRunner.h"
6 6
7 #include "platform/scheduler/test/fake_web_task_runner.h" 7 #include "platform/scheduler/test/fake_web_task_runner.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace blink { 10 namespace blink {
(...skipping 20 matching lines...) Expand all
31 int counter() const { return m_counter; } 31 int counter() const { return m_counter; }
32 32
33 private: 33 private:
34 int m_counter = 0; 34 int m_counter = 0;
35 WeakPtrFactory<CancellationTestHelper> m_weakPtrFactory; 35 WeakPtrFactory<CancellationTestHelper> m_weakPtrFactory;
36 }; 36 };
37 37
38 } // namespace 38 } // namespace
39 39
40 TEST(WebTaskRunnerTest, PostCancellableTaskTest) { 40 TEST(WebTaskRunnerTest, PostCancellableTaskTest) {
41 scheduler::FakeWebTaskRunner taskRunner; 41 RefPtr<scheduler::FakeWebTaskRunner> taskRunner =
42 adoptRef(new scheduler::FakeWebTaskRunner);
42 43
43 // Run without cancellation. 44 // Run without cancellation.
44 int count = 0; 45 int count = 0;
45 TaskHandle handle = taskRunner.postCancellableTask( 46 TaskHandle handle = taskRunner->postCancellableTask(
46 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); 47 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)));
47 EXPECT_EQ(0, count); 48 EXPECT_EQ(0, count);
48 EXPECT_TRUE(handle.isActive()); 49 EXPECT_TRUE(handle.isActive());
49 taskRunner.runUntilIdle(); 50 taskRunner->runUntilIdle();
50 EXPECT_EQ(1, count); 51 EXPECT_EQ(1, count);
51 EXPECT_FALSE(handle.isActive()); 52 EXPECT_FALSE(handle.isActive());
52 53
53 count = 0; 54 count = 0;
54 handle = taskRunner.postDelayedCancellableTask( 55 handle = taskRunner->postDelayedCancellableTask(
55 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)), 1); 56 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)), 1);
56 EXPECT_EQ(0, count); 57 EXPECT_EQ(0, count);
57 EXPECT_TRUE(handle.isActive()); 58 EXPECT_TRUE(handle.isActive());
58 taskRunner.runUntilIdle(); 59 taskRunner->runUntilIdle();
59 EXPECT_EQ(1, count); 60 EXPECT_EQ(1, count);
60 EXPECT_FALSE(handle.isActive()); 61 EXPECT_FALSE(handle.isActive());
61 62
62 // Cancel a task. 63 // Cancel a task.
63 count = 0; 64 count = 0;
64 handle = taskRunner.postCancellableTask( 65 handle = taskRunner->postCancellableTask(
65 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); 66 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)));
66 handle.cancel(); 67 handle.cancel();
67 EXPECT_EQ(0, count); 68 EXPECT_EQ(0, count);
68 EXPECT_FALSE(handle.isActive()); 69 EXPECT_FALSE(handle.isActive());
69 taskRunner.runUntilIdle(); 70 taskRunner->runUntilIdle();
70 EXPECT_EQ(0, count); 71 EXPECT_EQ(0, count);
71 72
72 // The task should be cancelled when the handle is dropped. 73 // The task should be cancelled when the handle is dropped.
73 { 74 {
74 count = 0; 75 count = 0;
75 TaskHandle handle2 = taskRunner.postCancellableTask( 76 TaskHandle handle2 = taskRunner->postCancellableTask(
76 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); 77 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)));
77 EXPECT_TRUE(handle2.isActive()); 78 EXPECT_TRUE(handle2.isActive());
78 } 79 }
79 EXPECT_EQ(0, count); 80 EXPECT_EQ(0, count);
80 taskRunner.runUntilIdle(); 81 taskRunner->runUntilIdle();
81 EXPECT_EQ(0, count); 82 EXPECT_EQ(0, count);
82 83
83 // The task should be cancelled when another TaskHandle is assigned on it. 84 // The task should be cancelled when another TaskHandle is assigned on it.
84 count = 0; 85 count = 0;
85 handle = taskRunner.postCancellableTask( 86 handle = taskRunner->postCancellableTask(
86 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); 87 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)));
87 handle = taskRunner.postCancellableTask(BLINK_FROM_HERE, WTF::bind([] {})); 88 handle = taskRunner->postCancellableTask(BLINK_FROM_HERE, WTF::bind([] {}));
88 EXPECT_EQ(0, count); 89 EXPECT_EQ(0, count);
89 taskRunner.runUntilIdle(); 90 taskRunner->runUntilIdle();
90 EXPECT_EQ(0, count); 91 EXPECT_EQ(0, count);
91 92
92 // Self assign should be nop. 93 // Self assign should be nop.
93 count = 0; 94 count = 0;
94 handle = taskRunner.postCancellableTask( 95 handle = taskRunner->postCancellableTask(
95 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); 96 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)));
96 #if COMPILER(CLANG) 97 #if COMPILER(CLANG)
97 #pragma GCC diagnostic push 98 #pragma GCC diagnostic push
98 #pragma GCC diagnostic ignored "-Wself-move" 99 #pragma GCC diagnostic ignored "-Wself-move"
99 handle = std::move(handle); 100 handle = std::move(handle);
100 #pragma GCC diagnostic pop 101 #pragma GCC diagnostic pop
101 #else 102 #else
102 handle = std::move(handle); 103 handle = std::move(handle);
103 #endif // COMPILER(CLANG) 104 #endif // COMPILER(CLANG)
104 EXPECT_EQ(0, count); 105 EXPECT_EQ(0, count);
105 taskRunner.runUntilIdle(); 106 taskRunner->runUntilIdle();
106 EXPECT_EQ(1, count); 107 EXPECT_EQ(1, count);
107 108
108 // handle->isActive() should switch to false before the task starts running. 109 // handle->isActive() should switch to false before the task starts running.
109 bool isActive = false; 110 bool isActive = false;
110 handle = taskRunner.postCancellableTask( 111 handle = taskRunner->postCancellableTask(
111 BLINK_FROM_HERE, WTF::bind(&getIsActive, WTF::unretained(&isActive), 112 BLINK_FROM_HERE, WTF::bind(&getIsActive, WTF::unretained(&isActive),
112 WTF::unretained(&handle))); 113 WTF::unretained(&handle)));
113 EXPECT_TRUE(handle.isActive()); 114 EXPECT_TRUE(handle.isActive());
114 taskRunner.runUntilIdle(); 115 taskRunner->runUntilIdle();
115 EXPECT_FALSE(isActive); 116 EXPECT_FALSE(isActive);
116 EXPECT_FALSE(handle.isActive()); 117 EXPECT_FALSE(handle.isActive());
117 } 118 }
118 119
119 TEST(WebTaskRunnerTest, CancellationCheckerTest) { 120 TEST(WebTaskRunnerTest, CancellationCheckerTest) {
120 scheduler::FakeWebTaskRunner taskRunner; 121 RefPtr<scheduler::FakeWebTaskRunner> taskRunner =
122 adoptRef(new scheduler::FakeWebTaskRunner);
121 123
122 int count = 0; 124 int count = 0;
123 TaskHandle handle = taskRunner.postCancellableTask( 125 TaskHandle handle = taskRunner->postCancellableTask(
124 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); 126 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count)));
125 EXPECT_EQ(0, count); 127 EXPECT_EQ(0, count);
126 128
127 // TaskHandle::isActive should detect the deletion of posted task. 129 // TaskHandle::isActive should detect the deletion of posted task.
128 auto queue = taskRunner.takePendingTasksForTesting(); 130 auto queue = taskRunner->takePendingTasksForTesting();
129 ASSERT_EQ(1u, queue.size()); 131 ASSERT_EQ(1u, queue.size());
130 EXPECT_FALSE(queue[0].IsCancelled()); 132 EXPECT_FALSE(queue[0].IsCancelled());
131 EXPECT_TRUE(handle.isActive()); 133 EXPECT_TRUE(handle.isActive());
132 queue.clear(); 134 queue.clear();
133 EXPECT_FALSE(handle.isActive()); 135 EXPECT_FALSE(handle.isActive());
134 EXPECT_EQ(0, count); 136 EXPECT_EQ(0, count);
135 137
136 count = 0; 138 count = 0;
137 CancellationTestHelper helper; 139 CancellationTestHelper helper;
138 handle = taskRunner.postCancellableTask( 140 handle = taskRunner->postCancellableTask(
139 BLINK_FROM_HERE, WTF::bind(&CancellationTestHelper::incrementCounter, 141 BLINK_FROM_HERE, WTF::bind(&CancellationTestHelper::incrementCounter,
140 helper.createWeakPtr())); 142 helper.createWeakPtr()));
141 EXPECT_EQ(0, helper.counter()); 143 EXPECT_EQ(0, helper.counter());
142 144
143 // The cancellation of the posted task should be propagated to TaskHandle. 145 // The cancellation of the posted task should be propagated to TaskHandle.
144 queue = taskRunner.takePendingTasksForTesting(); 146 queue = taskRunner->takePendingTasksForTesting();
145 ASSERT_EQ(1u, queue.size()); 147 ASSERT_EQ(1u, queue.size());
146 EXPECT_FALSE(queue[0].IsCancelled()); 148 EXPECT_FALSE(queue[0].IsCancelled());
147 EXPECT_TRUE(handle.isActive()); 149 EXPECT_TRUE(handle.isActive());
148 helper.revokeWeakPtrs(); 150 helper.revokeWeakPtrs();
149 EXPECT_TRUE(queue[0].IsCancelled()); 151 EXPECT_TRUE(queue[0].IsCancelled());
150 EXPECT_FALSE(handle.isActive()); 152 EXPECT_FALSE(handle.isActive());
151 } 153 }
152 154
153 } // namespace blink 155 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698