From 262198b2b6b2bc275adf05a47facb2ad76e3a21c Mon Sep 17 00:00:00 2001 From: ykisana Date: Tue, 20 Oct 2026 12:37:00 -0600 Subject: [PATCH] fix: ci/tests for lock tests --- NormalTests/Shared/ShieldReconcilerTests.swift | 7 ++++--- Shared/ShieldReconciler.swift | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/NormalTests/Shared/ShieldReconcilerTests.swift b/NormalTests/Shared/ShieldReconcilerTests.swift index 474930e..60155bf 100644 --- a/NormalTests/Shared/ShieldReconcilerTests.swift +++ b/NormalTests/Shared/ShieldReconcilerTests.swift @@ -97,8 +97,9 @@ struct ShieldReconcilerTests { #expect(store.manualBlockState?.applications.blockedTokens == [TokenFixtures.app(4)]) } - @Test(.enabled(if: FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: SharedConstants.appGroupID) != nil)) - func concurrentReconcilesNeverOverlap() async { + @Test func concurrentReconcilesNeverOverlap() async { + let lockURL = FileManager.default.temporaryDirectory.appending(path: "\(UUID().uuidString).lock") + defer { try? FileManager.default.removeItem(at: lockURL) } let store = FakeSharedStore() store.manualBlockState = ManualBlockState() let shields = RecordingShields() @@ -107,7 +108,7 @@ struct ShieldReconcilerTests { await withTaskGroup(of: Void.self) { group in for id in UInt8(1) ... 8 { group.addTask { - ShieldReconciler.reconcile(sharedStore: store, shields: shields, monitoredActivities: []) { + ShieldReconciler.reconcile(sharedStore: store, shields: shields, monitoredActivities: [], lockURL: lockURL) { $0.block(TokenFixtures.selection(apps: [id]), customDomains: [], at: .now) } } diff --git a/Shared/ShieldReconciler.swift b/Shared/ShieldReconciler.swift index d3e6e51..e8f1ae6 100644 --- a/Shared/ShieldReconciler.swift +++ b/Shared/ShieldReconciler.swift @@ -7,14 +7,14 @@ nonisolated protocol ShieldApplying: AnyObject { } nonisolated enum SharedFileLock { - private static var url: URL? { + private static var defaultURL: URL? { FileManager.default .containerURL(forSecurityApplicationGroupIdentifier: SharedConstants.appGroupID)? .appending(path: "shields.lock") } - static func withLock(_ body: () throws -> T) rethrows -> T? { - guard let path = url?.path(percentEncoded: false) else { return try body() } + static func withLock(at url: URL? = nil, _ body: () throws -> T) rethrows -> T? { + guard let path = (url ?? defaultURL)?.path(percentEncoded: false) else { return try body() } let descriptor = open(path, O_CREAT | O_RDWR, 0o644) guard descriptor >= 0 else { return errno == EPERM || errno == EACCES ? nil : try body() @@ -35,9 +35,10 @@ nonisolated enum ShieldReconciler { shields: some ShieldApplying, monitoredActivities: Set, at date: Date = .now, + lockURL: URL? = nil, update: (inout ManualBlockState) -> Void = { _ in } ) -> ShieldPlan? { - SharedFileLock.withLock { + SharedFileLock.withLock(at: lockURL) { let schedules = sharedStore.loadSchedules() .filter { $0.isEffectivelyEnabled(monitoredActivities: monitoredActivities) } let timedUnblocks = sharedStore.loadTimedUnblocks()