-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sync): pull feature flags when online (#2208)
On big teams, the user may not be notified when feature flags change, so we need to manually pull this information from the backend.
- Loading branch information
1 parent
2ae885d
commit bb40497
Showing
5 changed files
with
243 additions
and
5 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
66 changes: 66 additions & 0 deletions
66
...c/commonMain/kotlin/com/wire/kalium/logic/feature/featureConfig/FeatureFlagsSyncWorker.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,66 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.kalium.logic.feature.featureConfig | ||
|
||
import com.wire.kalium.logic.data.sync.IncrementalSyncRepository | ||
import com.wire.kalium.logic.data.sync.IncrementalSyncStatus | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.minutes | ||
|
||
/** | ||
* Worker that periodically syncs feature flags. | ||
*/ | ||
internal interface FeatureFlagsSyncWorker { | ||
suspend fun execute() | ||
} | ||
|
||
internal class FeatureFlagSyncWorkerImpl( | ||
private val incrementalSyncRepository: IncrementalSyncRepository, | ||
private val syncFeatureConfigs: SyncFeatureConfigsUseCase, | ||
private val minIntervalBetweenPulls: Duration = MIN_INTERVAL_BETWEEN_PULLS, | ||
private val clock: Clock = Clock.System | ||
) : FeatureFlagsSyncWorker { | ||
|
||
private var lastPullInstant: Instant? = null | ||
|
||
override suspend fun execute() { | ||
incrementalSyncRepository.incrementalSyncState.filter { | ||
it is IncrementalSyncStatus.Live | ||
}.collect { | ||
syncFeatureFlagsIfNeeded() | ||
} | ||
} | ||
|
||
private suspend fun FeatureFlagSyncWorkerImpl.syncFeatureFlagsIfNeeded() { | ||
val now = clock.now() | ||
val wasLastPullRecent = lastPullInstant?.let { lastPull -> | ||
lastPull + minIntervalBetweenPulls > now | ||
} ?: false | ||
if (!wasLastPullRecent) { | ||
syncFeatureConfigs() | ||
lastPullInstant = now | ||
} | ||
} | ||
|
||
private companion object { | ||
val MIN_INTERVAL_BETWEEN_PULLS = 60.minutes | ||
} | ||
} |
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
159 changes: 159 additions & 0 deletions
159
...ommonTest/kotlin/com/wire/kalium/logic/feature/featureConfig/FeatureFlagSyncWorkerTest.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,159 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 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.kalium.logic.feature.featureConfig | ||
|
||
import com.wire.kalium.logic.data.sync.IncrementalSyncStatus | ||
import com.wire.kalium.logic.functional.Either | ||
import com.wire.kalium.logic.util.arrangement.IncrementalSyncRepositoryArrangement | ||
import com.wire.kalium.logic.util.arrangement.IncrementalSyncRepositoryArrangementImpl | ||
import io.mockative.Mock | ||
import io.mockative.classOf | ||
import io.mockative.given | ||
import io.mockative.mock | ||
import io.mockative.once | ||
import io.mockative.verify | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.consumeAsFlow | ||
import kotlinx.coroutines.flow.flowOf | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.advanceUntilIdle | ||
import kotlinx.coroutines.test.runTest | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import kotlin.test.Test | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.minutes | ||
|
||
class FeatureFlagSyncWorkerTest { | ||
|
||
@Test | ||
fun givenSyncIsLive_thenShouldCallFeatureConfigsUseCase() = runTest { | ||
val (arrangement, featureFlagSyncWorker) = arrange { | ||
withIncrementalSyncState(flowOf(IncrementalSyncStatus.Live)) | ||
} | ||
val job = launch { | ||
featureFlagSyncWorker.execute() | ||
} | ||
|
||
advanceUntilIdle() | ||
verify(arrangement.syncFeatureConfigsUseCase) | ||
.suspendFunction(arrangement.syncFeatureConfigsUseCase::invoke) | ||
.wasInvoked(exactly = once) | ||
job.cancel() | ||
} | ||
|
||
@Test | ||
fun givenSyncIsLiveTwiceInAShortInterval_thenShouldCallFeatureConfigsUseCaseOnlyOnce() = runTest { | ||
val minimumInterval = 5.minutes | ||
val stateChannel = Channel<IncrementalSyncStatus>(capacity = Channel.UNLIMITED) | ||
|
||
val (arrangement, featureFlagSyncWorker) = arrange { | ||
minimumIntervalBetweenPulls = minimumInterval | ||
withIncrementalSyncState(stateChannel.consumeAsFlow()) | ||
} | ||
val job = launch { | ||
featureFlagSyncWorker.execute() | ||
} | ||
stateChannel.send(IncrementalSyncStatus.Live) | ||
stateChannel.send(IncrementalSyncStatus.Pending) | ||
advanceUntilIdle() | ||
stateChannel.send(IncrementalSyncStatus.Live) | ||
advanceUntilIdle() // Not enough to run twice | ||
verify(arrangement.syncFeatureConfigsUseCase) | ||
.suspendFunction(arrangement.syncFeatureConfigsUseCase::invoke) | ||
.wasInvoked(exactly = once) | ||
|
||
job.cancel() | ||
} | ||
|
||
@Test | ||
fun givenSyncIsLiveAgainAfterMinInterval_thenShouldCallFeatureConfigsUseCaseTwice() = runTest { | ||
val minInterval = 5.minutes | ||
val now = Clock.System.now() | ||
val stateTimes = mapOf( | ||
now to IncrementalSyncStatus.Live, | ||
now + minInterval + 1.milliseconds to IncrementalSyncStatus.Pending, | ||
now + minInterval + 2.milliseconds to IncrementalSyncStatus.Live | ||
) | ||
val fakeClock = object: Clock { | ||
var callCount = 0 | ||
override fun now(): Instant { | ||
return stateTimes.keys.toList()[callCount].also { callCount++ } | ||
} | ||
} | ||
val stateChannel = Channel<IncrementalSyncStatus>(capacity = Channel.UNLIMITED) | ||
val (arrangement, featureFlagSyncWorker) = arrange { | ||
minimumIntervalBetweenPulls = minInterval | ||
withIncrementalSyncState(stateChannel.consumeAsFlow()) | ||
clock = fakeClock | ||
} | ||
stateChannel.send(stateTimes.values.toList()[0]) | ||
val job = launch { | ||
featureFlagSyncWorker.execute() | ||
} | ||
advanceUntilIdle() | ||
|
||
verify(arrangement.syncFeatureConfigsUseCase) | ||
.suspendFunction(arrangement.syncFeatureConfigsUseCase::invoke) | ||
.wasInvoked(exactly = once) | ||
stateChannel.send(stateTimes.values.toList()[1]) | ||
advanceUntilIdle() | ||
|
||
stateChannel.send(stateTimes.values.toList()[2]) | ||
advanceUntilIdle() | ||
|
||
verify(arrangement.syncFeatureConfigsUseCase) | ||
.suspendFunction(arrangement.syncFeatureConfigsUseCase::invoke) | ||
.wasInvoked(exactly = once) | ||
job.cancel() | ||
} | ||
|
||
private class Arrangement( | ||
private val configure: Arrangement.() -> Unit | ||
) : IncrementalSyncRepositoryArrangement by IncrementalSyncRepositoryArrangementImpl() { | ||
|
||
@Mock | ||
val syncFeatureConfigsUseCase: SyncFeatureConfigsUseCase = mock(classOf<SyncFeatureConfigsUseCase>()) | ||
|
||
var minimumIntervalBetweenPulls: Duration = 1.minutes | ||
|
||
var clock: Clock = Clock.System | ||
|
||
init { | ||
given(syncFeatureConfigsUseCase) | ||
.suspendFunction(syncFeatureConfigsUseCase::invoke) | ||
.whenInvoked() | ||
.thenReturn(Either.Right(Unit)) | ||
} | ||
|
||
fun arrange(): Pair<Arrangement, FeatureFlagSyncWorkerImpl> = run { | ||
configure() | ||
this@Arrangement to FeatureFlagSyncWorkerImpl( | ||
incrementalSyncRepository = incrementalSyncRepository, | ||
syncFeatureConfigs = syncFeatureConfigsUseCase, | ||
minIntervalBetweenPulls = minimumIntervalBetweenPulls, | ||
clock = clock | ||
) | ||
} | ||
} | ||
|
||
private companion object { | ||
fun arrange(configure: Arrangement.() -> Unit) = Arrangement(configure).arrange() | ||
} | ||
} |
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