| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "platform/scheduler/CancellableTaskFactory.h" | |
| 6 | |
| 7 #include "platform/heap/Handle.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 #include "wtf/PtrUtil.h" | |
| 10 #include <memory> | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 class TestCancellableTaskFactory final : public CancellableTaskFactory { | |
| 17 public: | |
| 18 explicit TestCancellableTaskFactory(std::unique_ptr<WTF::Closure> closure) | |
| 19 : CancellableTaskFactory(std::move(closure)) {} | |
| 20 }; | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 using CancellableTaskFactoryTest = testing::Test; | |
| 25 | |
| 26 TEST_F(CancellableTaskFactoryTest, IsPending_TaskNotCreated) { | |
| 27 TestCancellableTaskFactory factory(nullptr); | |
| 28 | |
| 29 EXPECT_FALSE(factory.isPending()); | |
| 30 } | |
| 31 | |
| 32 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreated) { | |
| 33 TestCancellableTaskFactory factory(nullptr); | |
| 34 std::unique_ptr<WebTaskRunner::Task> task = | |
| 35 wrapUnique(factory.cancelAndCreate()); | |
| 36 | |
| 37 EXPECT_TRUE(factory.isPending()); | |
| 38 } | |
| 39 | |
| 40 void EmptyFn() {} | |
| 41 | |
| 42 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndRun) { | |
| 43 TestCancellableTaskFactory factory(WTF::bind(&EmptyFn)); | |
| 44 { | |
| 45 std::unique_ptr<WebTaskRunner::Task> task = | |
| 46 wrapUnique(factory.cancelAndCreate()); | |
| 47 task->run(); | |
| 48 } | |
| 49 | |
| 50 EXPECT_FALSE(factory.isPending()); | |
| 51 } | |
| 52 | |
| 53 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndDestroyed) { | |
| 54 TestCancellableTaskFactory factory(nullptr); | |
| 55 delete factory.cancelAndCreate(); | |
| 56 | |
| 57 EXPECT_FALSE(factory.isPending()); | |
| 58 } | |
| 59 | |
| 60 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndCancelled) { | |
| 61 TestCancellableTaskFactory factory(nullptr); | |
| 62 std::unique_ptr<WebTaskRunner::Task> task = | |
| 63 wrapUnique(factory.cancelAndCreate()); | |
| 64 factory.cancel(); | |
| 65 | |
| 66 EXPECT_FALSE(factory.isPending()); | |
| 67 } | |
| 68 | |
| 69 void AddOne(int* ptr) { | |
| 70 *ptr += 1; | |
| 71 } | |
| 72 | |
| 73 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) { | |
| 74 int executionCount = 0; | |
| 75 TestCancellableTaskFactory factory( | |
| 76 WTF::bind(&AddOne, WTF::unretained(&executionCount))); | |
| 77 std::unique_ptr<WebTaskRunner::Task> task = | |
| 78 wrapUnique(factory.cancelAndCreate()); | |
| 79 task->run(); | |
| 80 | |
| 81 EXPECT_EQ(1, executionCount); | |
| 82 } | |
| 83 | |
| 84 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) { | |
| 85 int executionCount = 0; | |
| 86 TestCancellableTaskFactory factory( | |
| 87 WTF::bind(&AddOne, WTF::unretained(&executionCount))); | |
| 88 std::unique_ptr<WebTaskRunner::Task> task = | |
| 89 wrapUnique(factory.cancelAndCreate()); | |
| 90 task->run(); | |
| 91 task->run(); | |
| 92 task->run(); | |
| 93 task->run(); | |
| 94 | |
| 95 EXPECT_EQ(1, executionCount); | |
| 96 } | |
| 97 | |
| 98 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) { | |
| 99 int executionCount = 0; | |
| 100 std::unique_ptr<WebTaskRunner::Task> task; | |
| 101 { | |
| 102 TestCancellableTaskFactory factory( | |
| 103 WTF::bind(&AddOne, WTF::unretained(&executionCount))); | |
| 104 task = wrapUnique(factory.cancelAndCreate()); | |
| 105 } | |
| 106 task->run(); | |
| 107 | |
| 108 EXPECT_EQ(0, executionCount); | |
| 109 } | |
| 110 | |
| 111 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence) { | |
| 112 int executionCount = 0; | |
| 113 TestCancellableTaskFactory factory( | |
| 114 WTF::bind(&AddOne, WTF::unretained(&executionCount))); | |
| 115 | |
| 116 std::unique_ptr<WebTaskRunner::Task> taskA = | |
| 117 wrapUnique(factory.cancelAndCreate()); | |
| 118 taskA->run(); | |
| 119 EXPECT_EQ(1, executionCount); | |
| 120 | |
| 121 std::unique_ptr<WebTaskRunner::Task> taskB = | |
| 122 wrapUnique(factory.cancelAndCreate()); | |
| 123 taskB->run(); | |
| 124 EXPECT_EQ(2, executionCount); | |
| 125 | |
| 126 std::unique_ptr<WebTaskRunner::Task> taskC = | |
| 127 wrapUnique(factory.cancelAndCreate()); | |
| 128 taskC->run(); | |
| 129 EXPECT_EQ(3, executionCount); | |
| 130 } | |
| 131 | |
| 132 TEST_F(CancellableTaskFactoryTest, Cancel) { | |
| 133 int executionCount = 0; | |
| 134 TestCancellableTaskFactory factory( | |
| 135 WTF::bind(&AddOne, WTF::unretained(&executionCount))); | |
| 136 std::unique_ptr<WebTaskRunner::Task> task = | |
| 137 wrapUnique(factory.cancelAndCreate()); | |
| 138 factory.cancel(); | |
| 139 task->run(); | |
| 140 | |
| 141 EXPECT_EQ(0, executionCount); | |
| 142 } | |
| 143 | |
| 144 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) { | |
| 145 int executionCount = 0; | |
| 146 TestCancellableTaskFactory factory( | |
| 147 WTF::bind(&AddOne, WTF::unretained(&executionCount))); | |
| 148 | |
| 149 std::unique_ptr<WebTaskRunner::Task> taskA = | |
| 150 wrapUnique(factory.cancelAndCreate()); | |
| 151 std::unique_ptr<WebTaskRunner::Task> taskB = | |
| 152 wrapUnique(factory.cancelAndCreate()); | |
| 153 | |
| 154 taskA->run(); | |
| 155 EXPECT_EQ(0, executionCount); | |
| 156 | |
| 157 taskB->run(); | |
| 158 EXPECT_EQ(1, executionCount); | |
| 159 } | |
| 160 | |
| 161 namespace { | |
| 162 | |
| 163 class GCObject final : public GarbageCollectedFinalized<GCObject> { | |
| 164 public: | |
| 165 GCObject() | |
| 166 : m_factory(CancellableTaskFactory::create(this, &GCObject::run)) {} | |
| 167 | |
| 168 ~GCObject() { s_destructed++; } | |
| 169 | |
| 170 void run() { s_invoked++; } | |
| 171 | |
| 172 DEFINE_INLINE_TRACE() {} | |
| 173 | |
| 174 static int s_destructed; | |
| 175 static int s_invoked; | |
| 176 | |
| 177 std::unique_ptr<CancellableTaskFactory> m_factory; | |
| 178 }; | |
| 179 | |
| 180 int GCObject::s_destructed = 0; | |
| 181 int GCObject::s_invoked = 0; | |
| 182 | |
| 183 } // namespace | |
| 184 | |
| 185 TEST(CancellableTaskFactoryTest, GarbageCollectedWeak) { | |
| 186 GCObject* object = new GCObject(); | |
| 187 std::unique_ptr<WebTaskRunner::Task> task = | |
| 188 wrapUnique(object->m_factory->cancelAndCreate()); | |
| 189 object = nullptr; | |
| 190 ThreadState::current()->collectAllGarbage(); | |
| 191 task->run(); | |
| 192 // The owning object will have been GCed and the task will have | |
| 193 // lost its weak reference. Verify that it wasn't invoked. | |
| 194 EXPECT_EQ(0, GCObject::s_invoked); | |
| 195 | |
| 196 // ..and just to make sure |object| was indeed destructed. | |
| 197 EXPECT_EQ(1, GCObject::s_destructed); | |
| 198 } | |
| 199 | |
| 200 } // namespace blink | |
| OLD | NEW |