File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ import Dependencies
12import Foundation
23import IdentifiedCollections
34import 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 {
You can’t perform that action at this time.
0 commit comments