• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
5 package kotlinx.coroutines.debug
6 
7 import com.google.gson.*
8 import kotlinx.coroutines.*
9 import kotlinx.coroutines.debug.internal.*
10 import org.junit.Test
11 import kotlin.test.*
12 
13 class EnhanceStackTraceWithTreadDumpAsJsonTest : DebugTestBase() {
14     private data class StackTraceElementInfoFromJson(
15         val declaringClass: String,
16         val methodName: String,
17         val fileName: String?,
18         val lineNumber: Int
19     )
20 
21     @Test
<lambda>null22     fun testEnhancedStackTraceFormatWithDeferred() = runTest {
23         val deferred = async {
24             suspendingMethod()
25             assertTrue(true)
26         }
27         yield()
28 
29         val coroutineInfo = DebugProbesImpl.dumpCoroutinesInfo()
30         assertEquals(coroutineInfo.size, 2)
31         val info = coroutineInfo[1]
32         val enhancedStackTraceAsJsonString = DebugProbesImpl.enhanceStackTraceWithThreadDumpAsJson(info)
33         val enhancedStackTraceFromJson = Gson().fromJson(enhancedStackTraceAsJsonString, Array<StackTraceElementInfoFromJson>::class.java)
34         val enhancedStackTrace = DebugProbesImpl.enhanceStackTraceWithThreadDump(info, info.lastObservedStackTrace)
35         assertEquals(enhancedStackTrace.size, enhancedStackTraceFromJson.size)
36         for ((frame, frameFromJson) in enhancedStackTrace.zip(enhancedStackTraceFromJson)) {
37             assertEquals(frame.className, frameFromJson.declaringClass)
38             assertEquals(frame.methodName, frameFromJson.methodName)
39             assertEquals(frame.fileName, frameFromJson.fileName)
40             assertEquals(frame.lineNumber, frameFromJson.lineNumber)
41         }
42 
43         deferred.cancelAndJoin()
44     }
45 
suspendingMethodnull46     private suspend fun suspendingMethod() {
47         delay(Long.MAX_VALUE)
48     }
49 }
50