Skip to content

Commit e6ed315

Browse files
committed
Remove blocking_sync usages from SWBLLBuildTests
These just need simple synchronization; use a mutex instead.
1 parent f0af94b commit e6ed315

File tree

1 file changed

+65
-48
lines changed

1 file changed

+65
-48
lines changed

Tests/SWBLLBuildTests/LLBuildTests.swift

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,137 +17,154 @@ import SWBUtil
1717

1818
import struct SWBLLBuild.Diagnostic
1919
import Foundation
20+
import Synchronization
2021

2122
fileprivate class LoggingDelegate: BuildSystemDelegate {
22-
let queue = SWBQueue(label: "LLBuildTests.LoggingDelegate.queue", qos: UserDefaults.defaultRequestQoS)
2323
var buildSystem: BuildSystem? = nil
2424
let fs: (any SWBLLBuild.FileSystem)?
2525
let fsProxy: any FSProxy
26-
var log: [String] = []
27-
var errors: [String] = []
26+
private struct Output: Sendable {
27+
var log: [String] = []
28+
var errors: [String] = []
29+
}
30+
private let output = SWBMutex<Output>(.init())
2831
let ignoreStatusChanges: Bool
2932

33+
var log: [String] {
34+
output.withLock { $0.log }
35+
}
36+
37+
var errors: [String] {
38+
output.withLock { $0.errors }
39+
}
40+
3041
init(fs: any FSProxy, ignoreStatusChanges: Bool = false) {
3142
self.fs = SWBLLBuild.FileSystemImpl(fs)
3243
self.fsProxy = fs
3344
self.ignoreStatusChanges = ignoreStatusChanges
3445
}
3546

47+
func append(log: String) {
48+
output.withLock { output in
49+
output.log.append(log)
50+
}
51+
}
52+
3653
func lookupTool(_ name: String) -> (any Tool)? {
3754
return nil
3855
}
3956

4057
func hadCommandFailure() {
41-
queue.blocking_sync {
42-
self.log.append("had-command-failure")
58+
output.withLock { output in
59+
output.log.append("had-command-failure")
4360
}
4461
buildSystem?.cancel()
4562
}
4663

4764
func handleDiagnostic(_ diagnostic: Diagnostic) {
48-
queue.blocking_sync {
49-
self.log.append("\(diagnostic.kind): \(diagnostic.message)")
50-
self.errors.append("\(diagnostic.kind): \(diagnostic.message)")
65+
output.withLock { output in
66+
output.log.append("\(diagnostic.kind): \(diagnostic.message)")
67+
output.errors.append("\(diagnostic.kind): \(diagnostic.message)")
5168
}
5269
}
5370

5471
func commandStatusChanged(_ command: Command, kind: CommandStatusKind) {
5572
if !ignoreStatusChanges {
56-
queue.blocking_sync {
57-
self.log.append("command-status-changed: \(command.name), to: \(kind)")
73+
output.withLock { output in
74+
output.log.append("command-status-changed: \(command.name), to: \(kind)")
5875
}
5976
}
6077
}
6178
func commandPreparing(_ command: Command) {
62-
queue.blocking_sync {
63-
self.log.append("command-preparing: \(command.name)")
79+
output.withLock { output in
80+
output.log.append("command-preparing: \(command.name)")
6481
}
6582
}
6683
func commandStarted(_ command: Command) {
67-
queue.blocking_sync {
68-
self.log.append("command-started: \(command.name)")
84+
output.withLock { output in
85+
output.log.append("command-started: \(command.name)")
6986
}
7087
}
7188
func shouldCommandStart(_ command: Command) -> Bool {
72-
queue.blocking_sync {
73-
self.log.append("should-command-start: \(command.name)")
89+
output.withLock { output in
90+
output.log.append("should-command-start: \(command.name)")
7491
}
7592
return true
7693
}
7794
func commandFinished(_ command: Command, result: CommandResult) {
78-
queue.blocking_sync {
79-
self.log.append("command-finished: \(command.name)")
95+
output.withLock { output in
96+
output.log.append("command-finished: \(command.name)")
8097
}
8198
}
8299
func commandFoundDiscoveredDependency(_ command: Command, path: String, kind: DiscoveredDependencyKind) {
83-
queue.blocking_sync {
84-
self.log.append("command-found-discovered-dependency: \(path) \(kind)")
100+
output.withLock { output in
101+
output.log.append("command-found-discovered-dependency: \(path) \(kind)")
85102
}
86103
}
87104
func commandHadError(_ command: Command, message: String) {
88-
queue.blocking_sync {
89-
self.log.append("command-had-error: \(command.name): \(message)")
90-
self.errors.append("command-had-error: \(command.name): \(message)")
105+
output.withLock { output in
106+
output.log.append("command-had-error: \(command.name): \(message)")
107+
output.errors.append("command-had-error: \(command.name): \(message)")
91108
}
92109
}
93110
func commandHadNote(_ command: Command, message: String) {
94-
queue.blocking_sync {
95-
self.log.append("command-had-note: \(command.name): \(message)")
111+
output.withLock { output in
112+
output.log.append("command-had-note: \(command.name): \(message)")
96113
}
97114
}
98115
func commandHadWarning(_ command: Command, message: String) {
99-
queue.blocking_sync {
100-
self.log.append("command-had-warning: \(command.name): \(message)")
116+
output.withLock { output in
117+
output.log.append("command-had-warning: \(command.name): \(message)")
101118
}
102119
}
103120
func commandProcessStarted(_ command: Command, process: ProcessHandle) {
104-
queue.blocking_sync {
105-
self.log.append("command-process-started: \(command.name) -- \(command.description)")
121+
output.withLock { output in
122+
output.log.append("command-process-started: \(command.name) -- \(command.description)")
106123
}
107124
}
108125
func commandProcessHadError(_ command: Command, process: ProcessHandle, message: String) {
109-
queue.blocking_sync {
110-
self.log.append("command-process-error: \(message)")
126+
output.withLock { output in
127+
output.log.append("command-process-error: \(message)")
111128
}
112129
}
113130
func commandProcessHadOutput(_ command: Command, process: ProcessHandle, data: [UInt8]) {
114-
queue.blocking_sync {
115-
self.log.append("command-process-output: \(command.name): \(ByteString(data).bytes.asReadableString().debugDescription)")
131+
output.withLock { output in
132+
output.log.append("command-process-output: \(command.name): \(ByteString(data).bytes.asReadableString().debugDescription)")
116133
}
117134
}
118135
func commandProcessFinished(_ command: Command, process: ProcessHandle, result: CommandExtendedResult) {
119-
queue.blocking_sync {
120-
self.log.append("command-process-finished: \(command.name)")
136+
output.withLock { output in
137+
output.log.append("command-process-finished: \(command.name)")
121138
}
122139
}
123140
func determinedRuleNeedsToRun(_ rule: BuildKey, reason: RuleRunReason, inputRule: BuildKey?) {
124-
queue.blocking_sync {
125-
self.log.append("determined-rule-needs-to-run: \(rule.description)")
141+
output.withLock { output in
142+
output.log.append("determined-rule-needs-to-run: \(rule.description)")
126143
}
127144
}
128145
func cycleDetected(rules: [BuildKey]) {
129-
queue.blocking_sync {
130-
self.log.append("cycle-detected: \(rules.map{ $0.key })")
146+
output.withLock { output in
147+
output.log.append("cycle-detected: \(rules.map{ $0.key })")
131148
}
132149
}
133150
func commandCannotBuildOutputDueToMissingInputs(_ command: Command, output: BuildKey, inputs: [BuildKey]) {
134-
queue.blocking_sync {
151+
self.output.withLock { outputBox in
135152
let msg = "commandCannotBuildOutputDueToMissingInputs: \(command.name) \(output.key) \(inputs.map { $0.key })"
136-
self.log.append(msg)
137-
self.errors.append(msg)
153+
outputBox.log.append(msg)
154+
outputBox.errors.append(msg)
138155
}
139156
}
140157
func cannotBuildNodeDueToMultipleProducers(output: BuildKey, commands: [Command]) {
141-
queue.blocking_sync {
158+
self.output.withLock { outputBox in
142159
let msg = "cannotBuildNodeDueToMultipleProducers: \(output.key) \(commands.map { $0.name })"
143-
self.log.append(msg)
144-
self.errors.append(msg)
160+
outputBox.log.append(msg)
161+
outputBox.errors.append(msg)
145162
}
146163
}
147164

148165
func shouldResolveCycle(rules: [BuildKey], candidate: BuildKey, action: CycleAction) -> Bool {
149-
queue.blocking_sync {
150-
self.log.append("should-resolve-cycle: \(rules.map{ $0.key })")
166+
output.withLock { output in
167+
output.log.append("should-resolve-cycle: \(rules.map{ $0.key })")
151168
}
152169
return false;
153170
}
@@ -521,7 +538,7 @@ fileprivate class LoggingDelegate: BuildSystemDelegate {
521538
}
522539

523540
func execute(_ command: Command, _ commandInterface: BuildSystemCommandInterface) -> Bool {
524-
delegate.log.append("write-command: execute")
541+
delegate.append(log: "write-command: execute")
525542
do {
526543
try delegate.fsProxy.write(Static.outputPath, contents: [])
527544
return true

0 commit comments

Comments
 (0)