-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: periodic checks for ws service to start if necessary (WPB-6343) (#…
…2792) Co-authored-by: Yamil Medina <[email protected]>
- Loading branch information
1 parent
6ff6280
commit 36a230f
Showing
8 changed files
with
266 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
app/src/main/kotlin/com/wire/android/feature/StartPersistentWebsocketIfNecessaryUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
@file:Suppress("StringTemplate") | ||
|
||
package com.wire.android.feature | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import com.wire.android.appLogger | ||
import com.wire.android.services.PersistentWebSocketService | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class StartPersistentWebsocketIfNecessaryUseCase @Inject constructor( | ||
@ApplicationContext private val appContext: Context, | ||
private val shouldStartPersistentWebSocketService: ShouldStartPersistentWebSocketServiceUseCase | ||
) { | ||
suspend operator fun invoke() { | ||
val persistentWebSocketServiceIntent = PersistentWebSocketService.newIntent(appContext) | ||
shouldStartPersistentWebSocketService().let { | ||
when (it) { | ||
is ShouldStartPersistentWebSocketServiceUseCase.Result.Failure -> { | ||
appLogger.e("${TAG}: Failure while fetching persistent web socket status flow") | ||
} | ||
|
||
is ShouldStartPersistentWebSocketServiceUseCase.Result.Success -> { | ||
if (it.shouldStartPersistentWebSocketService) { | ||
startForegroundService(persistentWebSocketServiceIntent) | ||
} else { | ||
appLogger.i("${TAG}: Stopping PersistentWebsocketService, no user with persistent web socket enabled found") | ||
appContext.stopService(persistentWebSocketServiceIntent) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun startForegroundService(persistentWebSocketServiceIntent: Intent) { | ||
when { | ||
PersistentWebSocketService.isServiceStarted -> { | ||
appLogger.i("${TAG}: PersistentWebsocketService already started, not starting again") | ||
} | ||
|
||
else -> { | ||
appLogger.i("${TAG}: Starting PersistentWebsocketService") | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
appContext.startForegroundService(persistentWebSocketServiceIntent) | ||
} else { | ||
appContext.startService(persistentWebSocketServiceIntent) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
const val TAG = "StartPersistentWebsocketIfNecessaryUseCase" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
app/src/main/kotlin/com/wire/android/workmanager/worker/PersistentWebsocketCheckWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
@file:Suppress("StringTemplate") | ||
|
||
package com.wire.android.workmanager.worker | ||
|
||
import android.content.Context | ||
import androidx.hilt.work.HiltWorker | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.ExistingPeriodicWorkPolicy | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import androidx.work.WorkerParameters | ||
import com.wire.android.appLogger | ||
import com.wire.android.feature.StartPersistentWebsocketIfNecessaryUseCase | ||
import com.wire.android.workmanager.worker.PersistentWebsocketCheckWorker.Companion.NAME | ||
import com.wire.android.workmanager.worker.PersistentWebsocketCheckWorker.Companion.TAG | ||
import com.wire.android.workmanager.worker.PersistentWebsocketCheckWorker.Companion.WORK_INTERVAL | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedInject | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlin.time.Duration.Companion.hours | ||
import kotlin.time.toJavaDuration | ||
|
||
@HiltWorker | ||
class PersistentWebsocketCheckWorker | ||
@AssistedInject constructor( | ||
@Assisted private val appContext: Context, | ||
@Assisted private val workerParams: WorkerParameters, | ||
private val startPersistentWebsocketIfNecessary: StartPersistentWebsocketIfNecessaryUseCase | ||
) : CoroutineWorker(appContext, workerParams) { | ||
|
||
override suspend fun doWork(): Result = coroutineScope { | ||
appLogger.i("${TAG}: Starting periodic work check for persistent websocket connection") | ||
startPersistentWebsocketIfNecessary() | ||
Result.success() | ||
} | ||
|
||
companion object { | ||
const val NAME = "wss_check_worker" | ||
const val TAG = "PersistentWebsocketCheckWorker" | ||
val WORK_INTERVAL = 24.hours.toJavaDuration() | ||
} | ||
} | ||
|
||
fun WorkManager.enqueuePeriodicPersistentWebsocketCheckWorker() { | ||
appLogger.i("${TAG}: Enqueueing periodic work for $TAG") | ||
enqueueUniquePeriodicWork( | ||
NAME, ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, | ||
PeriodicWorkRequestBuilder<PersistentWebsocketCheckWorker>(WORK_INTERVAL) | ||
.addTag(TAG) // adds the tag so we can cancel later all related work. | ||
.build() | ||
) | ||
} | ||
|
||
fun WorkManager.cancelPeriodicPersistentWebsocketCheckWorker() { | ||
appLogger.i("${TAG}: Cancelling all periodic scheduled work for the tag $TAG") | ||
cancelAllWorkByTag(TAG) | ||
} |
86 changes: 86 additions & 0 deletions
86
...rc/test/kotlin/com/wire/android/feature/StartPersistentWebsocketIfNecessaryUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.feature | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import io.mockk.MockKAnnotations | ||
import io.mockk.coEvery | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.verify | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.Test | ||
|
||
class StartPersistentWebsocketIfNecessaryUseCaseTest { | ||
|
||
@Test | ||
fun givenShouldStartPersistentWebsocketTrue_whenInvoking_thenStartService() = | ||
runTest { | ||
// given | ||
val (arrangement, sut) = Arrangement() | ||
.withShouldStartPersistentWebsocketServiceResult(true) | ||
.arrange() | ||
|
||
// when | ||
sut.invoke() | ||
|
||
// then | ||
verify(exactly = 1) { arrangement.applicationContext.startService(any()) } | ||
} | ||
|
||
@Test | ||
fun givenShouldStartPersistentWebsocketFalse_whenInvoking_thenDONTStartService() = | ||
runTest { | ||
// given | ||
val (arrangement, sut) = Arrangement() | ||
.withShouldStartPersistentWebsocketServiceResult(false) | ||
.arrange() | ||
|
||
// when | ||
sut.invoke() | ||
|
||
// then | ||
verify(exactly = 0) { arrangement.applicationContext.startService(any()) } | ||
} | ||
|
||
inner class Arrangement { | ||
|
||
@MockK | ||
lateinit var shouldStartPersistentWebSocketServiceUseCase: ShouldStartPersistentWebSocketServiceUseCase | ||
|
||
@MockK | ||
lateinit var applicationContext: Context | ||
|
||
init { | ||
MockKAnnotations.init(this, relaxUnitFun = true) | ||
every { applicationContext.startService(any()) } returns ComponentName.createRelative("dummy", "class") | ||
every { applicationContext.stopService(any()) } returns true | ||
} | ||
|
||
fun arrange() = this to StartPersistentWebsocketIfNecessaryUseCase( | ||
applicationContext, | ||
shouldStartPersistentWebSocketServiceUseCase | ||
) | ||
|
||
fun withShouldStartPersistentWebsocketServiceResult(shouldStart: Boolean) = apply { | ||
coEvery { shouldStartPersistentWebSocketServiceUseCase.invoke() } returns | ||
ShouldStartPersistentWebSocketServiceUseCase.Result.Success(shouldStart) | ||
} | ||
} | ||
} |
Oops, something went wrong.