Skip to content

Commit 354115e

Browse files
committed
test proving loadSavedOperations index issue
Test proving that loadSavedOperations can throw indexOutOfBoundsException. Real world scenario is this can happen if a few operations are added when the device is offline then the app is restarted.
1 parent 05a00a2 commit 354115e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/internal/operations/OperationRepoTests.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,26 @@ class OperationRepoTests : FunSpec({
630630
mocks.operationRepo.queue.size shouldBe 1
631631
mocks.operationRepo.queue.first().operation shouldBe op
632632
}
633+
634+
// Real world scenario is this can happen if a few operations are added when the device is
635+
// offline then the app is restarted.
636+
test("ensure loadSavedOperations doesn't index out of bounds on queue when duplicates exist") {
637+
// Given
638+
val mocks = Mocks()
639+
val op1 = mockOperation()
640+
val op2 = mockOperation()
641+
642+
repeat(2) { mocks.operationModelStore.add(op1) }
643+
mocks.operationModelStore.add(op2)
644+
645+
// When
646+
mocks.operationRepo.loadSavedOperations()
647+
648+
// Then
649+
mocks.operationRepo.queue.size shouldBe 2
650+
mocks.operationRepo.queue[0].operation shouldBe op1
651+
mocks.operationRepo.queue[1].operation shouldBe op2
652+
}
633653
}) {
634654
companion object {
635655
private fun mockOperation(

0 commit comments

Comments
 (0)