aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/CancelledAwaitStressTest.kt
blob: 55c05c55b05b93306e0cb768e20991d36d54d5b9 (plain)
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
/*
 * 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.*

class CancelledAwaitStressTest : TestBase() {
    private val n = 1000 * stressTestMultiplier

    /**
     * Tests that memory does not leak from cancelled [Deferred.await]
     */
    @Test
    fun testCancelledAwait() = runTest {
        val d = async {
            delay(Long.MAX_VALUE)
        }
        repeat(n) {
            val waiter = launch(start = CoroutineStart.UNDISPATCHED) {
                val a = ByteArray(10000000) // allocate 10M of memory here
                d.await()
                keepMe(a) // make sure it is kept in state machine
            }
            waiter.cancel() // cancel await
            yield() // complete the waiter job, release its memory
        }
        d.cancel() // done test
    }

    /**
     * Tests that memory does not leak from cancelled [Job.join]
     */
    @Test
    fun testCancelledJoin() = runTest {
        val j = launch {
            delay(Long.MAX_VALUE)
        }
        repeat(n) {
            val joiner = launch(start = CoroutineStart.UNDISPATCHED) {
                val a = ByteArray(10000000) // allocate 10M of memory here
                j.join()
                keepMe(a) // make sure it is kept in state machine
            }
            joiner.cancel() // cancel join
            yield() // complete the joiner job, release its memory
        }
        j.cancel() // done test
    }

    private fun keepMe(a: ByteArray) {
        // does nothing, makes sure the variable is kept in state-machine
    }
}