From 37ab633136cbabf6c08323fd019832cc57d4df4a Mon Sep 17 00:00:00 2001 From: Tuan Pham <103537251+phantumcode@users.noreply.github.com> Date: Fri, 20 Oct 2023 15:26:02 -0500 Subject: [PATCH] fix: update session timed out error handling (#65) * fix: update session timed out error to be non-blocking * chore: remove duplicate code * chore: fix failed timed out unit test --- ...ionViewModel+FaceDetectionResultHandler.swift | 16 ++++++++++------ Tests/FaceLivenessTests/LivenessTests.swift | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift index a7458ed4..28fbbb42 100644 --- a/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift +++ b/Sources/FaceLiveness/Views/Liveness/FaceLivenessDetectionViewModel+FaceDetectionResultHandler.swift @@ -89,9 +89,7 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { noFitStartTime = Date() } if let elapsedTime = noFitStartTime?.timeIntervalSinceNow, abs(elapsedTime) >= noFitTimeoutInterval { - self.livenessState - .unrecoverableStateEncountered(.timedOut) - self.captureSession.stopRunning() + handleSessionTimedOut() } } @@ -100,9 +98,7 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { noFitStartTime = Date() } if let elapsedTime = noFitStartTime?.timeIntervalSinceNow, abs(elapsedTime) >= noFitTimeoutInterval { - self.livenessState - .unrecoverableStateEncountered(.timedOut) - self.captureSession.stopRunning() + handleSessionTimedOut() } } @@ -130,4 +126,12 @@ extension FaceLivenessDetectionViewModel: FaceDetectionResultHandler { } } } + + private func handleSessionTimedOut() { + DispatchQueue.main.async { + self.livenessState + .unrecoverableStateEncountered(.timedOut) + self.captureSession.stopRunning() + } + } } diff --git a/Tests/FaceLivenessTests/LivenessTests.swift b/Tests/FaceLivenessTests/LivenessTests.swift index 1b3c13b2..39f84f53 100644 --- a/Tests/FaceLivenessTests/LivenessTests.swift +++ b/Tests/FaceLivenessTests/LivenessTests.swift @@ -152,6 +152,7 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase { XCTAssertNotEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) try await Task.sleep(seconds: 1) self.viewModel.handleNoFaceFit(instruction: .tooFar(nearnessPercentage: 0.2), percentage: 0.2) + try await Task.sleep(seconds: 1) XCTAssertEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) } @@ -168,6 +169,7 @@ final class FaceLivenessDetectionViewModelTestCase: XCTestCase { XCTAssertNotEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) try await Task.sleep(seconds: 1) self.viewModel.handleNoFaceDetected() + try await Task.sleep(seconds: 1) XCTAssertEqual(self.viewModel.livenessState.state, .encounteredUnrecoverableError(.timedOut)) } }