Fix sync tests

Change-Id: I4c62fcfa59793bef35eda2892f4f75acaeb86ca6
pull/1837/head
Adetunji Dahunsi 3 years ago committed by Don Turner
parent d01c5ce23a
commit 6ae1a48125

@ -21,7 +21,14 @@ plugins {
id 'nowinandroid.spotless' id 'nowinandroid.spotless'
} }
android {
defaultConfig {
testInstrumentationRunner "com.google.samples.apps.nowinandroid.core.testing.NiaTestRunner"
}
}
dependencies { dependencies {
implementation project(':core-common')
implementation project(':core-model') implementation project(':core-model')
implementation project(':core-domain') implementation project(':core-domain')
implementation project(':core-datastore') implementation project(':core-datastore')
@ -41,6 +48,9 @@ dependencies {
androidTestImplementation libs.androidx.work.testing androidTestImplementation libs.androidx.work.testing
kaptAndroidTest libs.hilt.compiler
kaptAndroidTest libs.hilt.ext.compiler
configurations.configureEach { configurations.configureEach {
resolutionStrategy { resolutionStrategy {
// Temporary workaround for https://issuetracker.google.com/174733673 // Temporary workaround for https://issuetracker.google.com/174733673

@ -19,19 +19,23 @@ package com.google.samples.apps.nowinandroid.sync.workers
import android.util.Log import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.work.Configuration import androidx.work.Configuration
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkInfo import androidx.work.WorkInfo
import androidx.work.WorkManager import androidx.work.WorkManager
import androidx.work.testing.SynchronousExecutor import androidx.work.testing.SynchronousExecutor
import androidx.work.testing.WorkManagerTestInitHelper import androidx.work.testing.WorkManagerTestInitHelper
import androidx.work.workDataOf import dagger.hilt.android.testing.HiltAndroidRule
import java.util.concurrent.TimeUnit import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Before import org.junit.Before
import org.junit.Rule
import org.junit.Test import org.junit.Test
@HiltAndroidTest
class SyncWorkerTest { class SyncWorkerTest {
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)
private val context get() = InstrumentationRegistry.getInstrumentation().context private val context get() = InstrumentationRegistry.getInstrumentation().context
@Before @Before
@ -46,26 +50,26 @@ class SyncWorkerTest {
} }
@Test @Test
fun testSyncPeriodicWork() { fun testSyncWork() {
// Define input data
val input = workDataOf()
// Create request // Create request
val request = PeriodicWorkRequestBuilder<SyncWorker>(15, TimeUnit.MINUTES) val request = SyncWorker.startUpSyncWork()
.setInputData(input)
.build()
val workManager = WorkManager.getInstance(context) val workManager = WorkManager.getInstance(context)
val testDriver = WorkManagerTestInitHelper.getTestDriver(context)!! val testDriver = WorkManagerTestInitHelper.getTestDriver(context)!!
// Enqueue and wait for result. // Enqueue and wait for result.
workManager.enqueue(request).result.get() workManager.enqueue(request).result.get()
// Tells the testing framework the period delay is met
testDriver.setPeriodDelayMet(request.id)
// Get WorkInfo and outputData // Get WorkInfo and outputData
val workInfo = workManager.getWorkInfoById(request.id).get() val preRunWorkInfo = workManager.getWorkInfoById(request.id).get()
// Assert // Assert
assertEquals(workInfo.state, WorkInfo.State.ENQUEUED) assertEquals(WorkInfo.State.ENQUEUED, preRunWorkInfo.state)
// Tells the testing framework that the constraints have been met
testDriver.setAllConstraintsMet(request.id)
val postRequirementWorkInfo = workManager.getWorkInfoById(request.id).get()
assertEquals(WorkInfo.State.RUNNING, postRequirementWorkInfo.state)
} }
} }

@ -29,13 +29,16 @@ import com.google.samples.apps.nowinandroid.core.domain.Synchronizer
import com.google.samples.apps.nowinandroid.core.domain.repository.AuthorsRepository import com.google.samples.apps.nowinandroid.core.domain.repository.AuthorsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository import com.google.samples.apps.nowinandroid.core.domain.repository.NewsRepository
import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository import com.google.samples.apps.nowinandroid.core.domain.repository.TopicsRepository
import com.google.samples.apps.nowinandroid.core.network.Dispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaDispatchers.IO
import com.google.samples.apps.nowinandroid.sync.initializers.SyncConstraints import com.google.samples.apps.nowinandroid.sync.initializers.SyncConstraints
import com.google.samples.apps.nowinandroid.sync.initializers.syncForegroundInfo import com.google.samples.apps.nowinandroid.sync.initializers.syncForegroundInfo
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.withContext
/** /**
* Syncs the data layer by delegating to the appropriate repository instances with * Syncs the data layer by delegating to the appropriate repository instances with
@ -49,12 +52,13 @@ class SyncWorker @AssistedInject constructor(
private val topicRepository: TopicsRepository, private val topicRepository: TopicsRepository,
private val newsRepository: NewsRepository, private val newsRepository: NewsRepository,
private val authorsRepository: AuthorsRepository, private val authorsRepository: AuthorsRepository,
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
) : CoroutineWorker(appContext, workerParams), Synchronizer { ) : CoroutineWorker(appContext, workerParams), Synchronizer {
override suspend fun getForegroundInfo(): ForegroundInfo = override suspend fun getForegroundInfo(): ForegroundInfo =
appContext.syncForegroundInfo() appContext.syncForegroundInfo()
override suspend fun doWork(): Result = coroutineScope { override suspend fun doWork(): Result = withContext(ioDispatcher) {
// First sync the repositories in parallel // First sync the repositories in parallel
val syncedSuccessfully = awaitAll( val syncedSuccessfully = awaitAll(
async { topicRepository.sync() }, async { topicRepository.sync() },

Loading…
Cancel
Save