Skip to content

Commit

Permalink
SyncDecisionMaker -> SyncRequestEvaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Mar 16, 2024
1 parent b8b886a commit 875099a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class FeedSyncEngineImpl(
.allFeedOrigins().asFlow().mapToList(dbDispatcher)
.map { SyncRequest(forceRefresh = false, skipFeedSources = it.isNotEmpty()) }

private val syncDecisionMaker = SyncDecisionMaker(
private val syncRequestEvaluator = SyncRequestEvaluator(
syncConfig = SyncConfig.Default,
feedOriginEntityQueries = db.feedOriginEntityQueries,
lastSyncMetadataQueries = db.lastSyncMetadataQueries,
Expand All @@ -68,7 +68,7 @@ public class FeedSyncEngineImpl(
}

private suspend fun performSync(syncRequest: SyncRequest) = coroutineScope {
val (shouldSyncSources, shouldSyncItems) = syncDecisionMaker.decide(syncRequest)
val (shouldSyncSources, shouldSyncItems) = syncRequestEvaluator.evaluate(syncRequest)

val feedSourcesDeferred = if (shouldSyncSources) {
async { feedService.fetchFeedOrigins() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import io.github.reactivecircus.kstreamlined.kmp.database.SyncResourceType
import io.github.reactivecircus.kstreamlined.kmp.feed.sync.mapper.toSyncParams
import kotlinx.datetime.Clock

internal class SyncDecisionMaker(
internal class SyncRequestEvaluator(
private val syncConfig: SyncConfig,
private val feedOriginEntityQueries: FeedOriginEntityQueries,
private val lastSyncMetadataQueries: LastSyncMetadataQueries,
private val clock: Clock,
) {
fun decide(syncRequest: SyncRequest): SyncDecision {
fun evaluate(syncRequest: SyncRequest): SyncDecision {
return SyncDecision(
shouldSyncFeedSources = !syncRequest.skipFeedSources &&
(syncRequest.forceRefresh || shouldSyncFeedSources()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds

class SyncDecisionMakerTest {
class SyncRequestEvaluatorTest {

private val db = createInMemoryDatabase()

private val syncConfig = SyncConfig.Default

private val fakeClock = FakeClock()

private val syncDecisionMaker = SyncDecisionMaker(
private val syncRequestEvaluator = SyncRequestEvaluator(
syncConfig = syncConfig,
feedOriginEntityQueries = db.feedOriginEntityQueries,
lastSyncMetadataQueries = db.lastSyncMetadataQueries,
Expand All @@ -26,22 +26,22 @@ class SyncDecisionMakerTest {
@Test
fun `should not sync feed sources when skipFeedSources is true`() {
val syncRequest = SyncRequest(forceRefresh = true, skipFeedSources = true)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertFalse(result.shouldSyncFeedSources)
}

@Test
fun `should sync feed sources and feed items when forceRefresh is true`() {
val syncRequest = SyncRequest(forceRefresh = true)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertTrue(result.shouldSyncFeedSources)
assertTrue(result.shouldSyncFeedItems)
}

@Test
fun `should sync feed sources when no lastSyncMetadata exists`() {
val syncRequest = SyncRequest(forceRefresh = false)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertTrue(result.shouldSyncFeedSources)
}

Expand All @@ -56,7 +56,7 @@ class SyncDecisionMakerTest {
fakeClock.currentTime += syncConfig.feedSourcesCacheMaxAge

val syncRequest = SyncRequest(forceRefresh = false)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertFalse(result.shouldSyncFeedSources)
}

Expand All @@ -71,14 +71,14 @@ class SyncDecisionMakerTest {
fakeClock.currentTime += syncConfig.feedSourcesCacheMaxAge + 1.seconds

val syncRequest = SyncRequest(forceRefresh = false)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertTrue(result.shouldSyncFeedSources)
}

@Test
fun `should sync feed items when no lastSyncMetadata exists`() {
val syncRequest = SyncRequest(forceRefresh = false)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertTrue(result.shouldSyncFeedItems)
}

Expand Down Expand Up @@ -106,7 +106,7 @@ class SyncDecisionMakerTest {
fakeClock.currentTime += syncConfig.feedItemsCacheMaxAge

val syncRequest = SyncRequest(forceRefresh = false)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertFalse(result.shouldSyncFeedItems)
}

Expand Down Expand Up @@ -142,7 +142,7 @@ class SyncDecisionMakerTest {
fakeClock.currentTime += syncConfig.feedItemsCacheMaxAge + 1.seconds

val syncRequest = SyncRequest(forceRefresh = false)
val result = syncDecisionMaker.decide(syncRequest)
val result = syncRequestEvaluator.evaluate(syncRequest)
assertTrue(result.shouldSyncFeedItems)
}
}

0 comments on commit 875099a

Please sign in to comment.