All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.MapConcurrencyTest.kt Maven / Gradle / Ivy

There is a newer version: 1.5.3
Show newest version
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