From 743e2912da82868e0c3e4816d60dde0a61d12930 Mon Sep 17 00:00:00 2001 From: Arnaud Giuliani Date: Wed, 3 Jul 2024 17:25:14 +0200 Subject: [PATCH 1/2] Instance Factory isolation fix --- .../org/koin/core/instance/InstanceFactory.kt | 18 ++++----- .../koin/core/registry/InstanceRegistry.kt | 2 +- .../koin/core/OverrideAndCreateatStartTest.kt | 12 +++++- .../koin/dsl/ModuleFactoryIsolationTest.kt | 40 +++++++++++++++++++ 4 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleFactoryIsolationTest.kt diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/instance/InstanceFactory.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/instance/InstanceFactory.kt index 82dd3d928..944d709cd 100644 --- a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/instance/InstanceFactory.kt +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/instance/InstanceFactory.kt @@ -70,15 +70,15 @@ abstract class InstanceFactory(val beanDefinition: BeanDefinition) : 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" diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/InstanceRegistry.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/InstanceRegistry.kt index c7c64b86a..7bd1d43ac 100644 --- a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/InstanceRegistry.kt +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/InstanceRegistry.kt @@ -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 } } diff --git a/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/OverrideAndCreateatStartTest.kt b/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/OverrideAndCreateatStartTest.kt index e675c53eb..45fd1ffa6 100644 --- a/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/OverrideAndCreateatStartTest.kt +++ b/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/OverrideAndCreateatStartTest.kt @@ -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 @@ -45,8 +50,9 @@ class OverrideAndCreateatStartTest { stopKoin() } + @OptIn(KoinInternalApi::class) @Test - fun testMe() { + fun testDefinitionOverride() { startKoin { printLogger(Level.DEBUG) modules(moduleA + moduleB) @@ -54,5 +60,9 @@ class OverrideAndCreateatStartTest { 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()) + } } } diff --git a/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleFactoryIsolationTest.kt b/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleFactoryIsolationTest.kt new file mode 100644 index 000000000..943ae5f0b --- /dev/null +++ b/projects/core/koin-core/src/commonTest/kotlin/org/koin/dsl/ModuleFactoryIsolationTest.kt @@ -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) + } + +} \ No newline at end of file From 7cf7dffb3e2cba1e9a3222b2bb35c59762617f43 Mon Sep 17 00:00:00 2001 From: Arnaud Giuliani Date: Wed, 3 Jul 2024 17:34:24 +0200 Subject: [PATCH 2/2] upgrade to zulu 17 GH --- .github/workflows/build.yml | 12 ++++++------ .github/workflows/release.yml | 4 ++-- .github/workflows/test.yml | 4 ++-- .github/workflows/weekly.yml | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3286b4d0f..34b80cf07 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 @@ -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 @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fbf84e94f..76c6b90e2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 272fa57b2..b2918235a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/.github/workflows/weekly.yml b/.github/workflows/weekly.yml index 45764c52e..b6125baf7 100644 --- a/.github/workflows/weekly.yml +++ b/.github/workflows/weekly.yml @@ -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