commonTest.MapConcurrencyTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-collections-atomic Show documentation
Show all versions of kotlinx-collections-atomic Show documentation
A Kotlin Multiplatform Mutable Collections that are thread safe
import expect.*
import kotlinx.collections.atomic.mutableAtomicMapOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runTest
import kotlinx.coroutines.withContext
import kotlin.test.Test
class MapConcurrencyTest {
val numbers: MutableMap = mutableAtomicMapOf()
@Test
fun should_add_items_to_list() {
numbers["one"] = 1
}
@Test
fun should_add_items_in_different_threads() = runTest {
val chars: MutableMap = mutableAtomicMapOf("A" to 'A')
withContext(Dispatchers.Default) {
chars["B"] = 'B'
}
withContext(Dispatchers.Unconfined) {
chars["C"] = 'C'
}
expect(chars.values).toContain('A', 'B', 'C')
}
@Test
fun should_add_multiple_items_in_different_threads() = runTest {
val chars: MutableMap = mutableAtomicMapOf("A" to 'A')
withContext(Dispatchers.Default) {
chars.putAll(listOf("B" to 'B'))
}
withContext(Dispatchers.Unconfined) {
chars.putAll(listOf("C" to 'C'))
}
expect(chars.values).toContain('A', 'B', 'C')
}
@Test
fun should_remove_items_in_any_thread() = runTest {
val chars: MutableMap = mutableAtomicMapOf("A" to 'A', "B" to 'B', "C" to 'C')
withContext(Dispatchers.Default) {
chars.remove("B")
}
withContext(Dispatchers.Unconfined) {
chars.remove("C")
}
expect(chars.values).toContain('A')
expect(chars.values).toBeOfSize(1)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy