ConcurrentUtilsTest: fix testRunSingle_SameKey_Parallel (bitfireAT/davx5#494)

ConcurrentUtilsTest: changed testRunSingle_SameKey_Parallel to testRunSingle_SameKey_Nested
This commit is contained in:
Ricki Hirner 2023-12-20 14:34:04 +01:00
parent 357275fe83
commit 88238a2406
No known key found for this signature in database
GPG key ID: 79A019FCAAEDD3AA

View file

@ -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() {
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)
}
}