1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import org.junit.*
import org.junit.Test
import java.io.*
import java.util.concurrent.*
import kotlin.test.*
class JoinStressTest : TestBase() {
private val iterations = 50_000 * stressTestMultiplier
private val pool = newFixedThreadPoolContext(3, "JoinStressTest")
@After
fun tearDown() {
pool.close()
}
@Test
fun testExceptionalJoinWithCancellation() = runBlocking {
val results = IntArray(2)
repeat(iterations) {
val barrier = CyclicBarrier(3)
val exceptionalJob = async(pool + NonCancellable) {
barrier.await()
throw TestException()
}
val awaiterJob = async(pool) {
barrier.await()
try {
exceptionalJob.await()
} catch (e: TestException) {
0
} catch (e: CancellationException) {
1
}
}
barrier.await()
exceptionalJob.cancel()
++results[awaiterJob.await()]
}
// Check that concurrent cancellation of job which throws TestException without suspends doesn't suppress TestException
assertEquals(iterations, results[0], results.toList().toString())
assertEquals(0, results[1], results.toList().toString())
}
@Test
fun testExceptionalJoinWithMultipleCancellations() = runBlocking {
val results = IntArray(2)
var successfulCancellations = 0
repeat(iterations) {
val barrier = CyclicBarrier(4)
val exceptionalJob = async(pool + NonCancellable) {
barrier.await()
throw TestException()
}
val awaiterJob = async(pool) {
barrier.await()
try {
exceptionalJob.await()
} catch (e: TestException) {
0
} catch (e: IOException) {
1
}
}
val canceller = async(pool + NonCancellable) {
barrier.await()
exceptionalJob.cancel(IOException())
}
barrier.await()
val awaiterResult = awaiterJob.await()
val cancellerResult = canceller.await()
if (awaiterResult == 1) {
assertTrue(cancellerResult)
}
++results[awaiterResult]
if (cancellerResult) {
++successfulCancellations
}
}
assertTrue(results[0] > 0, results.toList().toString())
assertTrue(results[1] > 0, results.toList().toString())
require(successfulCancellations > 0) { "Cancellation never succeeds, something wrong with stress test infra" }
}
}
|