From b00a2de32a916b8e02fdad76383bc3a1b44b9a56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebastian=20K=C3=BCrten?= <sebastian@mobanisto.de>
Date: Tue, 29 Mar 2022 10:33:25 +0200
Subject: [PATCH] Add test for handling of background thread exceptions

---
 .../mailbox/core/server/IntegrationTest.kt    |  6 ++++--
 .../core/server/ThreadExceptionTest.kt        | 20 +++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 mailbox-core/src/test/java/org/briarproject/mailbox/core/server/ThreadExceptionTest.kt

diff --git a/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt b/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt
index 98c0115f..778f9ed0 100644
--- a/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt
+++ b/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/IntegrationTest.kt
@@ -113,8 +113,10 @@ abstract class IntegrationTest(private val installJsonFeature: Boolean = true) {
         }
 
         if (exceptionInBackgroundThread != null) {
-            fail("background thread has thrown an exception unexpectedly",
-                exceptionInBackgroundThread)
+            fail(
+                "background thread has thrown an exception unexpectedly",
+                exceptionInBackgroundThread
+            )
         }
     }
 
diff --git a/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/ThreadExceptionTest.kt b/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/ThreadExceptionTest.kt
new file mode 100644
index 00000000..4a1a570c
--- /dev/null
+++ b/mailbox-core/src/test/java/org/briarproject/mailbox/core/server/ThreadExceptionTest.kt
@@ -0,0 +1,20 @@
+package org.briarproject.mailbox.core.server
+
+import org.junit.jupiter.api.Test
+import kotlin.concurrent.thread
+import kotlin.test.assertNotNull
+
+class ThreadExceptionTest : IntegrationTest() {
+
+    @Test
+    fun `exception thrown on background thread gets caught and stored`() {
+        val t = thread(name = "failing thread") {
+            throw RuntimeException("boom")
+        }
+        t.join()
+
+        assertNotNull(exceptionInBackgroundThread)
+        exceptionInBackgroundThread = null
+    }
+
+}
-- 
GitLab