I’m trying to run my test suite which uses koin mu...
# koin
c
I’m trying to run my test suite which uses koin multithreaded and it does not work as expected. I just want multiple koin instances to be independent from each other. this test illustrates the problem:
Copy code
@Test
    fun `multi instance koin`() {
        class User()

        val user = User()

        val myModule = module {
            single {user}
        }
        fun app()  = koinApplication { modules(myModule) }
        val koin1 = app()
        val koin2 = app()

        koin1.koin.get<User>()
        koin1.close()
        koin2.koin.get<User>()


    }
after closing koin1, koin2 no longer works
t
What’s the issue there?
c
The last statement fails
And it’s unclear to me why.
t
Well, if it fails, you get information about why it fails. And that’s the key to it. Which exception do you get?
c
Did you try to understand what this test does?
it works when i change it to this:
Copy code
@Test
fun `multi instance koin`() {
    fun module() = module { single { User() } }

    fun app() = koinApplication { modules(module()) }
    val koin1 = app()
    val koin2 = app()

    koin1.koin.get<User>()
    koin1.close()
    koin2.koin.get<User>()
}
so I think koinApplication should just copy the modules to make one application self contained.
I would have expected that modules are immutablw
t
Now I do. And you’re right. Modules are mutable and contain their loaded state.
c
i think thats really suprising and not how an api should behave. why is it that way?
t
@arnaud.giuliani maybe you could explain?
c
let me also explain why its suprising to me: I use a tdd approach and I also always run the tests in parallel. the parallel test running not only makes the tests faster, but it also shows if i rely on global state. (which is bad)
a
Koin can’t be fully isolated, as by default we are using a static context
the module declaration is filled to a variable, then not a Lazy thing
I don’t understand the case within the example here
c
its just a simplified example to show that when i close one koin server the other server that uses the same module definitions no longer works
its a mimized test case for a problem i saw when trying to run integration tests in parallel.
when i change it to always create a new copy of the module definition (by making it a function instead of a val) like the second snippet, it works
and I think its really suprising that the first example fails
a
because finally you use the same module reference
🤔