| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/storage/lib/background_loop/background_loop.h" | |
| #include <lib/async/cpp/task.h> | |
| #include <lib/async/cpp/wait.h> | |
| #include <lib/async/default.h> | |
| #include <lib/sync/cpp/completion.h> | |
| #include <lib/zx/eventpair.h> | |
| #include <lib/zx/thread.h> | |
| #include <lib/zx/time.h> | |
| #include <zircon/errors.h> | |
| #include <zircon/syscalls/object.h> | |
| #include <zircon/types.h> | |
| #include <atomic> | |
| #include <string> | |
| #include <string_view> | |
| #include <thread> | |
| #include <gtest/gtest.h> | |
| namespace storage { | |
| namespace { | |
| TEST(BackgroundLoopTest, RunsTasksOnBackgroundThreadWithDefaultDispatcher) { | |
| const std::thread::id caller_tid = std::this_thread::get_id(); | |
| EXPECT_EQ(async_get_default_dispatcher(), nullptr); | |
| libsync::Completion done; | |
| std::thread::id worker_tid; | |
| async_dispatcher_t* worker_default_dispatcher = nullptr; | |
| BackgroundLoop loop; | |
| EXPECT_EQ(async_get_default_dispatcher(), nullptr); | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), | |
| [&] { | |
| worker_tid = std::this_thread::get_id(); | |
| worker_default_dispatcher = async_get_default_dispatcher(); | |
| done.Signal(); | |
| }), | |
| ZX_OK); | |
| done.Wait(); | |
| EXPECT_NE(worker_tid, caller_tid); | |
| EXPECT_EQ(worker_default_dispatcher, loop.dispatcher()); | |
| EXPECT_EQ(async_get_default_dispatcher(), nullptr); | |
| } | |
| TEST(BackgroundLoopTest, SetsThreadName) { | |
| constexpr std::string_view kThreadName = "bg-loop-test"; | |
| libsync::Completion done; | |
| std::string actual_name; | |
| BackgroundLoop loop(kThreadName); | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), | |
| [&] { | |
| char buf[ZX_MAX_NAME_LEN] = {}; | |
| EXPECT_EQ( | |
| zx::thread::self()->get_property(ZX_PROP_NAME, buf, sizeof(buf)), | |
| ZX_OK); | |
| actual_name = buf; | |
| done.Signal(); | |
| }), | |
| ZX_OK); | |
| done.Wait(); | |
| EXPECT_EQ(actual_name, kThreadName); | |
| } | |
| TEST(BackgroundLoopTest, CancelsPendingWaitOnBackgroundThread) { | |
| zx::eventpair ep0, ep1; | |
| ASSERT_EQ(zx::eventpair::create(0, &ep0, &ep1), ZX_OK); | |
| zx_status_t begin_status = ZX_ERR_INTERNAL; | |
| zx_status_t cancel_status = ZX_OK; | |
| std::thread::id worker_tid; | |
| std::thread::id cancel_tid; | |
| async_dispatcher_t* cancel_default_dispatcher = nullptr; | |
| async::WaitOnce wait(ep0.get(), ZX_EVENTPAIR_SIGNALED); | |
| libsync::Completion wait_registered; | |
| BackgroundLoop loop; | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), | |
| [&] { | |
| worker_tid = std::this_thread::get_id(); | |
| begin_status = wait.Begin( | |
| loop.dispatcher(), | |
| [&](async_dispatcher_t* dispatcher, async::WaitOnce*, | |
| zx_status_t status, const zx_packet_signal_t*) { | |
| cancel_status = status; | |
| cancel_tid = std::this_thread::get_id(); | |
| cancel_default_dispatcher = async_get_default_dispatcher(); | |
| }); | |
| wait_registered.Signal(); | |
| }), | |
| ZX_OK); | |
| wait_registered.Wait(); | |
| ASSERT_EQ(begin_status, ZX_OK); | |
| loop.Shutdown(); | |
| EXPECT_EQ(cancel_status, ZX_ERR_CANCELED); | |
| EXPECT_EQ(cancel_tid, worker_tid); | |
| EXPECT_EQ(cancel_default_dispatcher, loop.dispatcher()); | |
| } | |
| TEST(BackgroundLoopTest, CancelsPendingDelayedTaskOnBackgroundThread) { | |
| zx_status_t post_status = ZX_ERR_INTERNAL; | |
| zx_status_t cancel_status = ZX_OK; | |
| std::thread::id worker_tid; | |
| std::thread::id cancel_tid; | |
| async_dispatcher_t* cancel_default_dispatcher = nullptr; | |
| async::Task task([&](async_dispatcher_t* dispatcher, async::Task*, zx_status_t status) { | |
| cancel_status = status; | |
| cancel_tid = std::this_thread::get_id(); | |
| cancel_default_dispatcher = async_get_default_dispatcher(); | |
| }); | |
| libsync::Completion task_posted; | |
| BackgroundLoop loop; | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), | |
| [&] { | |
| worker_tid = std::this_thread::get_id(); | |
| post_status = task.PostDelayed(loop.dispatcher(), zx::hour(1)); | |
| task_posted.Signal(); | |
| }), | |
| ZX_OK); | |
| task_posted.Wait(); | |
| ASSERT_EQ(post_status, ZX_OK); | |
| loop.Shutdown(); | |
| EXPECT_EQ(cancel_status, ZX_ERR_CANCELED); | |
| EXPECT_EQ(cancel_tid, worker_tid); | |
| EXPECT_EQ(cancel_default_dispatcher, loop.dispatcher()); | |
| } | |
| TEST(BackgroundLoopTest, DrainsPreviouslyPostedTasksBeforeShutdown) { | |
| constexpr int kNumTasks = 16; | |
| std::atomic<int> count = 0; | |
| BackgroundLoop loop; | |
| for (int i = 0; i < kNumTasks; ++i) { | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), [&] { count.fetch_add(1); }), ZX_OK); | |
| } | |
| loop.Shutdown(); | |
| EXPECT_EQ(count.load(), kNumTasks); | |
| } | |
| TEST(BackgroundLoopTest, ShutdownIsIdempotent) { | |
| BackgroundLoop loop; | |
| loop.Shutdown(); | |
| loop.Shutdown(); | |
| } | |
| TEST(BackgroundLoopTest, SafeWhenQuitFromBackgroundThread) { | |
| zx_status_t post_status = ZX_ERR_INTERNAL; | |
| zx_status_t cancel_status = ZX_OK; | |
| std::thread::id worker_tid; | |
| std::thread::id cancel_tid; | |
| libsync::Completion canceled; | |
| async::Task task([&](async_dispatcher_t*, async::Task*, zx_status_t status) { | |
| cancel_status = status; | |
| cancel_tid = std::this_thread::get_id(); | |
| canceled.Signal(); | |
| }); | |
| BackgroundLoop loop; | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), | |
| [&] { | |
| worker_tid = std::this_thread::get_id(); | |
| post_status = task.PostDelayed(loop.dispatcher(), zx::hour(1)); | |
| loop.Quit(); | |
| }), | |
| ZX_OK); | |
| canceled.Wait(); | |
| EXPECT_EQ(post_status, ZX_OK); | |
| EXPECT_EQ(cancel_status, ZX_ERR_CANCELED); | |
| EXPECT_EQ(cancel_tid, worker_tid); | |
| // By the time `task` is canceled, `loop_.Shutdown()` has already transitioned the loop to | |
| // `ASYNC_LOOP_SHUTDOWN`, so `PostTask` returns `ZX_ERR_BAD_STATE` and `loop.Shutdown()` | |
| // exercises the already-shut-down path deterministically. | |
| EXPECT_EQ(async::PostTask(loop.dispatcher(), [] {}), ZX_ERR_BAD_STATE); | |
| loop.Shutdown(); | |
| } | |
| TEST(BackgroundLoopTest, QuitWorksWhenDefaultDispatcherOverwritten) { | |
| libsync::Completion quit_called; | |
| BackgroundLoop loop; | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), | |
| [&] { | |
| // Simulate code temporarily overwriting the thread-local default | |
| // dispatcher (e.g., attaching a nested `async::Loop`). | |
| async_set_default_dispatcher(nullptr); | |
| loop.Quit(); | |
| async_set_default_dispatcher(loop.dispatcher()); | |
| quit_called.Signal(); | |
| }), | |
| ZX_OK); | |
| quit_called.Wait(); | |
| loop.Shutdown(); | |
| } | |
| TEST(BackgroundLoopDeathTest, QuitOffBackgroundThreadPanics) { | |
| ASSERT_DEATH( | |
| { | |
| BackgroundLoop loop; | |
| loop.Quit(); | |
| }, | |
| "must only be called from the loop's own thread"); | |
| } | |
| TEST(BackgroundLoopDeathTest, ShutdownFromBackgroundThreadPanics) { | |
| ASSERT_DEATH( | |
| { | |
| libsync::Completion done; | |
| BackgroundLoop loop; | |
| async::PostTask(loop.dispatcher(), [&] { | |
| loop.Shutdown(); | |
| done.Signal(); | |
| }); | |
| done.Wait(); | |
| }, | |
| "must not be called from the loop's own thread"); | |
| } | |
| TEST(BackgroundLoopTest, InFlightHandlerCanBeginWaitWhileShutdownIsPending) { | |
| zx::eventpair ep0, ep1; | |
| ASSERT_EQ(zx::eventpair::create(0, &ep0, &ep1), ZX_OK); | |
| libsync::Completion handler_started; | |
| zx_status_t begin_status = ZX_ERR_INTERNAL; | |
| zx_status_t cancel_status = ZX_OK; | |
| std::thread::id worker_tid; | |
| std::thread::id cancel_tid; | |
| async::WaitOnce wait(ep0.get(), ZX_EVENTPAIR_SIGNALED); | |
| BackgroundLoop loop; | |
| ASSERT_EQ(async::PostTask(loop.dispatcher(), | |
| [&] { | |
| worker_tid = std::this_thread::get_id(); | |
| handler_started.Signal(); | |
| // Give the main thread time to enter `loop.Shutdown()`. Because | |
| // `Shutdown()` posts `Quit()` rather than immediately setting | |
| // `ASYNC_LOOP_SHUTDOWN`, beginning a new wait from this in-flight | |
| // handler must still succeed. | |
| zx::nanosleep(zx::deadline_after(zx::msec(20))); | |
| begin_status = | |
| wait.Begin(loop.dispatcher(), | |
| [&](async_dispatcher_t*, async::WaitOnce*, | |
| zx_status_t status, const zx_packet_signal_t*) { | |
| cancel_status = status; | |
| cancel_tid = std::this_thread::get_id(); | |
| }); | |
| }), | |
| ZX_OK); | |
| handler_started.Wait(); | |
| loop.Shutdown(); | |
| EXPECT_EQ(begin_status, ZX_OK); | |
| EXPECT_EQ(cancel_status, ZX_ERR_CANCELED); | |
| EXPECT_EQ(cancel_tid, worker_tid); | |
| } | |
| } // namespace | |
| } // namespace storage |