From 9aa645e2f17194efb732691c515f4b179948fa5d Mon Sep 17 00:00:00 2001 From: evgeny Date: Thu, 29 Oct 2026 13:39:58 +0100 Subject: [PATCH] Test restart readiness recovery and explicit UTF-8 database cleanup --- .../com/utun/chat/headless/CoreStartupTest.kt | 48 +++++++++++++++++++ .../chatgui-android/tests/jni_utf8/README.md | 2 + .../tests/jni_utf8/test_jni_utf8.c | 7 +++ 3 files changed, 57 insertions(+) diff --git a/tools/chatgui-android/app/src/test/java/com/utun/chat/headless/CoreStartupTest.kt b/tools/chatgui-android/app/src/test/java/com/utun/chat/headless/CoreStartupTest.kt index 4c185916f..4b87a3b9b 100644 --- a/tools/chatgui-android/app/src/test/java/com/utun/chat/headless/CoreStartupTest.kt +++ b/tools/chatgui-android/app/src/test/java/com/utun/chat/headless/CoreStartupTest.kt @@ -8,6 +8,7 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async import kotlinx.coroutines.cancel import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.delay import kotlinx.coroutines.withTimeout import org.junit.After import org.junit.Assert.* @@ -198,4 +199,51 @@ class CoreStartupTest { assertEquals("foreground denied", result?.message) assertTrue(logs.any { "stopSelf rejected" in it }) } + + @Test fun restartClosesOldReadinessAndCancelledWaiterDoesNotCancelRecovery() = runBlocking { + nativeReady = true; start(); config.complete("config"); startup.awaitReady() + nativeReady = false; startup.restarting() + val retired = waiter(); val active = waiter() + assertFalse(active.isCompleted); retired.cancel() + nativeReady = true; startup.coreStarted() + assertEquals(2, active.await()); assertTrue(retired.isCancelled) + assertEquals(0, failures); assertEquals(1, submitted.size) + } + + @Test fun awaitClosesReadinessBeforeDelayedStoppedEventArrives() = runBlocking { + nativeReady = true; start(); config.complete("config"); startup.awaitReady() + nativeReady = false + val pending = waiter(); assertFalse(pending.isCompleted) + startup.coreStopped() + nativeReady = true; startup.coreStarted() + assertEquals(2, pending.await()); assertEquals(0, failures) + } + + @Test fun unexpectedCoreLossFailsRecoveryWaitersAndStopsTheService() = runBlocking { + val owner = coordinator(timeout = 20) + nativeReady = true; owner.start({ "config" }) { true }; owner.awaitReady() + nativeReady = false; owner.coreStopped() + val error = withTimeout(2_000) { runCatching { owner.awaitReady() }.exceptionOrNull() } + withTimeout(2_000) { failed.await() } + assertTrue(error is IllegalStateException); assertTrue(error!!.message!!.contains("recovery timeout")) + assertEquals(1, failures) + } + + @Test fun serviceStopCancelsPendingRecoveryWithoutASecondFailure() = runBlocking { + val owner = coordinator(timeout = 20) + nativeReady = true; owner.start({ "config" }) { true }; owner.awaitReady() + nativeReady = false; owner.restarting(); owner.stop() + assertTrue(runCatching { owner.awaitReady() }.exceptionOrNull() is IllegalStateException) + delay(40); nativeReady = true; owner.coreStarted() + assertEquals(1, readyCalls); assertEquals(0, failures) + } + + @Test fun recoveryMonitorFailurePropagatesWithoutEscapingItsCoroutine() = runBlocking { + nativeReady = true; start(); config.complete("config"); startup.awaitReady() + nativeReady = false; startup.restarting() + val pending = waiter(); readinessError = IllegalStateException("recovery monitor failed") + nativeReady = true; startup.coreStarted() + assertEquals("recovery monitor failed", runCatching { pending.await() }.exceptionOrNull()?.message) + assertEquals(1, failures) + } } diff --git a/tools/chatgui-android/tests/jni_utf8/README.md b/tools/chatgui-android/tests/jni_utf8/README.md index 417d10eee..018767aa5 100644 --- a/tools/chatgui-android/tests/jni_utf8/README.md +++ b/tools/chatgui-android/tests/jni_utf8/README.md @@ -11,3 +11,5 @@ JVM проверяет корректность вызовов JNI; ART и Andro Тот же запуск проверяет регистрацию/замену настоящего Java event callback, доставку payload, исключение обработчика с последующей успешной доставкой и сохранение чужого pending exception. Gradle запускает проверку как `:app:testJniUtf8` перед `:app:testDebugUnitTest` на Linux. +Проверяются также повторные config callbacks, attach/detach native-потока и удаление БД +по явно переданному пути с кириллицей/emoji через настоящий JNI entry point. diff --git a/tools/chatgui-android/tests/jni_utf8/test_jni_utf8.c b/tools/chatgui-android/tests/jni_utf8/test_jni_utf8.c index 7e1233c9a..21c33c355 100644 --- a/tools/chatgui-android/tests/jni_utf8/test_jni_utf8.c +++ b/tools/chatgui-android/tests/jni_utf8/test_jni_utf8.c @@ -78,6 +78,13 @@ int main(void) { Java_com_utun_chat_data_NativeLib_nativeSetEventCallback(env, NULL, NULL); CHECK(!(*env)->ExceptionCheck(env)); jni_on_event(13, NULL, 0); CHECK(!(*env)->ExceptionCheck(env)); (*env)->DeleteLocalRef(env, replacement); (*env)->DeleteLocalRef(env, sink); (*env)->DeleteLocalRef(env, sink_class); + CHECK(!mkdir("база😀", 0700)); + FILE* database = fopen("база😀/chats.db", "wb"); CHECK(database && fputs("owned database", database) >= 0 && !fclose(database)); + jstring directory = jni_text(env, "база😀"); CHECK(directory); + CHECK(Java_com_utun_chat_data_NativeLib_nativeClearDatabase(env, NULL, directory) == JNI_TRUE); + CHECK(access("база😀/chats.db", F_OK) && errno == ENOENT); + CHECK(Java_com_utun_chat_data_NativeLib_nativeClearDatabase(env, NULL, directory) == JNI_TRUE); + (*env)->DeleteLocalRef(env, directory); CHECK(!rmdir("база😀")); jstring empty = jni_text(env, ""); CHECK(empty && !(*env)->GetStringLength(env, empty)); (*env)->DeleteLocalRef(env, empty); (*env)->DeleteLocalRef(env, exception); (*env)->DeleteLocalRef(env, embedded); (*env)->DeleteLocalRef(env, original);