Skip to content

Commit eba7081

Browse files
committed
add test to ensure a 2nd pass behaves correctly
This ensures we don't have a off-by-one errors
1 parent 0f718b7 commit eba7081

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

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

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,15 @@ class OperationRepoTests : FunSpec({
381381
val enqueueAndWaitMaxTime = mocks.configModelStore.model.opRepoExecutionInterval / 2
382382
val opRepo = mocks.operationRepo
383383

384-
val executeOperationsCall = Waiter()
385-
coEvery { opRepo.executeOperations(any()) } coAnswers {
386-
executeOperationsCall.wake()
387-
delay(10)
388-
}
384+
val executeOperationsCall = mockExecuteOperations(opRepo)
389385

390386
// When
391387
opRepo.start()
392-
opRepo.enqueue(mockOperation(groupComparisonType = GroupComparisonType.NONE))
388+
opRepo.enqueue(mockOperationNonGroupable())
393389
executeOperationsCall.waitForWake()
394390
val secondEnqueueResult =
395391
withTimeoutOrNull(enqueueAndWaitMaxTime) {
396-
opRepo.enqueueAndWait(mockOperation(groupComparisonType = GroupComparisonType.NONE))
392+
opRepo.enqueueAndWait(mockOperationNonGroupable())
397393
}
398394

399395
// Then
@@ -402,6 +398,34 @@ class OperationRepoTests : FunSpec({
402398
opRepo.executeOperations(any())
403399
}
404400
}
401+
402+
// This ensures there are no off-by-one errors with the same scenario as above, but on a 2nd
403+
// pass of OperationRepo
404+
test("operations enqueued while repo is executing should be executed only after the next opRepoExecutionInterval, 2nd pass") {
405+
// Given
406+
val mocks = Mocks()
407+
mocks.configModelStore.model.opRepoExecutionInterval = 100
408+
val enqueueAndWaitMaxTime = mocks.configModelStore.model.opRepoExecutionInterval / 2
409+
val opRepo = mocks.operationRepo
410+
411+
val executeOperationsCall = mockExecuteOperations(opRepo)
412+
413+
// When
414+
opRepo.start()
415+
opRepo.enqueue(mockOperationNonGroupable())
416+
executeOperationsCall.waitForWake()
417+
opRepo.enqueueAndWait(mockOperationNonGroupable())
418+
val thirdEnqueueResult =
419+
withTimeoutOrNull(enqueueAndWaitMaxTime) {
420+
opRepo.enqueueAndWait(mockOperationNonGroupable())
421+
}
422+
423+
// Then
424+
thirdEnqueueResult shouldBe null
425+
coVerify(exactly = 2) {
426+
opRepo.executeOperations(any())
427+
}
428+
}
405429
}) {
406430
companion object {
407431
private fun mockOperation(
@@ -427,5 +451,17 @@ class OperationRepoTests : FunSpec({
427451

428452
return operation
429453
}
454+
455+
private fun mockOperationNonGroupable() = mockOperation(groupComparisonType = GroupComparisonType.NONE)
456+
457+
private fun mockExecuteOperations(opRepo: OperationRepo): Waiter {
458+
val executeWaiter = Waiter()
459+
coEvery { opRepo.executeOperations(any()) } coAnswers {
460+
executeWaiter.wake()
461+
delay(10)
462+
firstArg<List<OperationRepo.OperationQueueItem>>().forEach { it.waiter?.wake(true) }
463+
}
464+
return executeWaiter
465+
}
430466
}
431467
}

0 commit comments

Comments
 (0)