Skip to content

Commit

Permalink
Fixing ParametersHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Jan 9, 2025
1 parent 36865ef commit e2a9215
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any?>) : 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.")
}
}
Original file line number Diff line number Diff line change
@@ -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<Thread>()

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.")
}

}

0 comments on commit e2a9215

Please sign in to comment.