From 04a2a2d90d47dab2bba48c67161e52631bd1c39a Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Wed, 8 Nov 2023 11:47:43 +0100 Subject: [PATCH] [K/N][tests] Migrate termination runtime tests to new testing infra ^KT-61259 --- .../backend.native/tests/build.gradle | 105 ------------------ .../tests/runtime/basic/assert_failed.kt | 9 -- .../tests/runtime/basic/assert_passed.kt | 9 -- .../tests/runtime/basic/exit.kt | 12 -- .../tests/runtime/basic/main_exception.kt | 12 -- .../tests/runtime/exceptions/custom_hook.kt | 49 -------- .../exceptions/custom_hook_memory_leak.kt | 22 ---- .../exceptions/custom_hook_no_reset.kt | 20 ---- .../exceptions/custom_hook_terminate.kt | 18 --- ...stom_hook_terminate_unhandled_exception.kt | 20 ---- .../runtime/exceptions/custom_hook_throws.kt | 20 ---- .../custom_hook_unhandled_exception.kt | 20 ---- .../exceptions/exception_in_global_init.kt | 12 -- .../tests/runtime/exceptions/terminate.kt | 12 -- .../runtime/exceptions/unhandled_exception.kt | 12 -- .../standalone/termination/assertFailed.kt | 8 ++ .../standalone/termination/assertPassed.kt | 5 + .../standalone/termination/exitProcess.kt | 10 ++ .../standalone/termination/exitProcess.out | 1 + .../standalone/termination/globalThrow.kt | 8 ++ .../standalone/termination/mainThrow.kt | 6 + .../termination/processUnhandledException.kt | 7 ++ .../terminateWithUnhandledException.kt | 7 ++ .../unhandledExceptionHookClosure.kt | 16 +++ .../termination/unhandledExceptionHookFun.kt | 18 +++ .../termination/unhandledExceptionHookGet.kt | 15 +-- .../unhandledExceptionHookTerminate.kt | 14 +++ ...andledExceptionHookTerminateWithProcess.kt | 15 +++ .../unhandledExceptionHookThrow.kt | 14 +++ .../unhandledExceptionHookWithProcess.kt | 18 +++ .../unhandledExceptionHookWithProcess.out | 0 .../FirNativeStandaloneTestGenerated.java | 99 +++++++++++++++++ .../NativeStandaloneTestGenerated.java | 97 ++++++++++++++++ 33 files changed, 347 insertions(+), 363 deletions(-) delete mode 100644 kotlin-native/backend.native/tests/runtime/basic/assert_failed.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/basic/assert_passed.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/basic/exit.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/basic/main_exception.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_memory_leak.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_no_reset.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate_unhandled_exception.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_throws.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_unhandled_exception.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/exception_in_global_init.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/terminate.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/exceptions/unhandled_exception.kt create mode 100644 native/native.tests/testData/standalone/termination/assertFailed.kt create mode 100644 native/native.tests/testData/standalone/termination/assertPassed.kt create mode 100644 native/native.tests/testData/standalone/termination/exitProcess.kt create mode 100644 native/native.tests/testData/standalone/termination/exitProcess.out create mode 100644 native/native.tests/testData/standalone/termination/globalThrow.kt create mode 100644 native/native.tests/testData/standalone/termination/mainThrow.kt create mode 100644 native/native.tests/testData/standalone/termination/processUnhandledException.kt create mode 100644 native/native.tests/testData/standalone/termination/terminateWithUnhandledException.kt create mode 100644 native/native.tests/testData/standalone/termination/unhandledExceptionHookClosure.kt create mode 100644 native/native.tests/testData/standalone/termination/unhandledExceptionHookFun.kt rename kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt => native/native.tests/testData/standalone/termination/unhandledExceptionHookGet.kt (54%) create mode 100644 native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminate.kt create mode 100644 native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminateWithProcess.kt create mode 100644 native/native.tests/testData/standalone/termination/unhandledExceptionHookThrow.kt create mode 100644 native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.kt rename kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_unhandled_exception.out => native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.out (100%) diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index fba7d159584..050fee1b5b3 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -314,11 +314,6 @@ standaloneTest("runtime_basic_init") { expectedExitStatus = 0 } -standaloneTest("runtime_basic_exit") { - source = "runtime/basic/exit.kt" - expectedExitStatus = 42 -} - standaloneTest('enumEquals') { useGoldenData = true source = "runtime/basic/enum_equals.kt" @@ -833,70 +828,6 @@ standaloneTest("throw_from_except_constr") { source = "runtime/exceptions/throw_from_except_constr.kt" } -standaloneTest("custom_hook") { - outputChecker = { - it.contains("value 42: Error. Runnable state: true") && it.contains("Uncaught Kotlin exception: kotlin.Error: an error") - } - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/custom_hook.kt" - flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative'] -} - -standaloneTest("custom_hook_memory_leak") { - outputChecker = { - it.contains("Hook 42") && it.contains("Uncaught Kotlin exception: kotlin.Error: an error") - } - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/custom_hook_memory_leak.kt" -} - -standaloneTest("custom_hook_get") { - source = "runtime/exceptions/custom_hook_get.kt" -} - -standaloneTest("custom_hook_no_reset") { - outputChecker = { - it.contains("Hook called") && it.contains("Uncaught Kotlin exception: kotlin.Error: some error") - } - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/custom_hook_no_reset.kt" -} - -standaloneTest("custom_hook_throws") { - outputChecker = { - it.contains("Hook called") && it.contains("Uncaught Kotlin exception: kotlin.Error: another error") && !it.contains("some error") - } - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/custom_hook_throws.kt" -} - -standaloneTest("custom_hook_unhandled_exception") { - useGoldenData = true - source = "runtime/exceptions/custom_hook_unhandled_exception.kt" -} - -standaloneTest("custom_hook_terminate") { - outputChecker = { - it.contains("Hook called") - } - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/custom_hook_terminate.kt" -} - -standaloneTest("custom_hook_terminate_unhandled_exception") { - outputChecker = { - it.contains("Hook called") && it.contains("Uncaught Kotlin exception: kotlin.Error: some error") && !it.contains("Not going to happen") - } - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/custom_hook_terminate_unhandled_exception.kt" -} - -standaloneTest("exception_in_global_init") { - source = "runtime/exceptions/exception_in_global_init.kt" - expectedExitStatusChecker = { it != 0 } - outputChecker = { s -> s.contains("Uncaught Kotlin exception:") && s.contains("FAIL") && !s.contains("in kotlin main") } -} - tasks.register("rethrow_exception", KonanLocalTest) { source = "runtime/exceptions/rethrow.kt" } @@ -905,42 +836,6 @@ tasks.register("throw_from_catch", KonanLocalTest) { source = "runtime/exceptions/throw_from_catch.kt" } -standaloneTest("terminate") { - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/terminate.kt" -} - -standaloneTest("unhandled_exception") { - outputChecker = { - it.contains("Uncaught Kotlin exception: kotlin.Error: some error") - } - expectedExitStatusChecker = { it != 0 } - source = "runtime/exceptions/unhandled_exception.kt" -} - - -tasks.register("main_exception", KonanLocalTest) { - outputChecker = { s -> s.contains("kotlin.Error: Hello!") } - expectedExitStatus = 1 - source = "runtime/basic/main_exception.kt" -} - -standaloneTest("runtime_basic_assert_failed") { - expectedExitStatusChecker = { it != 0 } - source = "runtime/basic/assert_failed.kt" -} - -standaloneTest("runtime_basic_assert_passed") { - expectedExitStatus = 0 - source = "runtime/basic/assert_passed.kt" -} - -standaloneTest("runtime_basic_assert_disabled") { - expectedExitStatus = 0 - enableKonanAssertions = false - source = "runtime/basic/assert_failed.kt" -} - tasks.register("initializers0", KonanLocalTest) { useGoldenData = true source = "runtime/basic/initializers0.kt" diff --git a/kotlin-native/backend.native/tests/runtime/basic/assert_failed.kt b/kotlin-native/backend.native/tests/runtime/basic/assert_failed.kt deleted file mode 100644 index becb0e81d67..00000000000 --- a/kotlin-native/backend.native/tests/runtime/basic/assert_failed.kt +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) - -fun main(args: Array) { - assert(false) -} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/runtime/basic/assert_passed.kt b/kotlin-native/backend.native/tests/runtime/basic/assert_passed.kt deleted file mode 100644 index 9dca874b92a..00000000000 --- a/kotlin-native/backend.native/tests/runtime/basic/assert_passed.kt +++ /dev/null @@ -1,9 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) - -fun main(args: Array) { - assert(true) -} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/runtime/basic/exit.kt b/kotlin-native/backend.native/tests/runtime/basic/exit.kt deleted file mode 100644 index e32bae61f8e..00000000000 --- a/kotlin-native/backend.native/tests/runtime/basic/exit.kt +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ - -import kotlin.system.* - -fun main(args: Array) { - exitProcess(42) - @Suppress("UNREACHABLE_CODE") - throw RuntimeException("Exit function call returned normally") -} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/runtime/basic/main_exception.kt b/kotlin-native/backend.native/tests/runtime/basic/main_exception.kt deleted file mode 100644 index 78a20406592..00000000000 --- a/kotlin-native/backend.native/tests/runtime/basic/main_exception.kt +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ - -package runtime.basic.main_exception - -import kotlin.test.* - -@Test fun runTest() { - throw Error("Hello!") -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt deleted file mode 100644 index b2498a8d0e9..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook.kt +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class) - -import kotlin.test.* - -import kotlin.native.concurrent.* -import kotlin.native.internal.* -import kotlin.native.runtime.Debugging - -fun mainLegacyMM() { - val wrong = "wrong" - assertFailsWith { - setUnhandledExceptionHook { _ -> println(wrong) } - } - - val x = 42 - val old = setUnhandledExceptionHook({ throwable: Throwable -> - println("value $x: ${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}") - }.freeze()) - - assertNull(old) - - throw Error("an error") -} - -fun mainExperimentalMM() { - val unset = setUnhandledExceptionHook { _ -> println("ok") } - assertNull(unset) - - val x = 42 - val old = setUnhandledExceptionHook { throwable: Throwable -> - println("value $x: ${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}") - } - - assertNotNull(old) - - throw Error("an error") -} - -fun main() { - if (Platform.memoryModel == MemoryModel.EXPERIMENTAL) { - mainExperimentalMM() - } else { - mainLegacyMM() - } -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_memory_leak.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_memory_leak.kt deleted file mode 100644 index eccd017a267..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_memory_leak.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class) - -import kotlin.test.* - -import kotlin.native.concurrent.* - -data class C(val x: Int) - -fun main() { - Platform.isMemoryLeakCheckerActive = true - - val c = C(42) - setUnhandledExceptionHook({ _: Throwable -> - println("Hook ${c.x}") - }.freeze()) - - throw Error("an error") -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_no_reset.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_no_reset.kt deleted file mode 100644 index 50b625987e4..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_no_reset.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, FreezingIsDeprecated::class) - -import kotlin.test.* - -import kotlin.native.concurrent.* - -fun customExceptionHook(throwable: Throwable) { - println("Hook called") - assertEquals(::customExceptionHook, getUnhandledExceptionHook()) -} - -fun main() { - setUnhandledExceptionHook((::customExceptionHook).freeze()) - - throw Error("some error") -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate.kt deleted file mode 100644 index b6488fe2c79..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate.kt +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, FreezingIsDeprecated::class) - -import kotlin.test.* - -import kotlin.native.concurrent.* - -fun main() { - setUnhandledExceptionHook({ t: Throwable -> - println("Hook called") - terminateWithUnhandledException(t) - }.freeze()) - - throw Error("some error") -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate_unhandled_exception.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate_unhandled_exception.kt deleted file mode 100644 index 351f5125a06..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_terminate_unhandled_exception.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, FreezingIsDeprecated::class) - -import kotlin.test.* - -import kotlin.native.concurrent.* - -fun main() { - setUnhandledExceptionHook({ t: Throwable -> - println("Hook called") - terminateWithUnhandledException(t) - }.freeze()) - - val exception = Error("some error") - processUnhandledException(exception) - println("Not going to happen") -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_throws.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_throws.kt deleted file mode 100644 index f30a2cb5324..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_throws.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class) - -import kotlin.test.* - -import kotlin.native.concurrent.* - -fun customExceptionHook(throwable: Throwable) { - println("Hook called") - throw Error("another error") -} - -fun main() { - setUnhandledExceptionHook((::customExceptionHook).freeze()) - - throw Error("some error") -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_unhandled_exception.kt b/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_unhandled_exception.kt deleted file mode 100644 index 3f9f6690aca..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_unhandled_exception.kt +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, FreezingIsDeprecated::class) - -import kotlin.test.* -import kotlin.concurrent.AtomicInt -import kotlin.native.concurrent.* - -fun main() { - val called = AtomicInt(0) - setUnhandledExceptionHook({ _: Throwable -> - called.value = 1 - }.freeze()) - - val exception = Error("some error") - processUnhandledException(exception) - assertEquals(1, called.value) -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/exception_in_global_init.kt b/kotlin-native/backend.native/tests/runtime/exceptions/exception_in_global_init.kt deleted file mode 100644 index 0e67064b833..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/exception_in_global_init.kt +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ - -import kotlin.test.* - -val p: Nothing = error("FAIL") - -fun main() { - println("in kotlin main") -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/terminate.kt b/kotlin-native/backend.native/tests/runtime/exceptions/terminate.kt deleted file mode 100644 index fc48edfd8ca..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/terminate.kt +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) - -import kotlin.test.* - -fun main() { - val exception = Error("some error") - terminateWithUnhandledException(exception) -} diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/unhandled_exception.kt b/kotlin-native/backend.native/tests/runtime/exceptions/unhandled_exception.kt deleted file mode 100644 index cf0c135c85b..00000000000 --- a/kotlin-native/backend.native/tests/runtime/exceptions/unhandled_exception.kt +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) - -import kotlin.test.* - -fun main() { - val exception = Error("some error") - processUnhandledException(exception) -} diff --git a/native/native.tests/testData/standalone/termination/assertFailed.kt b/native/native.tests/testData/standalone/termination/assertFailed.kt new file mode 100644 index 00000000000..b98578e847e --- /dev/null +++ b/native/native.tests/testData/standalone/termination/assertFailed.kt @@ -0,0 +1,8 @@ +// EXIT_CODE: !0 +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +// TODO: Test running this test with disabled assertions. This requires removing always-enabled +// -enable-assertions from `BasicCompilation`. +fun main() { + assert(false) +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/assertPassed.kt b/native/native.tests/testData/standalone/termination/assertPassed.kt new file mode 100644 index 00000000000..e05888b3943 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/assertPassed.kt @@ -0,0 +1,5 @@ +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +fun main() { + assert(true) +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/exitProcess.kt b/native/native.tests/testData/standalone/termination/exitProcess.kt new file mode 100644 index 00000000000..97b90d9e420 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/exitProcess.kt @@ -0,0 +1,10 @@ +// EXIT_CODE: 42 +// OUTPUT_DATA_FILE: exitProcess.out + +import kotlin.system.* + +fun main() { + print("OK") + exitProcess(42) + println("FAIL") +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/exitProcess.out b/native/native.tests/testData/standalone/termination/exitProcess.out new file mode 100644 index 00000000000..a0aba9318ad --- /dev/null +++ b/native/native.tests/testData/standalone/termination/exitProcess.out @@ -0,0 +1 @@ +OK \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/globalThrow.kt b/native/native.tests/testData/standalone/termination/globalThrow.kt new file mode 100644 index 00000000000..8835c08832e --- /dev/null +++ b/native/native.tests/testData/standalone/termination/globalThrow.kt @@ -0,0 +1,8 @@ +// EXIT_CODE: !0 +// OUTPUT_REGEX: Uncaught Kotlin exception: kotlin\.Error: an error\n(?!.*FAIL.*).* + +val p: Nothing = throw Error("an error") + +fun main() { + println("FAIL") +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/mainThrow.kt b/native/native.tests/testData/standalone/termination/mainThrow.kt new file mode 100644 index 00000000000..515afdec5a8 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/mainThrow.kt @@ -0,0 +1,6 @@ +// EXIT_CODE: !0 +// OUTPUT_REGEX: Uncaught Kotlin exception: kotlin\.Error: an error\n.* + +fun main() { + throw Error("an error") +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/processUnhandledException.kt b/native/native.tests/testData/standalone/termination/processUnhandledException.kt new file mode 100644 index 00000000000..dbbadc07d69 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/processUnhandledException.kt @@ -0,0 +1,7 @@ +// EXIT_CODE: !0 +// OUTPUT_REGEX: Uncaught Kotlin exception: kotlin\.Error: an error\n.* +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +fun main() { + processUnhandledException(Error("an error")) +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/terminateWithUnhandledException.kt b/native/native.tests/testData/standalone/termination/terminateWithUnhandledException.kt new file mode 100644 index 00000000000..a5aaa7a4951 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/terminateWithUnhandledException.kt @@ -0,0 +1,7 @@ +// EXIT_CODE: !0 +// OUTPUT_REGEX: Uncaught Kotlin exception: kotlin\.Error: an error\n.* +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +fun main() { + terminateWithUnhandledException(Error("an error")) +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/unhandledExceptionHookClosure.kt b/native/native.tests/testData/standalone/termination/unhandledExceptionHookClosure.kt new file mode 100644 index 00000000000..b2163e02791 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/unhandledExceptionHookClosure.kt @@ -0,0 +1,16 @@ +// EXIT_CODE: !0 +// FREE_COMPILER_ARGS: -opt-in=kotlin.native.internal.InternalForKotlinNative +// OUTPUT_REGEX: value 42: Error\. Runnable state: true\nUncaught Kotlin exception: kotlin\.Error: an error\n.* +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class) + +import kotlin.native.runtime.Debugging +import kotlin.test.* + +fun main() { + val x = 42 + setUnhandledExceptionHook { throwable: Throwable -> + println("value $x: ${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}") + } + + throw Error("an error") +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/unhandledExceptionHookFun.kt b/native/native.tests/testData/standalone/termination/unhandledExceptionHookFun.kt new file mode 100644 index 00000000000..e0b9eff8033 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/unhandledExceptionHookFun.kt @@ -0,0 +1,18 @@ +// EXIT_CODE: !0 +// FREE_COMPILER_ARGS: -opt-in=kotlin.native.internal.InternalForKotlinNative +// OUTPUT_REGEX: value true: Error\. Runnable state: true\nUncaught Kotlin exception: kotlin\.Error: an error\n.* +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class) + +import kotlin.native.runtime.Debugging +import kotlin.test.* + +fun customExceptionHook(throwable: Throwable) { + println("value ${getUnhandledExceptionHook() == ::customExceptionHook}: ${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}") +} + +fun main() { + val x = 42 + setUnhandledExceptionHook(::customExceptionHook) + + throw Error("an error") +} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt b/native/native.tests/testData/standalone/termination/unhandledExceptionHookGet.kt similarity index 54% rename from kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt rename to native/native.tests/testData/standalone/termination/unhandledExceptionHookGet.kt index c32a2aff635..35690fe177c 100644 --- a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_get.kt +++ b/native/native.tests/testData/standalone/termination/unhandledExceptionHookGet.kt @@ -1,17 +1,10 @@ -/* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ -@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, FreezingIsDeprecated::class) +// No termination is going on here. But that's the closest location to other unhandled exception hook tests. +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) import kotlin.test.* -import kotlin.native.concurrent.* - fun main() { - val exceptionHook = { _: Throwable -> - println("Hook") - }.freeze() + val exceptionHook = { _: Throwable -> Unit } val oldHook = setUnhandledExceptionHook(exceptionHook) assertNull(oldHook) @@ -23,4 +16,4 @@ fun main() { assertEquals(exceptionHook, hook3) val hook4 = getUnhandledExceptionHook() assertNull(hook4) -} +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminate.kt b/native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminate.kt new file mode 100644 index 00000000000..77cf1cfe519 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminate.kt @@ -0,0 +1,14 @@ +// EXIT_CODE: !0 +// OUTPUT_REGEX: Hook\nUncaught Kotlin exception: kotlin\.Error: an error\n.* +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +import kotlin.test.* + +fun main() { + setUnhandledExceptionHook { + println("Hook") + terminateWithUnhandledException(it) + } + + throw Error("an error") +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminateWithProcess.kt b/native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminateWithProcess.kt new file mode 100644 index 00000000000..0f466f20696 --- /dev/null +++ b/native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminateWithProcess.kt @@ -0,0 +1,15 @@ +// EXIT_CODE: !0 +// OUTPUT_REGEX: Hook\nUncaught Kotlin exception: kotlin\.Error: an error\n(?!.*FAIL.*).* +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +import kotlin.test.* + +fun main() { + setUnhandledExceptionHook { + println("Hook") + terminateWithUnhandledException(it) + } + + processUnhandledException(Error("an error")) + println("FAIL") +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/unhandledExceptionHookThrow.kt b/native/native.tests/testData/standalone/termination/unhandledExceptionHookThrow.kt new file mode 100644 index 00000000000..5cd908b244d --- /dev/null +++ b/native/native.tests/testData/standalone/termination/unhandledExceptionHookThrow.kt @@ -0,0 +1,14 @@ +// EXIT_CODE: !0 +// OUTPUT_REGEX: Hook\nUncaught Kotlin exception: kotlin\.Error: another error\n(?!.*an error.*).* +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +import kotlin.test.* + +fun main() { + setUnhandledExceptionHook { + println("Hook") + throw Error("another error") + } + + throw Error("an error") +} \ No newline at end of file diff --git a/native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.kt b/native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.kt new file mode 100644 index 00000000000..cb60b2e9ddb --- /dev/null +++ b/native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.kt @@ -0,0 +1,18 @@ +// No termination is going on here. But that's the closest location to other unhandled exception hook tests. +// OUTPUT_DATA_FILE: unhandledExceptionHookWithProcess.out +@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class) + +import kotlin.concurrent.AtomicInt +import kotlin.test.* + +fun main() { + val exception = Error("an error") + val called = AtomicInt(0) + setUnhandledExceptionHook { + assertSame(exception, it) + called.value = 1 + } + + processUnhandledException(exception) + assertEquals(1, called.value) +} \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_unhandled_exception.out b/native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.out similarity index 100% rename from kotlin-native/backend.native/tests/runtime/exceptions/custom_hook_unhandled_exception.out rename to native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.out diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java index 96a8f836f9c..a36de8ae256 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeStandaloneTestGenerated.java @@ -141,4 +141,103 @@ public class FirNativeStandaloneTestGenerated extends AbstractNativeBlackBoxTest runTest("native/native.tests/testData/standalone/entryPoint/mainOverloadingNoArgs.kt"); } } + + @Nested + @TestMetadata("native/native.tests/testData/standalone/termination") + @TestDataPath("$PROJECT_ROOT") + @Tag("standalone") + @EnforcedProperty(property = ClassLevelProperty.TEST_KIND, propertyValue = "STANDALONE_NO_TR") + @UseStandardTestCaseGroupProvider() + @Tag("frontend-fir") + @FirPipeline() + public class Termination { + @Test + public void testAllFilesPresentInTermination() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("native/native.tests/testData/standalone/termination"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("assertFailed.kt") + public void testAssertFailed() throws Exception { + runTest("native/native.tests/testData/standalone/termination/assertFailed.kt"); + } + + @Test + @TestMetadata("assertPassed.kt") + public void testAssertPassed() throws Exception { + runTest("native/native.tests/testData/standalone/termination/assertPassed.kt"); + } + + @Test + @TestMetadata("exitProcess.kt") + public void testExitProcess() throws Exception { + runTest("native/native.tests/testData/standalone/termination/exitProcess.kt"); + } + + @Test + @TestMetadata("globalThrow.kt") + public void testGlobalThrow() throws Exception { + runTest("native/native.tests/testData/standalone/termination/globalThrow.kt"); + } + + @Test + @TestMetadata("mainThrow.kt") + public void testMainThrow() throws Exception { + runTest("native/native.tests/testData/standalone/termination/mainThrow.kt"); + } + + @Test + @TestMetadata("processUnhandledException.kt") + public void testProcessUnhandledException() throws Exception { + runTest("native/native.tests/testData/standalone/termination/processUnhandledException.kt"); + } + + @Test + @TestMetadata("terminateWithUnhandledException.kt") + public void testTerminateWithUnhandledException() throws Exception { + runTest("native/native.tests/testData/standalone/termination/terminateWithUnhandledException.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookClosure.kt") + public void testUnhandledExceptionHookClosure() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookClosure.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookFun.kt") + public void testUnhandledExceptionHookFun() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookFun.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookGet.kt") + public void testUnhandledExceptionHookGet() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookGet.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookTerminate.kt") + public void testUnhandledExceptionHookTerminate() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminate.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookTerminateWithProcess.kt") + public void testUnhandledExceptionHookTerminateWithProcess() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminateWithProcess.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookThrow.kt") + public void testUnhandledExceptionHookThrow() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookThrow.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookWithProcess.kt") + public void testUnhandledExceptionHookWithProcess() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.kt"); + } + } } diff --git a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java index a58e9d2e48d..eed402cb70a 100644 --- a/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java +++ b/native/native.tests/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeStandaloneTestGenerated.java @@ -134,4 +134,101 @@ public class NativeStandaloneTestGenerated extends AbstractNativeBlackBoxTest { runTest("native/native.tests/testData/standalone/entryPoint/mainOverloadingNoArgs.kt"); } } + + @Nested + @TestMetadata("native/native.tests/testData/standalone/termination") + @TestDataPath("$PROJECT_ROOT") + @Tag("standalone") + @EnforcedProperty(property = ClassLevelProperty.TEST_KIND, propertyValue = "STANDALONE_NO_TR") + @UseStandardTestCaseGroupProvider() + public class Termination { + @Test + public void testAllFilesPresentInTermination() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("native/native.tests/testData/standalone/termination"), Pattern.compile("^(.+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("assertFailed.kt") + public void testAssertFailed() throws Exception { + runTest("native/native.tests/testData/standalone/termination/assertFailed.kt"); + } + + @Test + @TestMetadata("assertPassed.kt") + public void testAssertPassed() throws Exception { + runTest("native/native.tests/testData/standalone/termination/assertPassed.kt"); + } + + @Test + @TestMetadata("exitProcess.kt") + public void testExitProcess() throws Exception { + runTest("native/native.tests/testData/standalone/termination/exitProcess.kt"); + } + + @Test + @TestMetadata("globalThrow.kt") + public void testGlobalThrow() throws Exception { + runTest("native/native.tests/testData/standalone/termination/globalThrow.kt"); + } + + @Test + @TestMetadata("mainThrow.kt") + public void testMainThrow() throws Exception { + runTest("native/native.tests/testData/standalone/termination/mainThrow.kt"); + } + + @Test + @TestMetadata("processUnhandledException.kt") + public void testProcessUnhandledException() throws Exception { + runTest("native/native.tests/testData/standalone/termination/processUnhandledException.kt"); + } + + @Test + @TestMetadata("terminateWithUnhandledException.kt") + public void testTerminateWithUnhandledException() throws Exception { + runTest("native/native.tests/testData/standalone/termination/terminateWithUnhandledException.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookClosure.kt") + public void testUnhandledExceptionHookClosure() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookClosure.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookFun.kt") + public void testUnhandledExceptionHookFun() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookFun.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookGet.kt") + public void testUnhandledExceptionHookGet() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookGet.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookTerminate.kt") + public void testUnhandledExceptionHookTerminate() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminate.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookTerminateWithProcess.kt") + public void testUnhandledExceptionHookTerminateWithProcess() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookTerminateWithProcess.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookThrow.kt") + public void testUnhandledExceptionHookThrow() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookThrow.kt"); + } + + @Test + @TestMetadata("unhandledExceptionHookWithProcess.kt") + public void testUnhandledExceptionHookWithProcess() throws Exception { + runTest("native/native.tests/testData/standalone/termination/unhandledExceptionHookWithProcess.kt"); + } + } }