Skip to content

Commit d188bfb

Browse files
committed
Add failing test for persistence locking order w.r.t. Dependencies
1 parent f675de1 commit d188bfb

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Tests/SharingTests/SharedTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Dependencies
12
import Foundation
23
import IdentifiedCollections
34
import PerceptionCore
@@ -35,6 +36,45 @@ import Testing
3536
let a = A()
3637
#expect(a.b.c == C())
3738
}
39+
40+
@Test func lockingOrderWithDependencies() async {
41+
struct D: TestDependencyKey {
42+
@Shared(.inMemory("count")) var count = 0
43+
init() {
44+
Thread.sleep(forTimeInterval: 0.2)
45+
$count.withLock { $0 += 1 }
46+
}
47+
static var testValue: D { D() }
48+
}
49+
let a = Task {
50+
do {
51+
try await Task.sleep(nanoseconds: 100_000_000)
52+
@Dependency(D.self) var d
53+
#expect(d.count == 1)
54+
} catch {}
55+
}
56+
let b = Task {
57+
@Shared(.inMemory("count")) var count: Int = {
58+
Thread.sleep(forTimeInterval: 0.2)
59+
return 2
60+
}()
61+
#expect(count == 1)
62+
}
63+
let c = Task {
64+
do {
65+
try await Task.sleep(nanoseconds: 500_000_000)
66+
Issue.record("Deadlock detected")
67+
exit(1)
68+
} catch {}
69+
}
70+
await withTaskGroup(of: Void.self) { taskGroup in
71+
taskGroup.addTask {
72+
_ = await (a.value, b.value)
73+
c.cancel()
74+
}
75+
taskGroup.addTask { await c.value }
76+
}
77+
}
3878
}
3979

4080
@Suite struct BoxReference {

0 commit comments

Comments
 (0)