[K/N][tests] Migrate interopTest tests ^KT-61259

This commit is contained in:
Alexander Shabalin
2024-03-13 23:33:31 +01:00
committed by Space Team
parent c227e60150
commit bbcc5c9aed
17 changed files with 206 additions and 251 deletions
@@ -973,82 +973,11 @@ createInterop("kt43502") {
}
}
createInterop("leakMemoryWithRunningThread") {
it.defFile 'interop/leakMemoryWithRunningThread/leakMemory.def'
it.headers "$projectDir/interop/leakMemoryWithRunningThread/leakMemory.h"
it.extraOpts "-Xcompile-source", "$projectDir/interop/leakMemoryWithRunningThread/leakMemory.cpp"
}
createInterop("workerSignals") {
it.defFile "interop/workerSignals/workerSignals.def"
it.headers "$projectDir/interop/workerSignals/workerSignals.h"
it.extraOpts "-Xcompile-source", "$projectDir/interop/workerSignals/workerSignals.cpp"
}
createInterop("exceptions_throwThroughBridge") {
it.defFile "interop/exceptions/throwThroughBridge.def"
it.extraOpts "-Xcompile-source", "$projectDir/interop/exceptions/throwThroughBridgeInterop.cpp"
}
/**
* Creates a task for the interop test. Configures runner and adds library and test build tasks.
*/
Task interopTestBase(String name, boolean multiFile, boolean interop2ForMainOnly, Closure<KonanInteropTest> configureClosure) {
return KotlinNativeTestKt.createTest(project, name, KonanInteropTest) { KonanInteropTest task ->
task.configure(configureClosure)
if (task.enabled) {
konanArtifacts {
def targetName = target.name
def interopLib = task.interop
def interopLib2 = task.interop2
UtilsKt.dependsOnKonanBuildingTask(task, interopLib, target)
if (interopLib2 != null) UtilsKt.dependsOnKonanBuildingTask(task, interopLib2, target)
def lib = "lib$name"
def hasIntermediateLib = task.lib != null
if (hasIntermediateLib) {
// build a library from KonanInteropTest::lib property
library(lib, targets: [targetName]) {
libraries {
artifact interopLib
if (interopLib2 != null && !interop2ForMainOnly) artifact interopLib2
}
srcFiles task.lib
baseDir "$testOutputLocal/$name"
extraOpts task.flags
extraOpts project.globalTestArgs.findAll { !it.contains("-Xpartial-linkage") }
}
UtilsKt.dependsOnKonanBuildingTask(task, lib, target)
}
program(name, targets: [targetName]) {
libraries {
artifact interopLib
if (interopLib2 != null) artifact interopLib2
if (hasIntermediateLib)
klibFile file("$testOutputLocal/$name/$targetName/${lib}.klib")
}
srcFiles multiFile ? fileTree(task.source) { include '**/*.kt' } : task.getSources()
baseDir "$testOutputLocal/$name"
linkerOpts "-L${project.layout.buildDirectory.get().asFile}"
extraOpts task.flags
extraOpts project.globalTestArgs
}
}
task.dependsOn(nativeDependencies.llvmDependency)
task.dependsOn(nativeDependencies.targetDependency(target))
}
}
}
Task interopTest(String name, Closure<KonanInteropTest> configureClosure) {
return interopTestBase(name, false, false, configureClosure)
}
Task interopTestMultifile(String name, Closure<KonanInteropTest> configureClosure) {
return interopTestBase(name, true, false, configureClosure)
}
standaloneTest("interop_objc_allocException") {
disabled = !PlatformInfo.isAppleTarget(project)
expectedExitStatus = 0
@@ -1063,19 +992,6 @@ dynamicTest("interop_kt43502") {
useGoldenData = true
}
interopTest("interop_leakMemoryWithRunningThreadChecked") {
interop = 'leakMemoryWithRunningThread'
source = "interop/leakMemoryWithRunningThread/checked.kt"
expectedExitStatusChecker = { it != 0 }
outputChecker = { s -> s.contains("Cannot run checkers when there are 1 alive runtimes at the shutdown") }
}
interopTest("interop_workerSignals") {
disabled = project.testTarget == 'mingw_x64' // cross-thread signalling does not work on Windows
source = "interop/workerSignals/workerSignals.kt"
interop = "workerSignals"
}
if (PlatformInfo.isAppleTarget(project)) {
standaloneTest("interop_objc_kt61441") {
source = "interop/objc/kt61441.kt"
@@ -1,29 +0,0 @@
@file:OptIn(kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class)
import leakMemory.*
import kotlin.concurrent.AtomicInt
import kotlin.native.concurrent.*
import kotlin.native.Platform
import kotlin.test.*
import kotlinx.cinterop.*
val global = AtomicInt(0)
fun ensureInititalized() {
// Only needed with the legacy MM. TODO: Remove when legacy MM is gone.
kotlin.native.initRuntimeIfNeeded()
// Leak memory
StableRef.create(Any())
global.value = 1
}
fun main() {
Platform.isMemoryLeakCheckerActive = true
kotlin.native.runtime.Debugging.forceCheckedShutdown = true
assertTrue(global.value == 0)
// Created a thread, made sure Kotlin is initialized there.
test_RunInNewThread(staticCFunction(::ensureInititalized))
assertTrue(global.value == 1)
// Now exiting. With checked shutdown we will fail, complaining there're
// unfinished threads with runtimes.
}
@@ -1,15 +0,0 @@
#include "leakMemory.h"
#include <atomic>
#include <thread>
extern "C" void test_RunInNewThread(void (*f)()) {
std::atomic<bool> haveRun(false);
std::thread t([f, &haveRun]() {
f();
haveRun = true;
while (true) {}
});
t.detach();
while (!haveRun) {}
}
@@ -1,5 +0,0 @@
package leakMemory
---
void test_RunInNewThread(void (*)());
@@ -1,9 +0,0 @@
#ifdef __cplusplus
extern "C" {
#endif
void test_RunInNewThread(void (*)());
#ifdef __cplusplus
}
#endif
@@ -1,33 +0,0 @@
#include "workerSignals.h"
#include <cassert>
#include <cstring>
#include <pthread.h>
#include <signal.h>
namespace {
int pendingValue = 0;
thread_local int value = 0;
void signalHandler(int signal) {
value = pendingValue;
}
} // namespace
extern "C" void setupSignalHandler(void) {
signal(SIGUSR1, &signalHandler);
}
extern "C" void signalThread(uint64_t thread, int value) {
pendingValue = value;
pthread_t t = {};
memcpy(&t, &thread, sizeof(pthread_t));
auto result = pthread_kill(t, SIGUSR1);
assert(result == 0);
}
extern "C" int getValue(void) {
return value;
}
@@ -1,2 +0,0 @@
language = C
headerFilter = **/workerSignals.h
@@ -1,13 +0,0 @@
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
void setupSignalHandler(void);
void signalThread(uint64_t thread, int value);
int getValue(void);
#ifdef __cplusplus
}
#endif
@@ -1,27 +0,0 @@
@file:OptIn(kotlin.ExperimentalStdlibApi::class, ObsoleteWorkersApi::class)
import kotlin.native.concurrent.*
import kotlin.test.*
import workerSignals.*
const val defaultValue = 0
const val newValue = 42
fun main() {
setupSignalHandler()
withWorker {
val before = execute(TransferMode.SAFE, {}) {
getValue()
}.result
assertEquals(defaultValue, getValue())
assertEquals(defaultValue, before)
signalThread(platformThreadId, newValue)
val after = execute(TransferMode.SAFE, {}) {
getValue()
}.result
assertEquals(defaultValue, getValue())
assertEquals(newValue, after)
}
}