chromium/third_party/blink/renderer/core/workers/worker_thread_test.cc

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/workers/worker_thread.h"

#include <memory>
#include <utility>

#include "base/run_loop.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/inspector/inspector_task_runner.h"
#include "third_party/blink/renderer/core/inspector/worker_devtools_params.h"
#include "third_party/blink/renderer/core/inspector/worker_thread_debugger.h"
#include "third_party/blink/renderer/core/script/script.h"
#include "third_party/blink/renderer/core/workers/global_scope_creation_params.h"
#include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h"
#include "third_party/blink/renderer/core/workers/worker_thread_test_helper.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/test/fake_task_runner.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"

_;
AtMost;

namespace blink {

ExitCode;

namespace {

// Used as a debugger task. Waits for a signal from the main thread.
void WaitForSignalTask(WorkerThread* worker_thread,
                       base::WaitableEvent* waitable_event,
                       CrossThreadOnceClosure quit_closure) {}

void TerminateParentOfNestedWorker(WorkerThread* parent_thread,
                                   base::WaitableEvent* waitable_event) {}

void PauseExecution(v8::Isolate* isolate, void* data) {}

// This helper managers a child worker thread and a reporting proxy
// and ensures they stay alive for the duration of the test. The struct
// is created on the main thread, but its members are created and
// destroyed on the parent worker thread.
struct NestedWorkerHelper {};

void CreateNestedWorkerThenTerminateParent(
    WorkerThread* parent_thread,
    NestedWorkerHelper* nested_worker_helper,
    CrossThreadOnceClosure quit_closure) {}

void VerifyParentAndChildAreTerminated(WorkerThread* parent_thread,
                                       NestedWorkerHelper* nested_worker_helper,
                                       base::WaitableEvent* waitable_event) {}

}  // namespace

class WorkerThreadTest : public testing::Test {};

TEST_F(WorkerThreadTest, ShouldTerminateScriptExecution) {}

TEST_F(WorkerThreadTest, AsyncTerminate_OnIdle) {}

TEST_F(WorkerThreadTest, SyncTerminate_OnIdle) {}

TEST_F(WorkerThreadTest, AsyncTerminate_ImmediatelyAfterStart) {}

TEST_F(WorkerThreadTest, SyncTerminate_ImmediatelyAfterStart) {}

// TODO(crbug.com/1503519): The test is flaky on Linux TSan
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_AsyncTerminate_WhileTaskIsRunning
#else
#define MAYBE_AsyncTerminate_WhileTaskIsRunning
#endif
TEST_F(WorkerThreadTest, MAYBE_AsyncTerminate_WhileTaskIsRunning) {}

TEST_F(WorkerThreadTest, SyncTerminate_WhileTaskIsRunning) {}

TEST_F(WorkerThreadTest,
       AsyncTerminateAndThenSyncTerminate_WhileTaskIsRunning) {}

TEST_F(WorkerThreadTest, Terminate_WhileDebuggerTaskIsRunningOnInitialization) {}

TEST_F(WorkerThreadTest, Terminate_WhileDebuggerTaskIsRunning) {}

// TODO(https://crbug.com/1072997): This test occasionally crashes.
TEST_F(WorkerThreadTest, DISABLED_TerminateWorkerWhileChildIsLoading) {}

// Tests terminating a worker when debugger is paused.
// TODO(crbug.com/1503316): The test is flaky on Linux TSan
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_TerminateWhileWorkerPausedByDebugger
#else
#define MAYBE_TerminateWhileWorkerPausedByDebugger
#endif
TEST_F(WorkerThreadTest, MAYBE_TerminateWhileWorkerPausedByDebugger) {}

// TODO(crbug.com/1503287): The test is flaky on Linux TSan
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_TerminateFrozenScript
#else
#define MAYBE_TerminateFrozenScript
#endif
TEST_F(WorkerThreadTest, MAYBE_TerminateFrozenScript) {}

// TODO(crbug.com/1508694): The test is flaky on Linux TSan
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_NestedPauseFreeze
#else
#define MAYBE_NestedPauseFreeze
#endif
TEST_F(WorkerThreadTest, MAYBE_NestedPauseFreeze) {}

// TODO(crbug.com/1508694): The test is flaky on Linux TSan
#if BUILDFLAG(IS_LINUX) && defined(THREAD_SANITIZER)
#define MAYBE_NestedPauseFreezeNoInterrupts
#else
#define MAYBE_NestedPauseFreezeNoInterrupts
#endif
TEST_F(WorkerThreadTest, MAYBE_NestedPauseFreezeNoInterrupts) {}

}  // namespace blink