1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/task/sequence_manager/work_tracker.h"
#include <optional>
#include "base/test/bind.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base::sequence_manager::internal {
// Verify that no sync work authorization is granted unless allowed by
// `SetRunTaskSynchronouslyAllowed()`.
TEST(SequenceManagerWorkTrackerTest, SetRunTaskSynchronouslyAllowed) {
WorkTracker tracker;
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.OnBeginWork();
tracker.OnIdle();
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.WillRequestReloadImmediateWorkQueue();
tracker.WillReloadImmediateWorkQueues();
tracker.OnIdle();
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.SetRunTaskSynchronouslyAllowed(true);
EXPECT_TRUE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.SetRunTaskSynchronouslyAllowed(false);
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
}
// Verify that `SetRunTaskSynchronouslyAllowed(false)` blocks until there is no
// valid sync work authorization.
TEST(SequenceManagerWorkTrackerTest, SetRunTaskSynchronouslyAllowedBlocks) {
WorkTracker tracker;
tracker.SetRunTaskSynchronouslyAllowed(true);
WaitableEvent did_acquire_sync_work_auth;
bool will_release_sync_work_auth = false;
Thread other_thread("OtherThread");
other_thread.Start();
other_thread.task_runner()->PostTask(
FROM_HERE, BindLambdaForTesting([&] {
std::optional<SyncWorkAuthorization> auth =
tracker.TryAcquireSyncWorkAuthorization();
EXPECT_TRUE(auth->IsValid());
did_acquire_sync_work_auth.Signal();
PlatformThread::Sleep(TestTimeouts::tiny_timeout());
will_release_sync_work_auth = true;
auth.reset();
}));
did_acquire_sync_work_auth.Wait();
tracker.SetRunTaskSynchronouslyAllowed(false);
// `will_release_sync_work_auth` must be true (with no data race detected by
// TSAN) when the call above returns.
EXPECT_TRUE(will_release_sync_work_auth);
other_thread.FlushForTesting();
}
// Verify that after `WillRequestReloadImmediateWorkQueue()`,
// `WillReloadImmediateWorkQueues()` and `OnIdle()` must be called in sequence
// for a sync work authorization to be granted.
TEST(SequenceManagerWorkTrackerTest, WillRequestReloadImmediateWorkQueue) {
WorkTracker tracker;
tracker.SetRunTaskSynchronouslyAllowed(true);
EXPECT_TRUE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.WillRequestReloadImmediateWorkQueue();
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.WillReloadImmediateWorkQueues();
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.OnIdle();
EXPECT_TRUE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.WillRequestReloadImmediateWorkQueue();
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
// `OnIdle()` without `WillReloadImmediateWorkQueues()` is not sufficient for
// a sync work authorization to be granted.
tracker.OnIdle();
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
}
// Verify that after `OnBeginWork()`, `OnIdle()` must be called for a sync
// work authorization to be granted.
TEST(SequenceManagerWorkTrackerTest, OnBeginWork) {
WorkTracker tracker;
tracker.SetRunTaskSynchronouslyAllowed(true);
EXPECT_TRUE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.OnBeginWork();
EXPECT_FALSE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
tracker.OnIdle();
EXPECT_TRUE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
}
// Verify that its not possible to simultaneously acquire two sync work
// authorizations.
TEST(SequenceManagerWorkTrackerTest, TwoSyncWorkAuthorizations) {
WorkTracker tracker;
tracker.SetRunTaskSynchronouslyAllowed(true);
std::optional<SyncWorkAuthorization> first =
tracker.TryAcquireSyncWorkAuthorization();
EXPECT_TRUE(first->IsValid());
SyncWorkAuthorization second = tracker.TryAcquireSyncWorkAuthorization();
EXPECT_FALSE(second.IsValid());
first.reset();
// `second` is invalid so doesn't prevent acquiring another sync work
// authorization.
EXPECT_TRUE(tracker.TryAcquireSyncWorkAuthorization().IsValid());
}
// Verify that `OnBeginWork()` blocks until there is no valid sync work
// authorization.
TEST(SequenceManagerWorkTrackerTest, OnBeginWorkBlocks) {
WorkTracker tracker;
tracker.SetRunTaskSynchronouslyAllowed(true);
WaitableEvent did_acquire_sync_work_auth;
bool will_release_sync_work_auth = false;
Thread other_thread("OtherThread");
other_thread.Start();
other_thread.task_runner()->PostTask(
FROM_HERE, BindLambdaForTesting([&] {
std::optional<SyncWorkAuthorization> auth =
tracker.TryAcquireSyncWorkAuthorization();
EXPECT_TRUE(auth->IsValid());
did_acquire_sync_work_auth.Signal();
PlatformThread::Sleep(TestTimeouts::tiny_timeout());
will_release_sync_work_auth = true;
auth.reset();
}));
did_acquire_sync_work_auth.Wait();
tracker.OnBeginWork();
// `will_release_sync_work_auth` must be true (with no data race detected by
// TSAN) when the call above returns.
EXPECT_TRUE(will_release_sync_work_auth);
other_thread.FlushForTesting();
}
} // namespace base::sequence_manager::internal
|