From a488bb20ef18784214ed903a3535db280a950830 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sebastian=20K=C3=BCrten?= <sebastian@mobanisto.de>
Date: Wed, 10 Aug 2022 13:04:24 +0200
Subject: [PATCH] Add parameter that can be used to instruct Mailbox not to
 exit on stop

---
 build.gradle                                                | 1 -
 .../briarproject/mailbox/core/lifecycle/LifecycleManager.kt | 5 ++++-
 .../mailbox/core/lifecycle/LifecycleManagerImpl.kt          | 6 ++++--
 .../src/main/java/org/briarproject/mailbox/lib/Mailbox.kt   | 4 ++--
 .../java/org/briarproject/mailbox/lib/MailboxLibTest.kt     | 2 +-
 5 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/build.gradle b/build.gradle
index 2855cc54..5748c5f2 100644
--- a/build.gradle
+++ b/build.gradle
@@ -31,7 +31,6 @@ task clean(type: Delete) {
     delete rootProject.buildDir
 }
 
-
 configure([project(':mailbox-core'), project(':mailbox-lib')]) {
     apply plugin: 'maven-publish'
 
diff --git a/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManager.kt b/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManager.kt
index a15d3b78..850e4bd0 100644
--- a/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManager.kt
+++ b/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManager.kt
@@ -74,7 +74,10 @@ interface LifecycleManager {
      * registered [Services][Service].
      */
     @Wakeful
-    fun startServices(wipeHook: WipeHook = WipeHook { }): StartResult
+    fun startServices(
+        exitAfterStopping: Boolean = true,
+        wipeHook: WipeHook = WipeHook { },
+    ): StartResult
 
     /**
      * Stops any registered [Services][Service], shuts down any
diff --git a/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManagerImpl.kt b/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManagerImpl.kt
index c598732b..82ecbf59 100644
--- a/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManagerImpl.kt
+++ b/mailbox-core/src/main/java/org/briarproject/mailbox/core/lifecycle/LifecycleManagerImpl.kt
@@ -82,6 +82,7 @@ internal class LifecycleManagerImpl @Inject constructor(
     private val state = MutableStateFlow(NOT_STARTED)
 
     private var wipeHook: WipeHook? = null
+    private var exitAfterStopping: Boolean = true
 
     init {
         services = CopyOnWriteArrayList()
@@ -105,7 +106,7 @@ internal class LifecycleManagerImpl @Inject constructor(
     }
 
     @GuardedBy("startStopWipeSemaphore")
-    override fun startServices(wipeHook: WipeHook): StartResult {
+    override fun startServices(exitAfterStopping: Boolean, wipeHook: WipeHook): StartResult {
         LOG.info("startServices()")
         try {
             LOG.info("acquiring start stop semaphore")
@@ -121,6 +122,7 @@ internal class LifecycleManagerImpl @Inject constructor(
             return LIFECYCLE_REUSE
         }
         this.wipeHook = wipeHook
+        this.exitAfterStopping = exitAfterStopping
         return try {
             LOG.info("Opening database")
             var start = now()
@@ -215,7 +217,7 @@ internal class LifecycleManagerImpl @Inject constructor(
             // This is for the CLI where we might call stopServices() twice due to the shutdown
             // hook. In order to avoid a deadlock with calling exitProcess() from two threads, make
             // sure here that it gets called only once.
-            if (stopped) {
+            if (stopped && exitAfterStopping) {
                 LOG.info("Exiting")
                 exitProcess(0)
             }
diff --git a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt
index 8bc80195..954a9a9d 100644
--- a/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt
+++ b/mailbox-lib/src/main/java/org/briarproject/mailbox/lib/Mailbox.kt
@@ -76,9 +76,9 @@ class Mailbox {
         LOG.info { "Mailbox wiped successfully \\o/" }
     }
 
-    fun startLifecycle() {
+    fun startLifecycle(exitAfterStopping: Boolean) {
         LOG.info { "Starting lifecycle" }
-        lifecycleManager.startServices()
+        lifecycleManager.startServices(exitAfterStopping = exitAfterStopping)
         LOG.info { "Waiting for startup" }
         lifecycleManager.waitForStartup()
         LOG.info { "Startup finished" }
diff --git a/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt b/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt
index 610feb25..ad5cc9da 100644
--- a/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt
+++ b/mailbox-lib/src/test/java/org/briarproject/mailbox/lib/MailboxLibTest.kt
@@ -27,7 +27,7 @@ class MailboxLibTest {
     fun testStartStopMailbox() {
         val mailbox = Mailbox()
         mailbox.init()
-        mailbox.startLifecycle()
+        mailbox.startLifecycle(false)
         mailbox.waitForTorPublished()
         mailbox.stopLifecycle()
     }
-- 
GitLab