How do you do atomic values in common code? atomic...
# multiplatform
r
How do you do atomic values in common code? atomicfu seems broken, this test does not pass
Copy code
@Test
    fun test() {
        val threadPool = newFixedThreadPoolContext(8, "TestDispatcher")
        val scope = CoroutineScope(threadPool)
        val atomic = atomic(0L)
        repeat(100_000) {
            scope.launch {
                atomic.incrementAndGet()
            }
        }
        assertEquals(100_000, atomic.value)
    }
Stately looks dead
j
you aren't waiting for the jobs to complete
r
Ah, tried to reduce a bigger use case into a test and failed miserably 😄
This works so I probably have a similar issue somewhere in the real code
Copy code
@Test
    fun test() = runBlocking {
        val threadPool = newFixedThreadPoolContext(8, "TestDispatcher")
        val scope = CoroutineScope(threadPool)
        val atomic = atomic(0L)
        val jobs = (1..100_000).map {
            scope.launch {
                atomic.incrementAndGet()
            }
        }
        jobs.joinAll()
        assertEquals(100_000, atomic.value)
    }