From bcd647aec5117097ed40c8e86a2e864563e9ba82 Mon Sep 17 00:00:00 2001 From: ovitrif Date: Mon, 23 Nov 2026 14:02:39 +0100 Subject: [PATCH 1/5] fix: keep the app running until database work ends bitkit-core's and the USDT wallet's SQLite databases live in the app group container, and iOS kills an app suspended while it holds a lock on such a file (0xdead10cc). Work on the core and migration queues, and the direct database calls outside them, now hold a background task from when they are queued until they end. Verified: none (unit suite runs next on m1a) --- Bitkit/Managers/UsdtWalletManager.swift | 22 +++--- Bitkit/Services/CoreService.swift | 8 +- Bitkit/Services/ServiceQueue.swift | 91 ++++++++++++++++++++++ Bitkit/Utilities/AppReset.swift | 2 +- Bitkit/ViewModels/BlocktankViewModel.swift | 5 +- BitkitTests/BackgroundTimeTests.swift | 78 +++++++++++++++++++ changelog.d/next/1126.fixed.md | 1 + 7 files changed, 192 insertions(+), 15 deletions(-) create mode 100644 BitkitTests/BackgroundTimeTests.swift create mode 100644 changelog.d/next/1126.fixed.md diff --git a/Bitkit/Managers/UsdtWalletManager.swift b/Bitkit/Managers/UsdtWalletManager.swift index 84dccd7448..53fa409b24 100644 --- a/Bitkit/Managers/UsdtWalletManager.swift +++ b/Bitkit/Managers/UsdtWalletManager.swift @@ -102,7 +102,7 @@ final class UsdtWalletManager { guard !isSendPresented else { throw UsdtWalletError.cannotDisable } let wallet = try await wallet() balance = try await wallet.balance() - transfers = try await wallet.refreshTransfers() + transfers = try await BackgroundTime.during("usdt refreshTransfers") { try await wallet.refreshTransfers() } guard !UsdtPreference.protectsFunds(balance: balance, transfers: transfers) else { throw UsdtWalletError.cannotDisable } if depositsConfigured { var offset: UInt32 = 0 @@ -194,7 +194,7 @@ final class UsdtWalletManager { var settlementError: Error? do { balance = try await wallet.balance() - transfers = try await wallet.refreshTransfers() + transfers = try await BackgroundTime.during("usdt refreshTransfers") { try await wallet.refreshTransfers() } } catch is CancellationError { throw CancellationError() } catch { @@ -207,7 +207,7 @@ final class UsdtWalletManager { if historyRefreshRequested, !isSendPresented, now() >= nextHistoryRefresh { historyRefreshRequested = false nextHistoryRefresh = now().addingTimeInterval(10) - let complete = try await wallet.syncHistory() + let complete = try await BackgroundTime.during("usdt syncHistory") { try await wallet.syncHistory() } historyComplete = historyComplete || complete transfers = try await ServiceQueue.background(.core, wrapErrors: false) { try wallet.history() } let received = Self.newIncomingTransfers( @@ -309,12 +309,12 @@ final class UsdtWalletManager { try beginOperation(requiresNetwork: false) defer { finishOperation() } let wallet = try await wallet(requiresNetwork: false) - try await wallet.restoreBackup(snapshot: snapshot) + try await BackgroundTime.during("usdt restoreBackup") { try await wallet.restoreBackup(snapshot: snapshot) } displayBalanceCache.clear() cachedBalance = nil balance = nil pendingDepositIds.removeAll() - transfers = try wallet.history() + transfers = try BackgroundTime.during("usdt history") { try wallet.history() } historyComplete = false if receiptsSince == nil { receiptsSince = Self.receiptBaseline(now()) @@ -373,7 +373,9 @@ final class UsdtWalletManager { defer { finishOperation() } let core = try await wallet() let credentials = try await loadCredentials() - return try await core.createPaymentProof(transferId: quoteId, binding: binding, mnemonic: credentials.0, passphrase: credentials.1) + return try await BackgroundTime.during("usdt createPaymentProof") { + try await core.createPaymentProof(transferId: quoteId, binding: binding, mnemonic: credentials.0, passphrase: credentials.1) + } } func storedTransfer(_ id: String) async throws -> UsdtTransfer? { @@ -391,9 +393,10 @@ final class UsdtWalletManager { defer { refreshing = false } try await withRateLimit { let wallet = try await wallet() - guard let transfer = try wallet.history().first(where: { $0.id == id }) else { throw UsdtError.InvalidBackup } + guard let transfer = try BackgroundTime.during("usdt history") { try wallet.history() }.first(where: { $0.id == id }) + else { throw UsdtError.InvalidBackup } guard transfer.status == .pending else { return } - transfers = try await wallet.refreshTransfers() + transfers = try await BackgroundTime.during("usdt refreshTransfers") { try await wallet.refreshTransfers() } } } @@ -415,7 +418,8 @@ final class UsdtWalletManager { let deadline = clock.now.advanced(by: .seconds(5)) while clock.now < deadline { try Task.checkCancellation() - let transfer = try await Task.detached { try await wallet.checkRecentExecution(id: id) }.value + let transfer = try await Task + .detached { try await BackgroundTime.during("usdt checkRecentExecution") { try await wallet.checkRecentExecution(id: id) } }.value try Task.checkCancellation() guard let transfer else { return } transfers.removeAll { $0.id == transfer.id } diff --git a/Bitkit/Services/CoreService.swift b/Bitkit/Services/CoreService.swift index da9d481ce0..595383b893 100644 --- a/Bitkit/Services/CoreService.swift +++ b/Bitkit/Services/CoreService.swift @@ -223,7 +223,7 @@ class ActivityService { func isActivitySeen(id: String, walletId: String = WalletScope.default) async -> Bool { do { - if let activity = try getActivityById(walletId: walletId, activityId: id) { + if let activity = try BackgroundTime.during("isActivitySeen", { try getActivityById(walletId: walletId, activityId: id) }) { switch activity { case let .onchain(onchain): return onchain.seenAt != nil @@ -1184,7 +1184,9 @@ class ActivityService { } } - let closedChannels = try getAllClosedChannels(sortDirection: .desc) + let closedChannels = try BackgroundTime.during("findClosedChannelForTransaction") { + try getAllClosedChannels(sortDirection: .desc) + } guard !closedChannels.isEmpty else { return nil } let details = if let provided = transactionDetails { @@ -2765,7 +2767,7 @@ class CoreService { private init(walletIndex: Int = 0) { self.walletIndex = walletIndex - _ = try! initDb(basePath: Env.bitkitCoreStorage(walletIndex: walletIndex).path) + _ = try! BackgroundTime.during("initDb") { try initDb(basePath: Env.bitkitCoreStorage(walletIndex: walletIndex).path) } // First thing ever added to the core queue so guarenteed to run first before any of above functions on the same queue ServiceQueue.background(.core) { diff --git a/Bitkit/Services/ServiceQueue.swift b/Bitkit/Services/ServiceQueue.swift index ffd6ceebbc..46cd9e04e5 100644 --- a/Bitkit/Services/ServiceQueue.swift +++ b/Bitkit/Services/ServiceQueue.swift @@ -1,4 +1,5 @@ import Foundation +import UIKit /// Handles app services each on it's own dedicated queue class ServiceQueue { @@ -31,6 +32,16 @@ class ServiceQueue { return ServiceQueue.backupQueue } } + + /// bitkit-core's SQLite databases live in the app group container, and iOS kills an app suspended while it holds a lock on + /// such a file (0xdead10cc). Work on these queues therefore keeps the app running in the background until it ends. + var usesDatabases: Bool { + self == .core || self == .migration + } + } + + private static func requestBackgroundTime(for service: ServiceTypes, functionName: String) -> BackgroundTime? { + service.usesDatabases ? BackgroundTime(name: "\(service) \(functionName)") : nil } /// Executes a thread blocking function on chosen service queue @@ -43,8 +54,10 @@ class ServiceQueue { functionName: String = #function) async throws -> T { let startTime = CFAbsoluteTimeGetCurrent() + let backgroundTime = requestBackgroundTime(for: service, functionName: functionName) let result = try await withCheckedThrowingContinuation { continuation in service.queue.async { + defer { backgroundTime?.end() } do { let res = try blocking() continuation.resume(with: .success(res)) @@ -71,9 +84,11 @@ class ServiceQueue { { let startTime = CFAbsoluteTimeGetCurrent() + let backgroundTime = requestBackgroundTime(for: service, functionName: functionName) return try await withCheckedThrowingContinuation { continuation in service.queue.async { Task { + defer { backgroundTime?.end() } do { let result = try await execute() continuation.resume(returning: result) @@ -111,7 +126,9 @@ class ServiceQueue { ) { let startTime = CFAbsoluteTimeGetCurrent() + let backgroundTime = requestBackgroundTime(for: service, functionName: functionName) service.queue.async { + defer { backgroundTime?.end() } do { let result = try execute() let timeElapsed = Double(round(100 * (CFAbsoluteTimeGetCurrent() - startTime)) / 100) @@ -137,8 +154,10 @@ class ServiceQueue { ) { let startTime = CFAbsoluteTimeGetCurrent() + let backgroundTime = requestBackgroundTime(for: service, functionName: functionName) service.queue.async { Task { + defer { backgroundTime?.end() } do { let result = try await execute() let timeElapsed = Double(round(100 * (CFAbsoluteTimeGetCurrent() - startTime)) / 100) @@ -153,3 +172,75 @@ class ServiceQueue { } } } + +/// Asks iOS for background running time until `end()` is called, the instance is released or the time expires. +/// Its state is guarded by `lock`. +final class BackgroundTime: @unchecked Sendable { + typealias Begin = (_ name: String, _ expiration: @escaping () -> Void) -> UIBackgroundTaskIdentifier + typealias End = (UIBackgroundTaskIdentifier) -> Void + + private let lock = NSLock() + private let endTask: End + private var identifier: UIBackgroundTaskIdentifier = .invalid + private var ended = false + + init(name: String, begin: Begin = BackgroundTime.beginTask, end: @escaping End = BackgroundTime.endTask) { + endTask = end + let identifier = begin(name) { [weak self] in + self?.end() + } + // The expiration handler can run before the identifier is stored. + let endedAlready = lock.withLock { + if !ended { + self.identifier = identifier + } + return ended + } + if endedAlready, identifier != .invalid { + end(identifier) + } + } + + deinit { + end() + } + + func end() { + let identifier = lock.withLock { + ended = true + defer { self.identifier = .invalid } + return self.identifier + } + if identifier != .invalid { + endTask(identifier) + } + } + + /// Runs `body` with background running time, for database work outside `ServiceQueue`'s database queues. + static func during(_ name: String, _ body: () async throws -> T) async rethrows -> T { + let backgroundTime = BackgroundTime(name: name) + defer { backgroundTime.end() } + return try await body() + } + + /// Runs `body` with background running time, for database work outside `ServiceQueue`'s database queues. + static func during(_ name: String, _ body: () throws -> T) rethrows -> T { + let backgroundTime = BackgroundTime(name: name) + defer { backgroundTime.end() } + return try body() + } + + static func beginTask(name: String, expiration: @escaping () -> Void) -> UIBackgroundTaskIdentifier { + #if BITKIT_NOTIFICATION_EXTENSION + return .invalid + #else + return UIApplication.shared.beginBackgroundTask(withName: name, expirationHandler: expiration) + #endif + } + + static func endTask(_ identifier: UIBackgroundTaskIdentifier) { + #if !BITKIT_NOTIFICATION_EXTENSION + UIApplication.shared.endBackgroundTask(identifier) + #endif + } +} diff --git a/Bitkit/Utilities/AppReset.swift b/Bitkit/Utilities/AppReset.swift index de1529e881..9d97fea2f9 100644 --- a/Bitkit/Utilities/AppReset.swift +++ b/Bitkit/Utilities/AppReset.swift @@ -98,7 +98,7 @@ final class AppReset: WipeStepExecutor { case .wipeDatabases: // bitkit-core wipes only databases it has opened, which `CoreService` does when it is created. _ = CoreService.shared - let coreWipeResult = try await wipeAllDatabases() + let coreWipeResult = try await BackgroundTime.during("wipeAllDatabases") { try await wipeAllDatabases() } Logger.info("Core DB wipe: \(coreWipeResult)") case .clearPubkyState: // Clear any live Pubky runtime state and cached profile images. diff --git a/Bitkit/ViewModels/BlocktankViewModel.swift b/Bitkit/ViewModels/BlocktankViewModel.swift index 3dc238235f..096dc98189 100644 --- a/Bitkit/ViewModels/BlocktankViewModel.swift +++ b/Bitkit/ViewModels/BlocktankViewModel.swift @@ -65,8 +65,9 @@ class BlocktankViewModel: ObservableObject { } func refreshInfo() async throws { - info = try await getInfo(refresh: false) // Instant set cached info to state before refreshing - info = try await getInfo(refresh: true) + info = try await BackgroundTime + .during("blocktank getInfo") { try await getInfo(refresh: false) } // Instant set cached info to state before refreshing + info = try await BackgroundTime.during("blocktank getInfo") { try await getInfo(refresh: true) } coreService.blocktank.notifyStateChanged() } diff --git a/BitkitTests/BackgroundTimeTests.swift b/BitkitTests/BackgroundTimeTests.swift new file mode 100644 index 0000000000..8d556a51e1 --- /dev/null +++ b/BitkitTests/BackgroundTimeTests.swift @@ -0,0 +1,78 @@ +@testable import Bitkit +import UIKit +import XCTest + +final class BackgroundTimeTests: XCTestCase { + private final class FakeTasks: @unchecked Sendable { + let lock = NSLock() + var began: [String] = [] + var ended: [UIBackgroundTaskIdentifier] = [] + var expiration: (() -> Void)? + var expireOnBegin = false + + func begin(name: String, expiration: @escaping () -> Void) -> UIBackgroundTaskIdentifier { + lock.withLock { + began.append(name) + self.expiration = expiration + } + if expireOnBegin { + expiration() + } + return UIBackgroundTaskIdentifier(rawValue: 7) + } + + func end(_ identifier: UIBackgroundTaskIdentifier) { + lock.withLock { ended.append(identifier) } + } + + func make(_ name: String = "work") -> BackgroundTime { + BackgroundTime(name: name, begin: begin, end: end) + } + } + + func testEndEndsTheTaskOnce() { + let tasks = FakeTasks() + let backgroundTime = tasks.make() + + backgroundTime.end() + backgroundTime.end() + + XCTAssertEqual(tasks.began, ["work"]) + XCTAssertEqual(tasks.ended, [UIBackgroundTaskIdentifier(rawValue: 7)]) + } + + func testExpirationEndsTheTask() { + let tasks = FakeTasks() + let backgroundTime = tasks.make() + + tasks.expiration?() + backgroundTime.end() + + XCTAssertEqual(tasks.ended, [UIBackgroundTaskIdentifier(rawValue: 7)]) + } + + func testExpirationBeforeTheIdentifierIsStoredStillEndsTheTask() { + let tasks = FakeTasks() + tasks.expireOnBegin = true + + let backgroundTime = tasks.make() + backgroundTime.end() + + XCTAssertEqual(tasks.ended, [UIBackgroundTaskIdentifier(rawValue: 7)]) + } + + func testReleasingEndsTheTask() { + let tasks = FakeTasks() + _ = tasks.make() + + XCTAssertEqual(tasks.ended, [UIBackgroundTaskIdentifier(rawValue: 7)]) + } + + func testOnlyDatabaseQueuesAskForBackgroundTime() { + XCTAssertTrue(ServiceQueue.ServiceTypes.core.usesDatabases) + XCTAssertTrue(ServiceQueue.ServiceTypes.migration.usesDatabases) + XCTAssertFalse(ServiceQueue.ServiceTypes.ldk.usesDatabases) + XCTAssertFalse(ServiceQueue.ServiceTypes.forex.usesDatabases) + XCTAssertFalse(ServiceQueue.ServiceTypes.backup.usesDatabases) + } +} diff --git a/changelog.d/next/1126.fixed.md b/changelog.d/next/1126.fixed.md new file mode 100644 index 0000000000..9ed310f40e --- /dev/null +++ b/changelog.d/next/1126.fixed.md @@ -0,0 +1 @@ +Fixed iOS closing the app when it goes to the background during wallet database work. From eec1ec16cb5c8838a0fd156b956c15e32e99c5ce Mon Sep 17 00:00:00 2001 From: ovitrif Date: Mon, 23 Nov 2026 14:49:40 +0100 Subject: [PATCH 2/5] fix: build the usdt history lookup without a confusable closure Verified: swiftformat --lint on the file; unit suite rerun pending. --- Bitkit/Managers/UsdtWalletManager.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Bitkit/Managers/UsdtWalletManager.swift b/Bitkit/Managers/UsdtWalletManager.swift index 53fa409b24..589fb9a124 100644 --- a/Bitkit/Managers/UsdtWalletManager.swift +++ b/Bitkit/Managers/UsdtWalletManager.swift @@ -393,8 +393,8 @@ final class UsdtWalletManager { defer { refreshing = false } try await withRateLimit { let wallet = try await wallet() - guard let transfer = try BackgroundTime.during("usdt history") { try wallet.history() }.first(where: { $0.id == id }) - else { throw UsdtError.InvalidBackup } + let history = try BackgroundTime.during("usdt history") { try wallet.history() } + guard let transfer = history.first(where: { $0.id == id }) else { throw UsdtError.InvalidBackup } guard transfer.status == .pending else { return } transfers = try await BackgroundTime.during("usdt refreshTransfers") { try await wallet.refreshTransfers() } } From 8f1ecd00f9b0fa7e4709d53bc74e4d4ad770091c Mon Sep 17 00:00:00 2001 From: ovitrif Date: Mon, 23 Nov 2026 15:33:47 +0100 Subject: [PATCH 3/5] fix: build the notification extension with background time The extension compiles ServiceQueue.swift, and a build that sets SWIFT_ACTIVE_COMPILATION_CONDITIONS on the command line (the E2E build) drops its BITKIT_NOTIFICATION_EXTENSION flag, so UIApplication.shared failed to compile there. The app now installs the background task calls at launch; elsewhere they do nothing. Verified: none yet (E2E-flag build and unit suite run next). --- Bitkit/BitkitApp.swift | 4 ++++ Bitkit/Services/ServiceQueue.swift | 21 +++++++-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Bitkit/BitkitApp.swift b/Bitkit/BitkitApp.swift index 5b7c7e9753..f136959393 100644 --- a/Bitkit/BitkitApp.swift +++ b/Bitkit/BitkitApp.swift @@ -72,6 +72,10 @@ class AppDelegate: NSObject, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { + BackgroundTime.beginTask = { name, expiration in + UIApplication.shared.beginBackgroundTask(withName: name, expirationHandler: expiration) + } + BackgroundTime.endTask = { UIApplication.shared.endBackgroundTask($0) } UNUserNotificationCenter.current().delegate = self SparkExitManager.registerBackgroundTask() diff --git a/Bitkit/Services/ServiceQueue.swift b/Bitkit/Services/ServiceQueue.swift index 46cd9e04e5..a607f8a1f2 100644 --- a/Bitkit/Services/ServiceQueue.swift +++ b/Bitkit/Services/ServiceQueue.swift @@ -184,7 +184,9 @@ final class BackgroundTime: @unchecked Sendable { private var identifier: UIBackgroundTaskIdentifier = .invalid private var ended = false - init(name: String, begin: Begin = BackgroundTime.beginTask, end: @escaping End = BackgroundTime.endTask) { + init(name: String, begin: Begin? = nil, end: End? = nil) { + let begin = begin ?? BackgroundTime.beginTask + let end = end ?? BackgroundTime.endTask endTask = end let identifier = begin(name) { [weak self] in self?.end() @@ -230,17 +232,8 @@ final class BackgroundTime: @unchecked Sendable { return try body() } - static func beginTask(name: String, expiration: @escaping () -> Void) -> UIBackgroundTaskIdentifier { - #if BITKIT_NOTIFICATION_EXTENSION - return .invalid - #else - return UIApplication.shared.beginBackgroundTask(withName: name, expirationHandler: expiration) - #endif - } - - static func endTask(_ identifier: UIBackgroundTaskIdentifier) { - #if !BITKIT_NOTIFICATION_EXTENSION - UIApplication.shared.endBackgroundTask(identifier) - #endif - } + /// The app sets these at launch. The notification extension, which compiles this file too, has no `UIApplication`, so its + /// work runs without background time. + nonisolated(unsafe) static var beginTask: Begin = { _, _ in .invalid } + nonisolated(unsafe) static var endTask: End = { _ in } } From 9306d7a3a35fc28b9394ca23e00ebbae6e3ba031 Mon Sep 17 00:00:00 2001 From: ovitrif Date: Mon, 23 Nov 2026 15:55:14 +0100 Subject: [PATCH 4/5] fix: keep the app running while a usdt quote is saved Quoting saves the quote to the USDT wallet's SQLite in the app group, so it now holds background time like the other USDT database calls. Verified: swiftformat --lint; build and unit suite run next. --- Bitkit/Managers/UsdtWalletManager.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Bitkit/Managers/UsdtWalletManager.swift b/Bitkit/Managers/UsdtWalletManager.swift index 589fb9a124..ec4d24596e 100644 --- a/Bitkit/Managers/UsdtWalletManager.swift +++ b/Bitkit/Managers/UsdtWalletManager.swift @@ -281,7 +281,9 @@ final class UsdtWalletManager { if recipient.trimmingCharacters(in: .whitespacesAndNewlines).caseInsensitiveCompare(wallet.receiveAddress()) == .orderedSame { throw UsdtWalletError.selfSend } - return try await wallet.quoteTransfer(recipient: recipient, amount: value, destination: destination) + return try await BackgroundTime.during("usdt quoteTransfer") { + try await wallet.quoteTransfer(recipient: recipient, amount: value, destination: destination) + } } } @@ -293,7 +295,9 @@ final class UsdtWalletManager { if recipient.trimmingCharacters(in: .whitespacesAndNewlines).caseInsensitiveCompare(wallet.receiveAddress()) == .orderedSame { throw UsdtWalletError.selfSend } - return try await wallet.quoteMaxTransfer(recipient: recipient, destination: destination) + return try await BackgroundTime.during("usdt quoteMaxTransfer") { + try await wallet.quoteMaxTransfer(recipient: recipient, destination: destination) + } } } From 6b6a3a385482ea83069cc0a3677d14f0b0da3cc5 Mon Sep 17 00:00:00 2001 From: ovitrif Date: Mon, 23 Nov 2026 16:37:46 +0100 Subject: [PATCH 5/5] test: cover background time through the app's hooks Verified: swiftformat --lint --- BitkitTests/BackgroundTimeTests.swift | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/BitkitTests/BackgroundTimeTests.swift b/BitkitTests/BackgroundTimeTests.swift index 8d556a51e1..5ba751ee9c 100644 --- a/BitkitTests/BackgroundTimeTests.swift +++ b/BitkitTests/BackgroundTimeTests.swift @@ -68,6 +68,48 @@ final class BackgroundTimeTests: XCTestCase { XCTAssertEqual(tasks.ended, [UIBackgroundTaskIdentifier(rawValue: 7)]) } + func testWithoutTheAppsHooksNoTaskIsEnded() { + let tasks = FakeTasks() + let backgroundTime = BackgroundTime(name: "work", begin: { _, _ in .invalid }, end: tasks.end) + + backgroundTime.end() + + XCTAssertEqual(tasks.ended, []) + } + + /// Runs `body` with `tasks` installed as the app's hooks. + private func withHooks(_ tasks: FakeTasks, _ body: () async throws -> Void) async rethrows { + let (begin, end) = (BackgroundTime.beginTask, BackgroundTime.endTask) + BackgroundTime.beginTask = tasks.begin + BackgroundTime.endTask = tasks.end + defer { (BackgroundTime.beginTask, BackgroundTime.endTask) = (begin, end) } + try await body() + } + + func testDuringEndsTheTaskAlsoWhenTheBodyThrows() async { + struct Failure: Error {} + let tasks = FakeTasks() + + await withHooks(tasks) { + _ = try? await BackgroundTime.during("quote") { () async throws -> Int in throw Failure() } + } + + XCTAssertEqual(tasks.began, ["quote"]) + XCTAssertEqual(tasks.ended, [UIBackgroundTaskIdentifier(rawValue: 7)]) + } + + func testDatabaseQueueWorkHoldsBackgroundTimeUntilItEnds() async throws { + let tasks = FakeTasks() + + try await withHooks(tasks) { + _ = try await ServiceQueue.background(.core) { 1 } + _ = try await ServiceQueue.background(.ldk) { 1 } + } + + XCTAssertEqual(tasks.began.count, 1) + XCTAssertEqual(tasks.ended, [UIBackgroundTaskIdentifier(rawValue: 7)]) + } + func testOnlyDatabaseQueuesAskForBackgroundTime() { XCTAssertTrue(ServiceQueue.ServiceTypes.core.usesDatabases) XCTAssertTrue(ServiceQueue.ServiceTypes.migration.usesDatabases)