mirror of
https://github.com/bitfireAT/davx5-ose
synced 2024-07-22 11:11:02 +00:00
ConcurrentUtilsTest: fix testRunSingle_SameKey_Parallel (bitfireAT/davx5#494)
ConcurrentUtilsTest: changed testRunSingle_SameKey_Parallel to testRunSingle_SameKey_Nested
This commit is contained in:
parent
357275fe83
commit
88238a2406
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue