Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 88238a24 authored by Ricki Hirner's avatar Ricki Hirner
Browse files

ConcurrentUtilsTest: fix testRunSingle_SameKey_Parallel (bitfireAT/davx5#494)

ConcurrentUtilsTest: changed testRunSingle_SameKey_Parallel to testRunSingle_SameKey_Nested
parent 357275fe
Loading
Loading
Loading
Loading
+19 −14
Original line number Diff line number Diff line
@@ -6,8 +6,12 @@ package at.bitfire.davdroid

import at.bitfire.davdroid.util.ConcurrentUtils
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Test
import java.util.concurrent.atomic.AtomicInteger
import kotlin.concurrent.thread

class ConcurrentUtilsTest {

@@ -24,12 +28,12 @@ class ConcurrentUtilsTest {
        var nrCalled = AtomicInteger()
        val threads = mutableListOf<Thread>()
        for (i in 0 until 10)
            threads += Thread() {
            threads += thread {
                ConcurrentUtils.runSingle(i) {
                    nrCalled.incrementAndGet()
                    Thread.sleep(100)
                }
            }.apply { start() }
            }
        threads.forEach { it.join() }
        assertEquals(10, nrCalled.get())
    }
@@ -44,19 +48,20 @@ class ConcurrentUtilsTest {
    }

    @Test
    fun testRunSingle_SameKey_Parallel() {
    fun testRunSingle_SameKey_Nested() {
        val key = "a"
        val nrCalled = AtomicInteger()
        val threads = mutableListOf<Thread>()
        for (i in 0 until 10)
            threads += Thread() {

        var outerBlockExecuted = false
        ConcurrentUtils.runSingle(key) {
                    nrCalled.incrementAndGet()
                    Thread.sleep(100)
            outerBlockExecuted = true

            // Now a code block with the key is already running, further ones should be ignored
            assertFalse(ConcurrentUtils.runSingle(key) {
                fail("Shouldn't have been called")
            })
        }
            }.apply { start() }
        threads.forEach { it.join() }
        assertEquals(1, nrCalled.get())

        assertTrue(outerBlockExecuted)
    }

}
 No newline at end of file