Rewrites CommandQueueImplementationTest with matchers

Issue #2745
This commit is contained in:
Ryan Haining 2023-09-19 23:05:38 -07:00
parent 8b3e39420e
commit 8c8b492eba

View file

@ -1,5 +1,6 @@
package info.nightscout.implementation.queue package info.nightscout.implementation.queue
import com.google.common.truth.Truth.assertThat
import android.content.Context import android.content.Context
import android.os.Handler import android.os.Handler
import android.os.PowerManager import android.os.PowerManager
@ -38,7 +39,6 @@ import info.nightscout.shared.utils.DateUtil
import info.nightscout.sharedtests.TestBaseWithProfile import info.nightscout.sharedtests.TestBaseWithProfile
import info.nightscout.sharedtests.TestPumpPlugin import info.nightscout.sharedtests.TestPumpPlugin
import io.reactivex.rxjava3.core.Single import io.reactivex.rxjava3.core.Single
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.mockito.Mock import org.mockito.Mock
@ -169,121 +169,121 @@ class CommandQueueImplementationTest : TestBaseWithProfile() {
commandQueue.handler = handler commandQueue.handler = handler
// start with empty queue // start with empty queue
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// add bolus command // add bolus command
commandQueue.bolus(DetailedBolusInfo(), null) commandQueue.bolus(DetailedBolusInfo(), null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
commandQueue.waitForFinishedThread() commandQueue.waitForFinishedThread()
Thread.sleep(1000) Thread.sleep(1000)
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
} }
@Test @Test
fun doTests() { fun doTests() {
// start with empty queue // start with empty queue
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// add bolus command // add bolus command
commandQueue.bolus(DetailedBolusInfo(), null) commandQueue.bolus(DetailedBolusInfo(), null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// add READSTATUS // add READSTATUS
commandQueue.readStatus("anyString", null) commandQueue.readStatus("anyString", null)
Assertions.assertEquals(2, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(2)
// adding another bolus should remove the first one (size still == 2) // adding another bolus should remove the first one (size still == 2)
commandQueue.bolus(DetailedBolusInfo(), null) commandQueue.bolus(DetailedBolusInfo(), null)
Assertions.assertEquals(2, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(2)
// clear the queue should reset size // clear the queue should reset size
commandQueue.clear() commandQueue.clear()
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// add tempbasal // add tempbasal
commandQueue.tempBasalAbsolute(0.0, 30, true, validProfile, PumpSync.TemporaryBasalType.NORMAL, null) commandQueue.tempBasalAbsolute(0.0, 30, true, validProfile, PumpSync.TemporaryBasalType.NORMAL, null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// add tempbasal percent. it should replace previous TEMPBASAL // add tempbasal percent. it should replace previous TEMPBASAL
commandQueue.tempBasalPercent(0, 30, true, validProfile, PumpSync.TemporaryBasalType.NORMAL, null) commandQueue.tempBasalPercent(0, 30, true, validProfile, PumpSync.TemporaryBasalType.NORMAL, null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// cancel tempbasal it should replace previous TEMPBASAL // cancel tempbasal it should replace previous TEMPBASAL
commandQueue.cancelTempBasal(false, null) commandQueue.cancelTempBasal(false, null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// add extended bolus // add extended bolus
commandQueue.extendedBolus(1.0, 30, null) commandQueue.extendedBolus(1.0, 30, null)
Assertions.assertEquals(2, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(2)
// add extended should remove previous extended setting // add extended should remove previous extended setting
commandQueue.extendedBolus(1.0, 30, null) commandQueue.extendedBolus(1.0, 30, null)
Assertions.assertEquals(2, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(2)
// cancel extended bolus should replace previous extended // cancel extended bolus should replace previous extended
commandQueue.cancelExtended(null) commandQueue.cancelExtended(null)
Assertions.assertEquals(2, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(2)
// add setProfile // add setProfile
// TODO: this crash the test // TODO: this crash the test
// commandQueue.setProfile(validProfile, null) // commandQueue.setProfile(validProfile, null)
// Assertions.assertEquals(3, commandQueue.size()) // assertThat(commandQueue.size()).isEqualTo(3)
// add loadHistory // add loadHistory
commandQueue.loadHistory(0.toByte(), null) commandQueue.loadHistory(0.toByte(), null)
Assertions.assertEquals(3, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(3)
// add loadEvents // add loadEvents
commandQueue.loadEvents(null) commandQueue.loadEvents(null)
Assertions.assertEquals(4, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(4)
// add clearAlarms // add clearAlarms
commandQueue.clearAlarms(null) commandQueue.clearAlarms(null)
Assertions.assertEquals(5, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(5)
// add deactivate // add deactivate
commandQueue.deactivate(null) commandQueue.deactivate(null)
Assertions.assertEquals(6, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(6)
// add updateTime // add updateTime
commandQueue.updateTime(null) commandQueue.updateTime(null)
Assertions.assertEquals(7, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(7)
commandQueue.clear() commandQueue.clear()
commandQueue.tempBasalAbsolute(0.0, 30, true, validProfile, PumpSync.TemporaryBasalType.NORMAL, null) commandQueue.tempBasalAbsolute(0.0, 30, true, validProfile, PumpSync.TemporaryBasalType.NORMAL, null)
commandQueue.pickup() commandQueue.pickup()
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
Assertions.assertNotNull(commandQueue.performing) assertThat(commandQueue.performing).isNotNull()
Assertions.assertEquals(Command.CommandType.TEMPBASAL, commandQueue.performing?.commandType) assertThat(commandQueue.performing?.commandType).isEqualTo(Command.CommandType.TEMPBASAL)
commandQueue.resetPerforming() commandQueue.resetPerforming()
Assertions.assertNull(commandQueue.performing) assertThat(commandQueue.performing).isNull()
} }
@Test @Test
fun callingCancelAllBolusesClearsQueue() { fun callingCancelAllBolusesClearsQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
val smb = DetailedBolusInfo() val smb = DetailedBolusInfo()
smb.lastKnownBolusTime = System.currentTimeMillis() smb.lastKnownBolusTime = System.currentTimeMillis()
smb.bolusType = DetailedBolusInfo.BolusType.SMB smb.bolusType = DetailedBolusInfo.BolusType.SMB
commandQueue.bolus(smb, null) commandQueue.bolus(smb, null)
commandQueue.bolus(DetailedBolusInfo(), null) commandQueue.bolus(DetailedBolusInfo(), null)
Assertions.assertEquals(2, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(2)
// when // when
commandQueue.cancelAllBoluses(null) commandQueue.cancelAllBoluses(null)
// then // then
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
} }
@Test @Test
fun smbIsRejectedIfABolusIsQueued() { fun smbIsRejectedIfABolusIsQueued() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.bolus(DetailedBolusInfo(), null) commandQueue.bolus(DetailedBolusInfo(), null)
@ -292,14 +292,14 @@ class CommandQueueImplementationTest : TestBaseWithProfile() {
val queued: Boolean = commandQueue.bolus(smb, null) val queued: Boolean = commandQueue.bolus(smb, null)
// then // then
Assertions.assertFalse(queued) assertThat(queued).isFalse()
Assertions.assertEquals(commandQueue.size(), 1) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun smbIsRejectedIfLastKnownBolusIsOutdated() { fun smbIsRejectedIfLastKnownBolusIsOutdated() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
val bolus = DetailedBolusInfo() val bolus = DetailedBolusInfo()
@ -308,14 +308,14 @@ class CommandQueueImplementationTest : TestBaseWithProfile() {
val queued: Boolean = commandQueue.bolus(bolus, null) val queued: Boolean = commandQueue.bolus(bolus, null)
// then // then
Assertions.assertFalse(queued) assertThat(queued).isFalse()
Assertions.assertEquals(commandQueue.size(), 0) assertThat(commandQueue.size()).isEqualTo(0)
} }
@Test @Test
fun isCustomCommandRunning() { fun isCustomCommandRunning() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
val queued1 = commandQueue.customCommand(CustomCommand1(), null) val queued1 = commandQueue.customCommand(CustomCommand1(), null)
@ -323,249 +323,249 @@ class CommandQueueImplementationTest : TestBaseWithProfile() {
commandQueue.pickup() commandQueue.pickup()
// then // then
Assertions.assertTrue(queued1) assertThat(queued1).isTrue()
Assertions.assertTrue(queued2) assertThat(queued2).isTrue()
Assertions.assertTrue(commandQueue.isCustomCommandInQueue(CustomCommand1::class.java)) assertThat(commandQueue.isCustomCommandInQueue(CustomCommand1::class.java)).isTrue()
Assertions.assertTrue(commandQueue.isCustomCommandInQueue(CustomCommand2::class.java)) assertThat(commandQueue.isCustomCommandInQueue(CustomCommand2::class.java)).isTrue()
Assertions.assertFalse(commandQueue.isCustomCommandInQueue(CustomCommand3::class.java)) assertThat(commandQueue.isCustomCommandInQueue(CustomCommand3::class.java)).isFalse()
Assertions.assertTrue(commandQueue.isCustomCommandRunning(CustomCommand1::class.java)) assertThat(commandQueue.isCustomCommandRunning(CustomCommand1::class.java)).isTrue()
Assertions.assertFalse(commandQueue.isCustomCommandRunning(CustomCommand2::class.java)) assertThat(commandQueue.isCustomCommandRunning(CustomCommand2::class.java)).isFalse()
Assertions.assertFalse(commandQueue.isCustomCommandRunning(CustomCommand3::class.java)) assertThat(commandQueue.isCustomCommandRunning(CustomCommand3::class.java)).isFalse()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isSetUserOptionsCommandInQueue() { fun isSetUserOptionsCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.setUserOptions(null) commandQueue.setUserOptions(null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.SET_USER_SETTINGS)) assertThat(commandQueue.isLastScheduled(Command.CommandType.SET_USER_SETTINGS)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.setUserOptions(null) commandQueue.setUserOptions(null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isLoadEventsCommandInQueue() { fun isLoadEventsCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.loadEvents(null) commandQueue.loadEvents(null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.LOAD_EVENTS)) assertThat(commandQueue.isLastScheduled(Command.CommandType.LOAD_EVENTS)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.loadEvents(null) commandQueue.loadEvents(null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isClearAlarmsCommandInQueue() { fun isClearAlarmsCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.clearAlarms(null) commandQueue.clearAlarms(null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.CLEAR_ALARMS)) assertThat(commandQueue.isLastScheduled(Command.CommandType.CLEAR_ALARMS)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.clearAlarms(null) commandQueue.clearAlarms(null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isDeactivateCommandInQueue() { fun isDeactivateCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.deactivate(null) commandQueue.deactivate(null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.DEACTIVATE)) assertThat(commandQueue.isLastScheduled(Command.CommandType.DEACTIVATE)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.deactivate(null) commandQueue.deactivate(null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isUpdateTimeCommandInQueue() { fun isUpdateTimeCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.updateTime(null) commandQueue.updateTime(null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.UPDATE_TIME)) assertThat(commandQueue.isLastScheduled(Command.CommandType.UPDATE_TIME)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.updateTime(null) commandQueue.updateTime(null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isLoadTDDsCommandInQueue() { fun isLoadTDDsCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.loadTDDs(null) commandQueue.loadTDDs(null)
// then // then
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.loadTDDs(null) commandQueue.loadTDDs(null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isLoadHistoryCommandInQueue() { fun isLoadHistoryCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.loadHistory(0, null) commandQueue.loadHistory(0, null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.LOAD_HISTORY)) assertThat(commandQueue.isLastScheduled(Command.CommandType.LOAD_HISTORY)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.loadHistory(0, null) commandQueue.loadHistory(0, null)
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isProfileSetCommandInQueue() { fun isProfileSetCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
testPumpPlugin.isProfileSet = true testPumpPlugin.isProfileSet = true
commandQueue.setProfile(validProfile, false, object : Callback() { commandQueue.setProfile(validProfile, false, object : Callback() {
override fun run() { override fun run() {
Assertions.assertTrue(result.success) assertThat(result.success).isTrue()
Assertions.assertFalse(result.enacted) assertThat(result.enacted).isFalse()
} }
}) })
// then // then
// the same profile -> ignore // the same profile -> ignore
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// different should be added // different should be added
testPumpPlugin.isProfileSet = false testPumpPlugin.isProfileSet = false
commandQueue.setProfile(validProfile, false, object : Callback() { commandQueue.setProfile(validProfile, false, object : Callback() {
override fun run() { override fun run() {
Assertions.assertTrue(result.success) assertThat(result.success).isTrue()
Assertions.assertTrue(result.enacted) assertThat(result.enacted).isTrue()
} }
}) })
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
// next should be ignored // next should be ignored
commandQueue.setProfile(validProfile, false, object : Callback() { commandQueue.setProfile(validProfile, false, object : Callback() {
override fun run() { override fun run() {
Assertions.assertTrue(result.success) assertThat(result.success).isTrue()
} }
}) })
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
testPumpPlugin.isProfileSet = true testPumpPlugin.isProfileSet = true
} }
@Test @Test
fun isStopCommandInQueue() { fun isStopCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.stopPump(null) commandQueue.stopPump(null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.STOP_PUMP)) assertThat(commandQueue.isLastScheduled(Command.CommandType.STOP_PUMP)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isStarCommandInQueue() { fun isStarCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.startPump(null) commandQueue.startPump(null)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.START_PUMP)) assertThat(commandQueue.isLastScheduled(Command.CommandType.START_PUMP)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun isSetTbrNotificationCommandInQueue() { fun isSetTbrNotificationCommandInQueue() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
commandQueue.setTBROverNotification(null, true) commandQueue.setTBROverNotification(null, true)
// then // then
Assertions.assertTrue(commandQueue.isLastScheduled(Command.CommandType.INSIGHT_SET_TBR_OVER_ALARM)) assertThat(commandQueue.isLastScheduled(Command.CommandType.INSIGHT_SET_TBR_OVER_ALARM)).isTrue()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun differentCustomCommandsAllowed() { fun differentCustomCommandsAllowed() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
val queued1 = commandQueue.customCommand(CustomCommand1(), null) val queued1 = commandQueue.customCommand(CustomCommand1(), null)
val queued2 = commandQueue.customCommand(CustomCommand2(), null) val queued2 = commandQueue.customCommand(CustomCommand2(), null)
// then // then
Assertions.assertTrue(queued1) assertThat(queued1).isTrue()
Assertions.assertTrue(queued2) assertThat(queued2).isTrue()
Assertions.assertEquals(2, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(2)
} }
@Test @Test
fun sameCustomCommandNotAllowed() { fun sameCustomCommandNotAllowed() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
val queued1 = commandQueue.customCommand(CustomCommand1(), null) val queued1 = commandQueue.customCommand(CustomCommand1(), null)
val queued2 = commandQueue.customCommand(CustomCommand1(), null) val queued2 = commandQueue.customCommand(CustomCommand1(), null)
// then // then
Assertions.assertTrue(queued1) assertThat(queued1).isTrue()
Assertions.assertFalse(queued2) assertThat(queued2).isFalse()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
} }
@Test @Test
fun readStatusTwiceIsNotAllowed() { fun readStatusTwiceIsNotAllowed() {
// given // given
Assertions.assertEquals(0, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(0)
// when // when
val queued1 = commandQueue.readStatus("1", null) val queued1 = commandQueue.readStatus("1", null)
val queued2 = commandQueue.readStatus("2", null) val queued2 = commandQueue.readStatus("2", null)
// then // then
Assertions.assertTrue(queued1) assertThat(queued1).isTrue()
Assertions.assertFalse(queued2) assertThat(queued2).isFalse()
Assertions.assertEquals(1, commandQueue.size()) assertThat(commandQueue.size()).isEqualTo(1)
Assertions.assertTrue(commandQueue.statusInQueue()) assertThat(commandQueue.statusInQueue()).isTrue()
} }
private class CustomCommand1 : CustomCommand { private class CustomCommand1 : CustomCommand {