Koin: Wrong scoped instance in case of multiple scopes of the same qualifier

Created on 8 Nov 2019  路  3Comments  路  Source: InsertKoinIO/koin

Describe the bug
get method returns different instance of object which is registered as scoped when is called two times: before opening second scope of the same qualifier and after opening it.

To Reproduce
Run following tests:
passing will pass (which is correct behavior), failing will fail, which is - in my opinion - an evidence of bug.

class ScopeTest : AutoCloseKoinTest() {

    @Test
    fun passing() {
        startKoin {
            modules(
                module {
                    scope(named("Scope Qualifier")) {
                        scoped { MyClass() }
                    }
                }
            )
        }

        val a = getKoin().getOrCreateScope("0", named("Scope Qualifier")).get<MyClass>()
        val b = getKoin().getOrCreateScope("0", named("Scope Qualifier")).get<MyClass>()

        assertThat(a)
            .isSameAs(b)
    }

    @Test
    fun failing() {
        startKoin {
            modules(
                module {
                    scope(named("Scope Qualifier")) {
                        scoped { MyClass() }
                    }
                }
            )
        }

        val a = getKoin().getOrCreateScope("0", named("Scope Qualifier")).get<MyClass>()
        // The only different line between these two tests.
        getKoin().getOrCreateScope("1", named("Scope Qualifier")).get<MyClass>()
        val b = getKoin().getOrCreateScope("0", named("Scope Qualifier")).get<MyClass>()

        assertThat(a)
            .isSameAs(b)
    }
}

Expected behavior
In both tests, a should be the same instance as b.
However if Koin does not allow multiple scopes of the same qualifier at the same time, maybe some exception should be thrown when user tries to create new scope with the same qualifier without closing the previous one?

Koin project used and used version (please complete the following information):
koin-core and koin-test version 2.0.1

question

Most helpful comment

Hey, I won't file a new issue (unless you say so) since it is probably related to this issue but I've encountered it in a slightly different use case:

Describe the bug
Closing one scope clears other instances of the same scope.

To reproduce

@Test
fun whenFirstScopeIsClosed_thenSecondIsUntouched() {
  startKoin {
    modules(
      module {
        scope(named<Foo>()) {
          scoped<Bar>()
        }
      }
    )
  }

  val scope1 = getKoin().createScope("1", named<Foo>())
  assertThat(scope1.get<Bar>(), notNullValue())

  val scope2 = getKoin().createScope("2", named<Foo>())
  assertThat(scope2.get<Bar>(), notNullValue())

  scope1.close()
  assertThat(scope2.get<Bar>(), notNullValue())
}

throws
java.lang.IllegalStateException: Definition without any InstanceContext - [type:Scoped,scope:'KoinTest$Foo', primary_type:'KoinTest$Bar']

Expected behavior
Different instances of the same scope should not affect each other. Use case for this is when you create a scope for one activity (think of Foo as AppCompatActivity) then go to other activity, the onCreate() of the second one, where you create the new scope instance, is called after onDestroy() of the first one where you close the previous scope instance.

Koin project used and used version
koin-core koin-androidx-scope 2.0.1

All 3 comments

Hey, I won't file a new issue (unless you say so) since it is probably related to this issue but I've encountered it in a slightly different use case:

Describe the bug
Closing one scope clears other instances of the same scope.

To reproduce

@Test
fun whenFirstScopeIsClosed_thenSecondIsUntouched() {
  startKoin {
    modules(
      module {
        scope(named<Foo>()) {
          scoped<Bar>()
        }
      }
    )
  }

  val scope1 = getKoin().createScope("1", named<Foo>())
  assertThat(scope1.get<Bar>(), notNullValue())

  val scope2 = getKoin().createScope("2", named<Foo>())
  assertThat(scope2.get<Bar>(), notNullValue())

  scope1.close()
  assertThat(scope2.get<Bar>(), notNullValue())
}

throws
java.lang.IllegalStateException: Definition without any InstanceContext - [type:Scoped,scope:'KoinTest$Foo', primary_type:'KoinTest$Bar']

Expected behavior
Different instances of the same scope should not affect each other. Use case for this is when you create a scope for one activity (think of Foo as AppCompatActivity) then go to other activity, the onCreate() of the second one, where you create the new scope instance, is called after onDestroy() of the first one where you close the previous scope instance.

Koin project used and used version
koin-core koin-androidx-scope 2.0.1

Hey @pchmielowski,

pull requests #601 and #602 target both issues you describe.

I can confirm that my part of the issue is fixed in 2.1.0-alpha-7.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haroldadmin picture haroldadmin  路  3Comments

sankarsana picture sankarsana  路  4Comments

iRYO400 picture iRYO400  路  3Comments

hkelidari picture hkelidari  路  3Comments

mubarak1361 picture mubarak1361  路  3Comments