Vsevolod Tolstopyatov | cd00643 | 2018-04-26 16:03:40 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental |
| 2 | |
| 3 | import org.junit.* |
| 4 | import java.util.concurrent.* |
| 5 | import kotlin.coroutines.experimental.* |
| 6 | |
| 7 | class DefaultExecutorStressTest : TestBase() { |
| 8 | |
| 9 | @Test |
| 10 | fun testDelay() = runTest { |
| 11 | val iterations = 100_000 * stressTestMultiplier |
| 12 | |
| 13 | val ctx = DefaultExecutor + coroutineContext |
| 14 | expect(1) |
| 15 | var expected = 1 |
| 16 | repeat(iterations) { |
| 17 | expect(++expected) |
| 18 | val deferred = async(ctx) { |
| 19 | expect(++expected) |
| 20 | val largeArray = IntArray(10_000) { it } |
| 21 | delay(Long.MAX_VALUE, TimeUnit.NANOSECONDS) |
| 22 | println(largeArray) // consume to avoid DCE, actually unreachable |
| 23 | } |
| 24 | |
| 25 | expect(++expected) |
| 26 | yield() |
| 27 | deferred.cancel() |
| 28 | try { |
| 29 | deferred.await() |
| 30 | } catch (e: JobCancellationException) { |
| 31 | expect(++expected) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | finish(2 + iterations * 4) |
| 36 | } |
| 37 | } |