Skip to content

Commit 5c90d2c

Browse files
authored
Merge pull request #74 from normalengineering/fix/ci-lock-test
fix: ci/tests for lock tests
2 parents 7b1cc54 + 262198b commit 5c90d2c

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

‎NormalTests/Shared/ShieldReconcilerTests.swift‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ struct ShieldReconcilerTests {
9797
#expect(store.manualBlockState?.applications.blockedTokens == [TokenFixtures.app(4)])
9898
}
9999

100-
@Test(.enabled(if: FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: SharedConstants.appGroupID) != nil))
101-
func concurrentReconcilesNeverOverlap() async {
100+
@Test func concurrentReconcilesNeverOverlap() async {
101+
let lockURL = FileManager.default.temporaryDirectory.appending(path: "\(UUID().uuidString).lock")
102+
defer { try? FileManager.default.removeItem(at: lockURL) }
102103
let store = FakeSharedStore()
103104
store.manualBlockState = ManualBlockState()
104105
let shields = RecordingShields()
@@ -107,7 +108,7 @@ struct ShieldReconcilerTests {
107108
await withTaskGroup(of: Void.self) { group in
108109
for id in UInt8(1) ... 8 {
109110
group.addTask {
110-
ShieldReconciler.reconcile(sharedStore: store, shields: shields, monitoredActivities: []) {
111+
ShieldReconciler.reconcile(sharedStore: store, shields: shields, monitoredActivities: [], lockURL: lockURL) {
111112
$0.block(TokenFixtures.selection(apps: [id]), customDomains: [], at: .now)
112113
}
113114
}

‎Shared/ShieldReconciler.swift‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ nonisolated protocol ShieldApplying: AnyObject {
77
}
88

99
nonisolated enum SharedFileLock {
10-
private static var url: URL? {
10+
private static var defaultURL: URL? {
1111
FileManager.default
1212
.containerURL(forSecurityApplicationGroupIdentifier: SharedConstants.appGroupID)?
1313
.appending(path: "shields.lock")
1414
}
1515

16-
static func withLock<T>(_ body: () throws -> T) rethrows -> T? {
17-
guard let path = url?.path(percentEncoded: false) else { return try body() }
16+
static func withLock<T>(at url: URL? = nil, _ body: () throws -> T) rethrows -> T? {
17+
guard let path = (url ?? defaultURL)?.path(percentEncoded: false) else { return try body() }
1818
let descriptor = open(path, O_CREAT | O_RDWR, 0o644)
1919
guard descriptor >= 0 else {
2020
return errno == EPERM || errno == EACCES ? nil : try body()
@@ -35,9 +35,10 @@ nonisolated enum ShieldReconciler {
3535
shields: some ShieldApplying,
3636
monitoredActivities: Set<String>,
3737
at date: Date = .now,
38+
lockURL: URL? = nil,
3839
update: (inout ManualBlockState) -> Void = { _ in }
3940
) -> ShieldPlan? {
40-
SharedFileLock.withLock {
41+
SharedFileLock.withLock(at: lockURL) {
4142
let schedules = sharedStore.loadSchedules()
4243
.filter { $0.isEffectivelyEnabled(monitoredActivities: monitoredActivities) }
4344
let timedUnblocks = sharedStore.loadTimedUnblocks()

0 commit comments

Comments
 (0)