Skip to content

Commit 3def584

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

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

Tests/SWBLLBuildTests/LLBuildTests.swift

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,135 +19,151 @@ import struct SWBLLBuild.Diagnostic
1919
import Foundation
2020

2121
fileprivate class LoggingDelegate: BuildSystemDelegate {
22-
let queue = SWBQueue(label: "LLBuildTests.LoggingDelegate.queue", qos: UserDefaults.defaultRequestQoS)
2322
var buildSystem: BuildSystem? = nil
2423
let fs: (any SWBLLBuild.FileSystem)?
2524
let fsProxy: any FSProxy
26-
var log: [String] = []
27-
var errors: [String] = []
25+
private struct Output: Sendable {
26+
var log: [String] = []
27+
var errors: [String] = []
28+
}
29+
private let output = SWBMutex<Output>(.init())
2830
let ignoreStatusChanges: Bool
2931

32+
var log: [String] {
33+
output.withLock { $0.log }
34+
}
35+
36+
var errors: [String] {
37+
output.withLock { $0.errors }
38+
}
39+
3040
init(fs: any FSProxy, ignoreStatusChanges: Bool = false) {
3141
self.fs = SWBLLBuild.FileSystemImpl(fs)
3242
self.fsProxy = fs
3343
self.ignoreStatusChanges = ignoreStatusChanges
3444
}
3545

46+
func append(log: String) {
47+
output.withLock { output in
48+
output.log.append(log)
49+
}
50+
}
51+
3652
func lookupTool(_ name: String) -> (any Tool)? {
3753
return nil
3854
}
3955

4056
func hadCommandFailure() {
41-
queue.blocking_sync {
42-
self.log.append("had-command-failure")
57+
output.withLock { output in
58+
output.log.append("had-command-failure")
4359
}
4460
buildSystem?.cancel()
4561
}
4662

4763
func handleDiagnostic(_ diagnostic: Diagnostic) {
48-
queue.blocking_sync {
49-
self.log.append("\(diagnostic.kind): \(diagnostic.message)")
50-
self.errors.append("\(diagnostic.kind): \(diagnostic.message)")
64+
output.withLock { output in
65+
output.log.append("\(diagnostic.kind): \(diagnostic.message)")
66+
output.errors.append("\(diagnostic.kind): \(diagnostic.message)")
5167
}
5268
}
5369

5470
func commandStatusChanged(_ command: Command, kind: CommandStatusKind) {
5571
if !ignoreStatusChanges {
56-
queue.blocking_sync {
57-
self.log.append("command-status-changed: \(command.name), to: \(kind)")
72+
output.withLock { output in
73+
output.log.append("command-status-changed: \(command.name), to: \(kind)")
5874
}
5975
}
6076
}
6177
func commandPreparing(_ command: Command) {
62-
queue.blocking_sync {
63-
self.log.append("command-preparing: \(command.name)")
78+
output.withLock { output in
79+
output.log.append("command-preparing: \(command.name)")
6480
}
6581
}
6682
func commandStarted(_ command: Command) {
67-
queue.blocking_sync {
68-
self.log.append("command-started: \(command.name)")
83+
output.withLock { output in
84+
output.log.append("command-started: \(command.name)")
6985
}
7086
}
7187
func shouldCommandStart(_ command: Command) -> Bool {
72-
queue.blocking_sync {
73-
self.log.append("should-command-start: \(command.name)")
88+
output.withLock { output in
89+
output.log.append("should-command-start: \(command.name)")
7490
}
7591
return true
7692
}
7793
func commandFinished(_ command: Command, result: CommandResult) {
78-
queue.blocking_sync {
79-
self.log.append("command-finished: \(command.name)")
94+
output.withLock { output in
95+
output.log.append("command-finished: \(command.name)")
8096
}
8197
}
8298
func commandFoundDiscoveredDependency(_ command: Command, path: String, kind: DiscoveredDependencyKind) {
83-
queue.blocking_sync {
84-
self.log.append("command-found-discovered-dependency: \(path) \(kind)")
99+
output.withLock { output in
100+
output.log.append("command-found-discovered-dependency: \(path) \(kind)")
85101
}
86102
}
87103
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)")
104+
output.withLock { output in
105+
output.log.append("command-had-error: \(command.name): \(message)")
106+
output.errors.append("command-had-error: \(command.name): \(message)")
91107
}
92108
}
93109
func commandHadNote(_ command: Command, message: String) {
94-
queue.blocking_sync {
95-
self.log.append("command-had-note: \(command.name): \(message)")
110+
output.withLock { output in
111+
output.log.append("command-had-note: \(command.name): \(message)")
96112
}
97113
}
98114
func commandHadWarning(_ command: Command, message: String) {
99-
queue.blocking_sync {
100-
self.log.append("command-had-warning: \(command.name): \(message)")
115+
output.withLock { output in
116+
output.log.append("command-had-warning: \(command.name): \(message)")
101117
}
102118
}
103119
func commandProcessStarted(_ command: Command, process: ProcessHandle) {
104-
queue.blocking_sync {
105-
self.log.append("command-process-started: \(command.name) -- \(command.description)")
120+
output.withLock { output in
121+
output.log.append("command-process-started: \(command.name) -- \(command.description)")
106122
}
107123
}
108124
func commandProcessHadError(_ command: Command, process: ProcessHandle, message: String) {
109-
queue.blocking_sync {
110-
self.log.append("command-process-error: \(message)")
125+
output.withLock { output in
126+
output.log.append("command-process-error: \(message)")
111127
}
112128
}
113129
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)")
130+
output.withLock { output in
131+
output.log.append("command-process-output: \(command.name): \(ByteString(data).bytes.asReadableString().debugDescription)")
116132
}
117133
}
118134
func commandProcessFinished(_ command: Command, process: ProcessHandle, result: CommandExtendedResult) {
119-
queue.blocking_sync {
120-
self.log.append("command-process-finished: \(command.name)")
135+
output.withLock { output in
136+
output.log.append("command-process-finished: \(command.name)")
121137
}
122138
}
123139
func determinedRuleNeedsToRun(_ rule: BuildKey, reason: RuleRunReason, inputRule: BuildKey?) {
124-
queue.blocking_sync {
125-
self.log.append("determined-rule-needs-to-run: \(rule.description)")
140+
output.withLock { output in
141+
output.log.append("determined-rule-needs-to-run: \(rule.description)")
126142
}
127143
}
128144
func cycleDetected(rules: [BuildKey]) {
129-
queue.blocking_sync {
130-
self.log.append("cycle-detected: \(rules.map{ $0.key })")
145+
output.withLock { output in
146+
output.log.append("cycle-detected: \(rules.map{ $0.key })")
131147
}
132148
}
133149
func commandCannotBuildOutputDueToMissingInputs(_ command: Command, output: BuildKey, inputs: [BuildKey]) {
134-
queue.blocking_sync {
150+
self.output.withLock { outputBox in
135151
let msg = "commandCannotBuildOutputDueToMissingInputs: \(command.name) \(output.key) \(inputs.map { $0.key })"
136-
self.log.append(msg)
137-
self.errors.append(msg)
152+
outputBox.log.append(msg)
153+
outputBox.errors.append(msg)
138154
}
139155
}
140156
func cannotBuildNodeDueToMultipleProducers(output: BuildKey, commands: [Command]) {
141-
queue.blocking_sync {
157+
self.output.withLock { outputBox in
142158
let msg = "cannotBuildNodeDueToMultipleProducers: \(output.key) \(commands.map { $0.name })"
143-
self.log.append(msg)
144-
self.errors.append(msg)
159+
outputBox.log.append(msg)
160+
outputBox.errors.append(msg)
145161
}
146162
}
147163

148164
func shouldResolveCycle(rules: [BuildKey], candidate: BuildKey, action: CycleAction) -> Bool {
149-
queue.blocking_sync {
150-
self.log.append("should-resolve-cycle: \(rules.map{ $0.key })")
165+
output.withLock { output in
166+
output.log.append("should-resolve-cycle: \(rules.map{ $0.key })")
151167
}
152168
return false;
153169
}
@@ -521,7 +537,7 @@ fileprivate class LoggingDelegate: BuildSystemDelegate {
521537
}
522538

523539
func execute(_ command: Command, _ commandInterface: BuildSystemCommandInterface) -> Bool {
524-
delegate.log.append("write-command: execute")
540+
delegate.append(log: "write-command: execute")
525541
do {
526542
try delegate.fsProxy.write(Static.outputPath, contents: [])
527543
return true

0 commit comments

Comments
 (0)