apple_kit

Apple-native services for Radroots iOS and macOS apps
git clone https://radroots.dev/git/apple_kit.git
Log | Files | Refs | README

RadrootsBackgroundTaskTesting.swift (2603B)


      1 import Foundation
      2 import RadrootsKit
      3 
      4 public actor RadrootsFakeBackgroundTaskScheduler: RadrootsBackgroundTaskScheduler {
      5     private var pendingTaskSnapshots: [RadrootsBackgroundTaskIdentifier: RadrootsBackgroundTaskSnapshot]
      6     private var submittedRequestsValue: [RadrootsBackgroundTaskRequest]
      7     private var cancelledIdentifiersValue: [RadrootsBackgroundTaskIdentifier]
      8     private var cancelAllCountValue: Int
      9     private var submitOutcome: Result<Void, RadrootsBackgroundTaskError>
     10     private let submittedAt: Date
     11 
     12     public init(
     13         pendingTasks: [RadrootsBackgroundTaskSnapshot] = [],
     14         submitOutcome: Result<Void, RadrootsBackgroundTaskError> = .success(()),
     15         submittedAt: Date = Date(timeIntervalSince1970: 0)
     16     ) {
     17         self.pendingTaskSnapshots = Dictionary(uniqueKeysWithValues: pendingTasks.map { ($0.identifier, $0) })
     18         self.submittedRequestsValue = []
     19         self.cancelledIdentifiersValue = []
     20         self.cancelAllCountValue = 0
     21         self.submitOutcome = submitOutcome
     22         self.submittedAt = submittedAt
     23     }
     24 
     25     public func setSubmitOutcome(_ outcome: Result<Void, RadrootsBackgroundTaskError>) {
     26         submitOutcome = outcome
     27     }
     28 
     29     public func submit(_ request: RadrootsBackgroundTaskRequest) async throws -> RadrootsBackgroundTaskSnapshot {
     30         submittedRequestsValue.append(request)
     31         switch submitOutcome {
     32         case .success:
     33             let snapshot = try RadrootsBackgroundTaskSnapshot(request: request, submittedAt: submittedAt)
     34             pendingTaskSnapshots[request.identifier] = snapshot
     35             return snapshot
     36         case .failure(let error):
     37             throw error
     38         }
     39     }
     40 
     41     public func cancel(_ identifier: RadrootsBackgroundTaskIdentifier) async throws {
     42         cancelledIdentifiersValue.append(identifier)
     43         pendingTaskSnapshots.removeValue(forKey: identifier)
     44     }
     45 
     46     public func cancelAll() async throws {
     47         cancelAllCountValue += 1
     48         pendingTaskSnapshots.removeAll()
     49     }
     50 
     51     public func pendingTasks() async throws -> [RadrootsBackgroundTaskSnapshot] {
     52         pendingTaskSnapshots.values.sorted { lhs, rhs in
     53             lhs.identifier < rhs.identifier
     54         }
     55     }
     56 
     57     public var submittedRequests: [RadrootsBackgroundTaskRequest] {
     58         submittedRequestsValue
     59     }
     60 
     61     public var submittedRequestCount: Int {
     62         submittedRequestsValue.count
     63     }
     64 
     65     public var cancelledIdentifiers: [RadrootsBackgroundTaskIdentifier] {
     66         cancelledIdentifiersValue
     67     }
     68 
     69     public var cancelAllCount: Int {
     70         cancelAllCountValue
     71     }
     72 }