• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 import kotlinx.coroutines.*
2 import kotlinx.coroutines.test.*
3 import kotlinx.coroutines.testing.*
4 import kotlin.test.*
5 
6 class MemoryLeakTest {
7 
8     @Test
<lambda>null9     fun testCancellationLeakInTestCoroutineScheduler() = runTest {
10         val leakingObject = Any()
11         val job = launch(start = CoroutineStart.UNDISPATCHED) {
12             delay(1)
13             // This code is needed to hold a reference to `leakingObject` until the job itself is weakly reachable.
14             leakingObject.hashCode()
15         }
16         job.cancel()
17         FieldWalker.assertReachableCount(1, testScheduler) { it === leakingObject }
18         runCurrent()
19         FieldWalker.assertReachableCount(0, testScheduler) { it === leakingObject }
20     }
21 }
22