LoadBgWorkerTest

This commit is contained in:
Milos Kozak 2023-01-13 23:19:23 +01:00
parent 0f1de302b4
commit 2f1c2bd0cc
3 changed files with 125 additions and 12 deletions

View file

@ -1,7 +1,11 @@
package info.nightscout.plugins.sync.di
import android.content.Context
import androidx.work.WorkManager
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.Reusable
import dagger.android.ContributesAndroidInjector
import info.nightscout.interfaces.nsclient.NSSettingsStatus
import info.nightscout.interfaces.nsclient.ProcessedDeviceStatusData
@ -31,7 +35,8 @@ import info.nightscout.plugins.sync.tidepool.TidepoolFragment
@Module(
includes = [
SyncModule.Binding::class
SyncModule.Binding::class,
SyncModule.Provide::class
]
)
@ -61,6 +66,13 @@ abstract class SyncModule {
@ContributesAndroidInjector abstract fun contributesTidepoolFragment(): TidepoolFragment
@Module
open class Provide {
@Reusable
@Provides
fun providesWorkManager(context: Context) = WorkManager.getInstance(context)
}
@Module
interface Binding {

View file

@ -36,10 +36,11 @@ class LoadBgWorker(
@Inject lateinit var nsClientV3Plugin: NSClientV3Plugin
@Inject lateinit var nsClientSource: NSClientSource
@Inject lateinit var workerClasses: WorkerClasses
@Inject lateinit var workManager: WorkManager
override suspend fun doWorkAndLog(): Result {
if (!nsClientSource.isEnabled() && !sp.getBoolean(info.nightscout.core.utils.R.string.key_ns_receive_cgm, false)) {
WorkManager.getInstance(context)
workManager
.enqueueUniqueWork(
NSClientV3Plugin.JOB_NAME,
ExistingWorkPolicy.APPEND_OR_REPLACE,
@ -71,11 +72,12 @@ class LoadBgWorker(
// Objective0
sp.putBoolean(info.nightscout.core.utils.R.string.key_objectives_bg_is_available_in_ns, true)
// Schedule processing of fetched data and continue of loading
WorkManager.getInstance(context).beginUniqueWork(
NSClientV3Plugin.JOB_NAME,
ExistingWorkPolicy.APPEND_OR_REPLACE,
OneTimeWorkRequest.Builder(workerClasses.nsClientSourceWorker).setInputData(dataWorkerStorage.storeInputData(sgvs)).build()
)
workManager
.beginUniqueWork(
NSClientV3Plugin.JOB_NAME,
ExistingWorkPolicy.APPEND_OR_REPLACE,
OneTimeWorkRequest.Builder(workerClasses.nsClientSourceWorker).setInputData(dataWorkerStorage.storeInputData(sgvs)).build()
)
// response 304 == Not modified (happens when date > srvModified => bad time on phone or server during upload
.then(response.code != 304, OneTimeWorkRequest.Builder(LoadBgWorker::class.java).build())
.then(response.code == 304, OneTimeWorkRequest.Builder(LoadTreatmentsWorker::class.java).build())
@ -87,7 +89,7 @@ class LoadBgWorker(
nsClientV3Plugin.storeLastLoadedSrvModified()
}
rxBus.send(EventNSClientNewLog("RCV END", "No SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
WorkManager.getInstance(context)
workManager
.beginUniqueWork(
NSClientV3Plugin.JOB_NAME,
ExistingWorkPolicy.APPEND_OR_REPLACE,
@ -103,7 +105,7 @@ class LoadBgWorker(
nsClientV3Plugin.storeLastLoadedSrvModified()
}
rxBus.send(EventNSClientNewLog("RCV END", "No new SGVs from ${dateUtil.dateAndTimeAndSecondsString(lastLoaded)}"))
WorkManager.getInstance(context)
workManager
.beginUniqueWork(
NSClientV3Plugin.JOB_NAME,
ExistingWorkPolicy.APPEND_OR_REPLACE,

View file

@ -1,12 +1,19 @@
package info.nightscout.plugins.sync.nsclientV3.workers
import androidx.work.ExistingWorkPolicy
import androidx.work.ListenableWorker
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkContinuation
import androidx.work.WorkManager
import androidx.work.testing.TestListenableWorkerBuilder
import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.TestBase
import info.nightscout.core.utils.fabric.FabricPrivacy
import info.nightscout.core.utils.receivers.DataWorkerStorage
import info.nightscout.core.utils.worker.LoggingWorker
import info.nightscout.database.entities.GlucoseValue
import info.nightscout.database.entities.embedments.InterfaceIDs
import info.nightscout.database.impl.AppRepository
import info.nightscout.interfaces.Config
import info.nightscout.interfaces.profile.ProfileFunction
@ -17,8 +24,10 @@ import info.nightscout.interfaces.ui.UiInteraction
import info.nightscout.interfaces.workflow.WorkerClasses
import info.nightscout.plugins.sync.nsclient.NsClientReceiverDelegate
import info.nightscout.plugins.sync.nsclientV3.NSClientV3Plugin
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSSvgV3
import info.nightscout.rx.bus.RxBus
import info.nightscout.sdk.interfaces.NSAndroidClient
import info.nightscout.sdk.remotemodel.LastModified
import info.nightscout.shared.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import info.nightscout.shared.utils.DateUtil
@ -27,14 +36,16 @@ import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.ArgumentMatchers.anyString
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
import org.mockito.Mockito
@OptIn(ExperimentalCoroutinesApi::class)
internal class LoadBgWorkerTest : TestBase() {
@Mock lateinit var dataWorkerStorage: DataWorkerStorage
@Mock lateinit var workerClasses: WorkerClasses
@Mock lateinit var sp: SP
@Mock lateinit var fabricPrivacy: FabricPrivacy
@ -49,9 +60,12 @@ internal class LoadBgWorkerTest : TestBase() {
@Mock lateinit var repository: AppRepository
@Mock lateinit var receiverStatusStore: ReceiverStatusStore
@Mock lateinit var nsClientSource: NSClientSource
@Mock lateinit var workManager: WorkManager
@Mock lateinit var workContinuation: WorkContinuation
private lateinit var nsClientV3Plugin: NSClientV3Plugin
private lateinit var nsClientReceiverDelegate: NsClientReceiverDelegate
private lateinit var dataWorkerStorage: DataWorkerStorage
private lateinit var sut: LoadBgWorker
private val now = 1000000000L
@ -69,6 +83,7 @@ internal class LoadBgWorkerTest : TestBase() {
it.nsClientV3Plugin = nsClientV3Plugin
it.workerClasses = workerClasses
it.nsClientSource = nsClientSource
it.workManager = workManager
}
}
}
@ -79,12 +94,13 @@ internal class LoadBgWorkerTest : TestBase() {
Mockito.`when`(context.androidInjector()).thenReturn(injector.androidInjector())
Mockito.`when`(dateUtil.now()).thenReturn(now)
Mockito.`when`(nsClientSource.isEnabled()).thenReturn(true)
dataWorkerStorage = DataWorkerStorage(context)
nsClientReceiverDelegate = NsClientReceiverDelegate(rxBus, rh, sp, receiverStatusStore)
nsClientV3Plugin = NSClientV3Plugin(
injector, aapsLogger, aapsSchedulers, rxBus, rh, context, fabricPrivacy, sp, nsClientReceiverDelegate, config, dateUtil, uiInteraction, dataSyncSelector,
profileFunction, repository
)
nsClientV3Plugin.newestDataOnServer = LastModified(LastModified.Collections())
}
@Test
@ -95,9 +111,26 @@ internal class LoadBgWorkerTest : TestBase() {
Assertions.assertTrue(result is ListenableWorker.Result.Failure)
}
@Test
fun notEnabledNSClientSource() = runTest {
sut = TestListenableWorkerBuilder<LoadBgWorker>(context).build()
Mockito.`when`(nsClientSource.isEnabled()).thenReturn(false)
Mockito.`when`(sp.getBoolean(info.nightscout.core.utils.R.string.key_ns_receive_cgm, false)).thenReturn(false)
val result = sut.doWorkAndLog()
Assertions.assertTrue(result is ListenableWorker.Result.Success)
Assertions.assertTrue(result.outputData.getString("Result") == "Load not enabled")
Mockito.verify(workManager, Mockito.times(1)).enqueueUniqueWork(
eq(NSClientV3Plugin.JOB_NAME),
eq(ExistingWorkPolicy.APPEND_OR_REPLACE),
any<OneTimeWorkRequest>()
)
}
@Test
fun testThereAreNewerDataFirstLoadEmptyReturn() = runTest {
initWorkManager()
Mockito.`when`(workManager.beginUniqueWork(anyString(), any(), any<OneTimeWorkRequest>())).thenReturn(workContinuation)
Mockito.`when`(workContinuation.then(any<OneTimeWorkRequest>())).thenReturn(workContinuation)
nsClientV3Plugin.nsAndroidClient = nsAndroidClient
nsClientV3Plugin.lastLoadedSrvModified.collections.entries = 0L // first load
nsClientV3Plugin.firstLoadContinueTimestamp.collections.entries = now - 1000
@ -107,5 +140,71 @@ internal class LoadBgWorkerTest : TestBase() {
val result = sut.doWorkAndLog()
Assertions.assertEquals(now - 1000, nsClientV3Plugin.lastLoadedSrvModified.collections.entries)
Assertions.assertTrue(result is ListenableWorker.Result.Success)
Mockito.verify(workManager, Mockito.times(1)).beginUniqueWork(
eq(NSClientV3Plugin.JOB_NAME),
eq(ExistingWorkPolicy.APPEND_OR_REPLACE),
any<OneTimeWorkRequest>()
)
Mockito.verify(workContinuation, Mockito.times(1)).then(any<OneTimeWorkRequest>())
Mockito.verify(workContinuation, Mockito.times(1)).enqueue()
}
@Test
fun testThereAreNewerDataFirstLoadListReturn() = runTest {
val glucoseValue = GlucoseValue(
timestamp = 10000,
isValid = true,
raw = 101.0,
value = 99.0,
trendArrow = GlucoseValue.TrendArrow.DOUBLE_UP,
noise = 1.0,
sourceSensor = GlucoseValue.SourceSensor.DEXCOM_G4_WIXEL,
interfaceIDs_backing = InterfaceIDs(
nightscoutId = "nightscoutId"
)
)
Mockito.`when`(workManager.beginUniqueWork(anyString(), any(), any<OneTimeWorkRequest>())).thenReturn(workContinuation)
Mockito.`when`(workContinuation.then(any<OneTimeWorkRequest>())).thenReturn(workContinuation)
Mockito.`when`(workerClasses.nsClientSourceWorker).thenReturn(LoggingWorker::class.java)
nsClientV3Plugin.nsAndroidClient = nsAndroidClient
nsClientV3Plugin.lastLoadedSrvModified.collections.entries = 0L // first load
nsClientV3Plugin.firstLoadContinueTimestamp.collections.entries = now - 1000
sut = TestListenableWorkerBuilder<LoadBgWorker>(context).build()
Mockito.`when`(nsAndroidClient.getSgvsNewerThan(anyLong(), anyLong())).thenReturn(NSAndroidClient.ReadResponse(200, 0, listOf(glucoseValue.toNSSvgV3())))
val result = sut.doWorkAndLog()
Assertions.assertTrue(result is ListenableWorker.Result.Success)
Mockito.verify(workManager, Mockito.times(1)).beginUniqueWork(
eq(NSClientV3Plugin.JOB_NAME),
eq(ExistingWorkPolicy.APPEND_OR_REPLACE),
any<OneTimeWorkRequest>()
)
Mockito.verify(workContinuation, Mockito.times(1)).then(any<OneTimeWorkRequest>())
Mockito.verify(workContinuation, Mockito.times(1)).enqueue()
}
@Test
fun testNoLoadNeeded() = runTest {
Mockito.`when`(workManager.beginUniqueWork(anyString(), any(), any<OneTimeWorkRequest>())).thenReturn(workContinuation)
Mockito.`when`(workContinuation.then(any<OneTimeWorkRequest>())).thenReturn(workContinuation)
nsClientV3Plugin.nsAndroidClient = nsAndroidClient
nsClientV3Plugin.firstLoadContinueTimestamp.collections.entries = now - 1000
nsClientV3Plugin.newestDataOnServer?.collections?.entries = now - 2000
sut = TestListenableWorkerBuilder<LoadBgWorker>(context).build()
Mockito.`when`(nsAndroidClient.getSgvsNewerThan(anyLong(), anyLong())).thenReturn(NSAndroidClient.ReadResponse(200, 0, emptyList()))
val result = sut.doWorkAndLog()
Assertions.assertEquals(now - 1000, nsClientV3Plugin.lastLoadedSrvModified.collections.entries)
Assertions.assertTrue(result is ListenableWorker.Result.Success)
Mockito.verify(workManager, Mockito.times(1)).beginUniqueWork(
eq(NSClientV3Plugin.JOB_NAME),
eq(ExistingWorkPolicy.APPEND_OR_REPLACE),
any<OneTimeWorkRequest>()
)
Mockito.verify(workContinuation, Mockito.times(1)).then(any<OneTimeWorkRequest>())
Mockito.verify(workContinuation, Mockito.times(1)).enqueue()
}
}