Skip to content

Commit

Permalink
fix: when queue cannot find task, expect queue runs next task
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian committed Oct 26, 2023
1 parent f2c7f0c commit b5ad08a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Common/Background Queue/QueueRunRequest.swift
Expand Up @@ -95,7 +95,7 @@ public class CioQueueRunRequest: QueueRunRequest {

// The task failed to execute like a HTTP failure. Update `lastFailedTask`.
updateWhileLoopLogicVariables(didTaskFail: true, taskJustExecuted: nextTaskToRunInventoryItem)
break
continue // quit loop early and move to next task.
}

logger.debug("queue tasks left to run: \(queueInventory.count)")
Expand Down
34 changes: 34 additions & 0 deletions Tests/Common/Background Queue/QueueRunRequestTest.swift
Expand Up @@ -125,6 +125,40 @@ class QueueRunRequestTest: UnitTest {

// MARK: runTasks

func test_runTasks_givenQueueTaskNotFoundInStorage_expectToContinueRunningQueue() {
// Setup the queue with 2 tasks.
// 1st task that cannot be found in queue storage.
// 2nd task that can be found and is expected to be executed.
let givenInventoryTaskStorageCannotFind = QueueTaskMetadata.random
let givenInventoryTaskInStorage = QueueTaskMetadata.random

var givenInventory = [givenInventoryTaskStorageCannotFind, givenInventoryTaskInStorage]
storageMock.getInventoryReturnValue = givenInventory

storageMock.getClosure = { taskStorageId in
if taskStorageId == givenInventoryTaskInStorage.taskPersistedId {
return QueueTask.random // queue storage found task in file system for 1 of the tasks
}

return nil
}

// setup runner and storage to run tasks successfully.
runnerMock.runTaskClosure = { _, onComplete in
onComplete(.success(()))
}
storageMock.deleteClosure = { taskStorageId in
givenInventory.removeAll(where: { $0.taskPersistedId == taskStorageId })
return true
}

runRequest.runTasks()

XCTAssertEqual(storageMock.getCallsCount, 2) // expect queue tried to get queue task from storage
XCTAssertEqual(runnerMock.runTaskCallsCount, 1)
XCTAssertEqual(requestManagerMock.requestCompleteCallsCount, 1)
}

// runTasks() performs async operations inside of it. A stackoverflow/infinite loop can occur if these async operations do not run in sequential order.
func test_runTasks_expectAsyncOperationsToPerformSequentially() {
// Events, in order, that are expected to happen
Expand Down

0 comments on commit b5ad08a

Please sign in to comment.