From e2a9215faa407d07b9193e72675445593cafcdb4 Mon Sep 17 00:00:00 2001 From: Arnaud Giuliani Date: Thu, 9 Jan 2025 19:17:39 +0100 Subject: [PATCH] Fixing ParametersHolder --- .../koin/core/parameter/ParametersHolder.kt | 9 ++-- .../org/koin/core/ParametersHolderTest.kt | 48 +++++++++++++++++++ .../kotlin/org/koin/core/ParametersTest.kt | 25 ++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 projects/core/koin-core/src/jvmTest/kotlin/org/koin/core/ParametersTest.kt diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/parameter/ParametersHolder.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/parameter/ParametersHolder.kt index 94cdd49ee..2819afcba 100644 --- a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/parameter/ParametersHolder.kt +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/parameter/ParametersHolder.kt @@ -141,13 +141,14 @@ open class ParametersHolder( override fun toString(): String = "DefinitionParameters${_values.toList()}" override fun equals(other: Any?): Boolean { - if (other is ParametersHolder){ - return this.values == other.values - } else return false + if (this === other) return true + if (other !is ParametersHolder) return false + + return values == other.values && useIndexedValues == other.useIndexedValues } override fun hashCode(): Int { - return values.hashCode() + return 31 * values.hashCode() + (useIndexedValues?.hashCode() ?: 0) } } diff --git a/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ParametersHolderTest.kt b/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ParametersHolderTest.kt index 06aa53385..9ec04af94 100644 --- a/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ParametersHolderTest.kt +++ b/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ParametersHolderTest.kt @@ -134,4 +134,52 @@ class ParametersHolderTest { assertNotEquals(p1,p3) } + + @Test + fun `equality mutable check`() { + val p1 = parametersOf(1, 2, 3, 4) + val p2 = parametersOf(1, 2, 3, 4) + + assertEquals(p1, p2) + + p2.add(5) + + assertNotEquals(p1, p2) + } + + class DumBParam(v : ArrayList) : ParametersHolder(v) + + @Test + fun `equality check 2`() { + val p1 = DumBParam(arrayListOf(1, 2, 3, 4)) + val p2 = DumBParam(arrayListOf(1, 2, 3, 4)) + val p3 = DumBParam(arrayListOf(1, 2, 3)) + + assertEquals(p1, p2) + + assertNotEquals(p1,p3) + } + + @Test + fun `test equals considers useIndexedValues`() { + val holderWithIndexed = ParametersHolder(mutableListOf(1, 2, 3), useIndexedValues = true) + val holderWithoutIndexed = ParametersHolder(mutableListOf(1, 2, 3), useIndexedValues = false) + + // Assert they are not equal due to differing `useIndexedValues` + assertNotEquals(holderWithIndexed, holderWithoutIndexed, "ParametersHolder instances with different useIndexedValues should not be equal.") + } + + @Test + fun `test mutability affects hashCode and equality`() { + val holder = ParametersHolder(mutableListOf(1, 2, 3)) + val originalHashCode = holder.hashCode() + + // Modify the values list + holder.add(4) + // Assert hashCode changes after modification + assertNotEquals(originalHashCode, holder.hashCode(), "hashCode should reflect changes in the values list.") + // Assert equality is impacted + val holderUnmodified = ParametersHolder(mutableListOf(1, 2, 3)) + assertNotEquals(holder, holderUnmodified, "ParametersHolder should not be equal after its content is modified.") + } } diff --git a/projects/core/koin-core/src/jvmTest/kotlin/org/koin/core/ParametersTest.kt b/projects/core/koin-core/src/jvmTest/kotlin/org/koin/core/ParametersTest.kt new file mode 100644 index 000000000..23d0b713d --- /dev/null +++ b/projects/core/koin-core/src/jvmTest/kotlin/org/koin/core/ParametersTest.kt @@ -0,0 +1,25 @@ +package org.koin.core + +import org.koin.core.parameter.ParametersHolder +import kotlin.test.Test +import kotlin.test.assertEquals + +class ParametersTest { + + @Test + fun `test thread-safety in concurrent access`() { + val holder = ParametersHolder(mutableListOf(1, 2, 3)) + val threads = mutableListOf() + + for (i in 1..10) { + threads.add(Thread { + holder.add(i) + }) + } + + threads.forEach { it.start() } + threads.forEach { it.join() } + assertEquals(13, holder.size(), "ParametersHolder should contain all elements added concurrently.") + } + +} \ No newline at end of file