fix: a failed cloud save recovery no longer ends the app (#846)

The cloud-recovery thread started at every app start had nothing to
catch an error, so one went to CrashHandler, which halts the process
with status 2. In unit tests the thread can outlive the test that
started it, fail on the torn-down preferences, and take the whole test
run down. The error is now logged and the thread ends.

Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com>
This commit is contained in:
KirkandKirk authored and GitHub committed 2026-10-14 22:01:17 -04:00
1 parent 04f7b7e162
commit 98be0838c4
2 files changed
+44 -1

No files matched your search

@@ -0,0 +1,40 @@
package com.droiddeck.launcher.stores
import android.content.Context
import android.content.ContextWrapper
import android.content.SharedPreferences
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33], manifest = Config.NONE)
class CloudRecoveryThreadTest {
private val uncaught = mutableListOf<Throwable>()
private var previous: Thread.UncaughtExceptionHandler? = null
@Before fun setUp() {
previous = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler { _, error -> synchronized(uncaught) { uncaught += error } }
}
@After fun tearDown() {
Thread.setDefaultUncaughtExceptionHandler(previous)
}
@Test fun aFailingRecoveryUploadDoesNotReachTheCrashHandler() {
val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) {
override fun getApplicationContext(): Context = this
override fun getSharedPreferences(name: String, mode: Int): SharedPreferences =
if (name == "stores.cloud") throw IllegalStateException("preferences are gone") else super.getSharedPreferences(name, mode)
}
StoresState.init(context)
Thread.getAllStackTraces().keys.filter { it.name == "cloud-recovery" }.forEach { it.join(5000) }
assertEquals(emptyList<Throwable>(), synchronized(uncaught) { uncaught.toList() })
}
}