From 88238a240661e45f6d2bf58e906a60ea0552f65c Mon Sep 17 00:00:00 2001 From: Ricki Hirner Date: Wed, 20 Dec 2023 14:34:04 +0100 Subject: [PATCH] ConcurrentUtilsTest: fix testRunSingle_SameKey_Parallel (bitfireAT/davx5#494) ConcurrentUtilsTest: changed testRunSingle_SameKey_Parallel to testRunSingle_SameKey_Nested --- .../bitfire/davdroid/ConcurrentUtilsTest.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/test/kotlin/at/bitfire/davdroid/ConcurrentUtilsTest.kt b/app/src/test/kotlin/at/bitfire/davdroid/ConcurrentUtilsTest.kt index c302ab04..db7af612 100644 --- a/app/src/test/kotlin/at/bitfire/davdroid/ConcurrentUtilsTest.kt +++ b/app/src/test/kotlin/at/bitfire/davdroid/ConcurrentUtilsTest.kt @@ -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() 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() - for (i in 0 until 10) - threads += Thread() { - ConcurrentUtils.runSingle(key) { - nrCalled.incrementAndGet() - Thread.sleep(100) - } - }.apply { start() } - threads.forEach { it.join() } - assertEquals(1, nrCalled.get()) + + var outerBlockExecuted = false + ConcurrentUtils.runSingle(key) { + 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") + }) + } + + assertTrue(outerBlockExecuted) } } \ No newline at end of file