Skip to content

Conversation

@mgulbronson
Copy link
Contributor

@mgulbronson mgulbronson commented Oct 27, 2025

Description

Currently if the injector is not reused no test fixtures are reset, presumably because it's assumed the test fixtures are scoped to the injector and will therefore be reconstructed for each test when the injector is created. But test fixtures that are Kotlin objects aren't scoped to the injector, they're scoped to the JVM classloader and therefore persist across tests/injectors. So if the injector is not reused, those test fixtures never get reset.

Practically this means that binding a test fixture doesn't do anything. This PR makes it so all test fixtures are always reset, making it possible to implement test fixtures as objects.


Alternative approach that I did originally was to reflect on the test fixtures to check if they're an object or not. For test fixtures that aren't objects no change from the current behaviour is needed. For test fixtures that are objects they need to be reset every time (regardless of whether the injector is being reused). But realistically the cost of reflecting over the test fixtures is more expensive than just resetting all test fixtures.

@mgulbronson mgulbronson force-pushed the mgulbronson/always-reset-object-test-fixtures branch from 44aa21a to cb9baba Compare October 29, 2025 01:25
@mgulbronson mgulbronson changed the title [proposal] Always reset object test fixtures [proposal] Always reset all test fixtures Oct 29, 2025
@adrw adrw requested a review from mmollaverdi October 29, 2025 17:27
Copy link
Collaborator

@adrw adrw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, going to wait for one more review and then merge.

@mmollaverdi
Copy link
Collaborator

But test fixtures that are Kotlin objects aren't scoped to the injector, they're scoped to the JVM classloader and therefore persist across tests/injectors. So if the injector is not reused, those test fixtures never get reset.

I might be misunderstanding this, but testFixtures.reset() only reset what's scoped to the an injector, because testFixtures is an @Injected value. So the change in this PR won't change that behaviour.

Curious though, are you leveraging misk test injector reuse to speed up your tests?

@mgulbronson
Copy link
Contributor Author

But test fixtures that are Kotlin objects aren't scoped to the injector, they're scoped to the JVM classloader and therefore persist across tests/injectors. So if the injector is not reused, those test fixtures never get reset.

I might be misunderstanding this, but testFixtures.reset() only reset what's scoped to the an injector, because testFixtures is an @Injected value. So the change in this PR won't change that behaviour.

@mmollaverdi I think if you use instance bindings then it'll have an effect. For instance if I have

object FakeFoo : Foo, TestFixture

multibind<TestFixtures>().toInstance(FakeFoo)

You're right that the binding itself is scoped to the injector, but the actual thing it's injecting (FakeFoo) is not. So in this case, injecting a Set<TestFixture> would inject the singleton Foo (which may have state from previous test runs). Before my change we wouldn't reset it unless we're re-using the injector, now we will. Let me know if that makes sense

Curious though, are you leveraging misk test injector reuse to speed up your tests?

We do not — I'm not sure exactly why as I wasn't around for our initial setup but IMO it's confusing regardless that object test fixtures don't properly get reset

@mmollaverdi
Copy link
Collaborator

Makes sense

@mgulbronson
Copy link
Contributor Author

@adrw or @mmollaverdi either of you able to approve the workflow so we can merge this thing? much appreciated for your help with this PR! 🙏

@adrw adrw enabled auto-merge November 11, 2025 19:48
@adrw
Copy link
Collaborator

adrw commented Dec 4, 2025

@mgulbronson Apologies for the delay, I thought this had already merged, I'll try and shepherd it through today.

@adrw adrw disabled auto-merge December 5, 2025 19:51
@adrw
Copy link
Collaborator

adrw commented Dec 17, 2025

@mgulbronson Not sure why but some test seems to keep timing out in the testShardNonHibernate, I've triggered a rebuild a few times so it might not be a test flake but a regression from your code change. Do you mind taking a look?

@mgulbronson
Copy link
Contributor Author

@mgulbronson Not sure why but some test seems to keep timing out in the testShardNonHibernate, I've triggered a rebuild a few times so it might not be a test flake but a regression from your code change. Do you mind taking a look?

@adrw I think I see the issue — will try to get a fix up today but may wait until after the holidays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants