Skip to content

Commit ecf5c39

Browse files
authored
Make test hook setup safe across repeated runs
Summary: Repeated GTest invocations and the XCTest bridge retain the process-global hook registry, so unconditional test-provider registration fails on subsequent suite setup. Register once per process while retaining the assertion that the initial registration succeeded. Add a regression for repeated setup without changing the production one-provider contract or duplicate-provider rejection coverage. Leave the disabled Xcode-staging configuration unchanged. Differential Revision: D123082075 Pull Request resolved: pytorch#23376
1 parent a78b4da commit ecf5c39

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎extension/module/test/ptn_hooks_test.cpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,25 @@ class PtnHooksTest : public ::testing::Test {
127127
protected:
128128
static void SetUpTestSuite() {
129129
runtime::runtime_init();
130-
ASSERT_EQ(
131-
native_module::internal::register_ptn_hooks(kFakeHooks),
132-
runtime::Error::Ok);
130+
// The registry survives repeated GTest runs, including the XCTest bridge.
131+
static const auto registration_error =
132+
native_module::internal::register_ptn_hooks(kFakeHooks);
133+
ASSERT_EQ(registration_error, runtime::Error::Ok);
133134
}
134135

135136
void SetUp() override {
136137
last_source().reset();
137138
}
138139
};
139140

141+
TEST_F(PtnHooksTest, SuiteSetup_CanBeRepeated) {
142+
ASSERT_NO_FATAL_FAILURE(SetUpTestSuite());
143+
ASSERT_NO_FATAL_FAILURE(SetUpTestSuite());
144+
const auto* hooks = native_module::internal::get_ptn_hooks();
145+
ASSERT_NE(hooks, nullptr);
146+
EXPECT_EQ(hooks->load, load_fake_ptn);
147+
}
148+
140149
TEST_F(PtnHooksTest, Load_PtnSource_DispatchesMetadataAndMethodState) {
141150
const std::array<uint8_t, 2> bytes{'P', 'K'};
142151
auto loader = std::make_unique<BufferDataLoader>(bytes.data(), bytes.size());

0 commit comments

Comments
 (0)