diff --git a/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp b/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp index df618a7b8e9..d06a22e4594 100644 --- a/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp +++ b/backends/aoti/slim/cuda/test/test_cuda_stream_guard.cpp @@ -12,6 +12,7 @@ #include #include +#include #include using namespace executorch::backends::cuda; @@ -296,6 +297,30 @@ TEST(CallerStreamGuardTest, GuardSelectsThenRestores) { EXPECT_FALSE(getCallerStream().has_value()); } +TEST(CallerStreamGuardTest, ExplicitNullStreamIsStillSelected) { + CallerStreamGuard guard(nullptr); + ASSERT_TRUE(getCallerStream().has_value()); + EXPECT_EQ(*getCallerStream(), nullptr); +} + +TEST(CallerStreamGuardTest, SelectionIsThreadLocal) { + const cudaStream_t selected = fake_stream(0); + CallerStreamGuard guard(selected); + + bool child_started_without_stream = false; + bool child_selected_own_stream = false; + std::thread child([&]() { + child_started_without_stream = !getCallerStream().has_value(); + CallerStreamGuard child_guard(fake_stream(1)); + child_selected_own_stream = getCallerStream() == fake_stream(1); + }); + child.join(); + + EXPECT_TRUE(child_started_without_stream); + EXPECT_TRUE(child_selected_own_stream); + EXPECT_EQ(getCallerStream(), selected); +} + TEST(CallerStreamGuardTest, NestedGuardsRestoreOuter) { const cudaStream_t outer = fake_stream(1); const cudaStream_t inner = fake_stream(2);