Skip to content

Commit

Permalink
Merge pull request #1905 from InsertKoinIO/instance_factory_isolation
Browse files Browse the repository at this point in the history
Instance Factory isolation fix
  • Loading branch information
arnaudgiuliani authored Jul 3, 2024
2 parents fffcc08 + 7cf7dff commit 13e0913
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 23 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Set up JDK 11
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
Expand All @@ -50,11 +50,11 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Set up JDK 11
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
Expand All @@ -73,11 +73,11 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Set up JDK 11
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Set up JDK 11
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Set up JDK 11
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Set up JDK 11
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
java-version: 17

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ abstract class InstanceFactory<T>(val beanDefinition: BeanDefinition<T>) : Locka

abstract fun dropAll()

@Suppress("NAME_SHADOWING")
override fun equals(other: Any?): Boolean {
val other = (other as? InstanceFactory<*>)?.beanDefinition
return beanDefinition == other
}

override fun hashCode(): Int {
return beanDefinition.hashCode()
}
// @Suppress("NAME_SHADOWING")
// override fun equals(other: Any?): Boolean {
// val other = (other as? InstanceFactory<*>)?.beanDefinition
// return beanDefinition == other
// }
//
// override fun hashCode(): Int {
// return beanDefinition.hashCode()
// }

companion object {
const val ERROR_SEPARATOR = "\n\t"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class InstanceRegistry(val _koin: Koin) {

private fun addAllEagerInstances(module: Module) {
module.eagerInstances.forEach { factory ->
eagerInstances[factory.hashCode()] = factory
eagerInstances[factory.beanDefinition.hashCode()] = factory
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.koin.core

import org.koin.core.annotation.KoinInternalApi
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.core.definition.IndexKey
import org.koin.core.instance.InstanceFactory
import org.koin.core.logger.Level
import org.koin.dsl.module
import org.koin.mp.KoinPlatform
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

interface SomeClassInterface
Expand Down Expand Up @@ -45,14 +50,19 @@ class OverrideAndCreateatStartTest {
stopKoin()
}

@OptIn(KoinInternalApi::class)
@Test
fun testMe() {
fun testDefinitionOverride() {
startKoin {
printLogger(Level.DEBUG)
modules(moduleA + moduleB)
}

assertTrue(count == 1)
assertTrue(created == "SomeClassB")
KoinPlatform.getKoin().instanceRegistry.instances.firstNotNullOf { (k: IndexKey,v: InstanceFactory<*>) ->
assertEquals(k, moduleB.mappings.keys.first())
assertEquals(v, moduleB.mappings.values.first())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.koin.dsl

import org.koin.Simple
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.module.Module
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

val myValModule = module { single { Simple.ComponentA() } }

val myGetModule : Module
get() = module { single { Simple.ComponentA() } }

fun myFunModule() = module { single { Simple.ComponentA() } }

@OptIn(KoinInternalApi::class)
class ModuleFactoryIsolationTest {

@Test
fun testVariableIsolationAndInstanceFactories(){
val a = myGetModule
val b = myGetModule

val aA = myValModule
val aB = myValModule

val aF = myFunModule()
val bF = myFunModule()

assertTrue(a.mappings != b.mappings)
assertEquals(a.mappings.values.first().beanDefinition, b.mappings.values.first().beanDefinition)

assertEquals(aA.mappings, aB.mappings)

assertTrue(aF.mappings != bF.mappings)
assertEquals(aF.mappings.values.first().beanDefinition, bF.mappings.values.first().beanDefinition)
}

}

0 comments on commit 13e0913

Please sign in to comment.