-
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2084 from InsertKoinIO/update_stately_collection_…
…usages Adjust collection usages
- Loading branch information
Showing
4 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ConcurrencyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.koin.core | ||
|
||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.runTest | ||
import org.koin.core.annotation.KoinInternalApi | ||
import org.koin.core.component.getScopeId | ||
import org.koin.core.context.startKoin | ||
import org.koin.core.context.stopKoin | ||
import org.koin.dsl.module | ||
import org.koin.mp.KoinPlatform | ||
import org.koin.mp.KoinPlatformTools | ||
import org.koin.mp.Lockable | ||
import org.koin.mp.generateId | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class ConcurrencyTest { | ||
data class Counter(val id : String = KoinPlatformTools.generateId()) | ||
class CounterService { | ||
|
||
private var counter = 0 | ||
private val lock = Lockable() | ||
|
||
fun increment() = KoinPlatformTools.synchronized(lock) { | ||
counter++ | ||
} | ||
|
||
fun getCounter(): Int = KoinPlatformTools.synchronized(lock) { counter } | ||
} | ||
|
||
val jobList = 100 | ||
val jobRepeat = 100 | ||
val scopeList = 10_000 | ||
|
||
@Test | ||
fun parallel_access() = runTest { | ||
|
||
startKoin { | ||
modules(module { | ||
single { CounterService() } | ||
}) | ||
} | ||
|
||
try { | ||
// Use a coroutine scope for concurrency | ||
coroutineScope { | ||
val jobs = List(jobList) { | ||
launch { | ||
repeat(jobRepeat) { | ||
KoinPlatform.getKoin().get<CounterService>().increment() | ||
} | ||
} | ||
} | ||
jobs.joinAll() | ||
} | ||
|
||
assertEquals(jobList * jobRepeat, KoinPlatform.getKoin().get<CounterService>().getCounter()) | ||
} finally { | ||
stopKoin() | ||
} | ||
} | ||
|
||
|
||
@OptIn(KoinInternalApi::class) | ||
@Test | ||
fun parallel_create_scopes() = runTest { | ||
|
||
startKoin { | ||
modules(module { | ||
scope<Counter>{ | ||
scoped { CounterService() } | ||
} | ||
}) | ||
} | ||
|
||
val result = KoinPlatformTools.safeHashMap<String,Int>() | ||
|
||
try { | ||
coroutineScope { | ||
val startScopes = List(scopeList) { | ||
launch { | ||
KoinPlatform.getKoin().apply { | ||
val counter = Counter() | ||
val scope = createScope<Counter>(counter.getScopeId()) | ||
scope.get<CounterService>().increment() | ||
result[scope.id] = scope.get<CounterService>().getCounter() | ||
scope.close() | ||
} | ||
} | ||
} | ||
startScopes.joinAll() | ||
} | ||
assertTrue(KoinPlatform.getKoin().instanceRegistry.instances.values.none { it.isCreated() }) | ||
} finally { | ||
stopKoin() | ||
} | ||
assertEquals(scopeList, result.size) | ||
assertTrue(result.values.all { it == 1 }) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters