1 /* 2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines 6 7 import kotlin.test.* 8 9 class DebugThreadNameTest : TestBase() { 10 @BeforeTest resetNamenull11 fun resetName() { 12 resetCoroutineId() 13 } 14 15 @Test <lambda>null16 fun testLaunchId() = runTest { 17 assertName("coroutine#1") 18 launch { 19 assertName("coroutine#2") 20 yield() 21 assertName("coroutine#2") 22 } 23 assertName("coroutine#1") 24 } 25 26 @Test <lambda>null27 fun testLaunchIdUndispatched() = runTest { 28 assertName("coroutine#1") 29 launch(start = CoroutineStart.UNDISPATCHED) { 30 assertName("coroutine#2") 31 yield() 32 assertName("coroutine#2") 33 } 34 assertName("coroutine#1") 35 } 36 37 @Test testLaunchNamenull38 fun testLaunchName() = runTest { 39 assertName("coroutine#1") 40 launch(CoroutineName("TEST")) { 41 assertName("TEST#2") 42 yield() 43 assertName("TEST#2") 44 } 45 assertName("coroutine#1") 46 } 47 48 @Test <lambda>null49 fun testWithContext() = runTest { 50 assertName("coroutine#1") 51 withContext(Dispatchers.Default) { 52 assertName("coroutine#1") 53 yield() 54 assertName("coroutine#1") 55 withContext(CoroutineName("TEST")) { 56 assertName("TEST#1") 57 yield() 58 assertName("TEST#1") 59 } 60 assertName("coroutine#1") 61 yield() 62 assertName("coroutine#1") 63 } 64 assertName("coroutine#1") 65 } 66 assertNamenull67 private fun assertName(expected: String) { 68 val name = Thread.currentThread().name 69 val split = name.split(Regex(" @")) 70 assertEquals(2, split.size, "Thread name '$name' is expected to contain one coroutine name") 71 assertEquals(expected, split[1], "Thread name '$name' is expected to end with coroutine name '$expected'") 72 } 73 }