|
| 1 | +#include "gtest/gtest.h" |
| 2 | +#include "inspector/main_thread_interface.h" |
| 3 | +#include "inspector/worker_inspector.h" |
| 4 | +#include "node_test_fixture.h" |
| 5 | + |
| 6 | +#include <memory> |
| 7 | + |
| 8 | +namespace node { |
| 9 | +namespace inspector { |
| 10 | +namespace { |
| 11 | + |
| 12 | +class MainThreadInterfaceTest : public EnvironmentTestFixture {}; |
| 13 | + |
| 14 | +class RetainInterfaceRequest : public Request { |
| 15 | + public: |
| 16 | + explicit RetainInterfaceRequest( |
| 17 | + std::shared_ptr<MainThreadInterface>* retained_interface) |
| 18 | + : retained_interface_(retained_interface) {} |
| 19 | + |
| 20 | + void Call(MainThreadInterface* thread) override { |
| 21 | + *retained_interface_ = thread->shared_from_this(); |
| 22 | + } |
| 23 | + |
| 24 | + private: |
| 25 | + std::shared_ptr<MainThreadInterface>* retained_interface_; |
| 26 | +}; |
| 27 | + |
| 28 | +TEST_F(MainThreadInterfaceTest, ClosesHandleBeforeReleasingInterface) { |
| 29 | + const v8::HandleScope handle_scope(isolate_); |
| 30 | + const Argv argv; |
| 31 | + std::shared_ptr<MainThreadInterface> retained_interface; |
| 32 | + std::shared_ptr<MainThreadHandle> handle; |
| 33 | + { |
| 34 | + Env env{handle_scope, argv}; |
| 35 | + handle = (*env)->inspector_agent()->GetWorkerManager()->MainThread(); |
| 36 | + ASSERT_FALSE(handle->Expired()); |
| 37 | + ASSERT_TRUE(handle->Post( |
| 38 | + std::make_unique<RetainInterfaceRequest>(&retained_interface))); |
| 39 | + (*env)->RunAndClearInterrupts(); |
| 40 | + ASSERT_NE(retained_interface, nullptr); |
| 41 | + |
| 42 | + // Keep the interface alive after its inspector owner is destroyed, so |
| 43 | + // closing the handle cannot depend on the interface's destructor. |
| 44 | + } |
| 45 | + |
| 46 | + ASSERT_TRUE(handle->Expired()); |
| 47 | + EXPECT_FALSE(handle->Post( |
| 48 | + std::make_unique<RetainInterfaceRequest>(&retained_interface))); |
| 49 | + retained_interface.reset(); |
| 50 | +} |
| 51 | + |
| 52 | +} // namespace |
| 53 | +} // namespace inspector |
| 54 | +} // namespace node |
0 commit comments