Skip to content

Commit 4ff6e14

Browse files
Refactor MockURLProtocol to use @mainactor for task execution, improving thread safety and simplifying error handling.
1 parent a2a4123 commit 4ff6e14

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

Tests/KiteTests/Mocks/MockURLProtocol.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class MockURLProtocol: URLProtocol, @unchecked Sendable {
2222
}
2323

2424
override func startLoading() {
25-
Task {
25+
Task { @MainActor in
2626
guard let testID = self.request.value(forHTTPHeaderField: "X-Test-ID") else {
2727
throw Error.missedXTestIDHeader
2828
}
@@ -31,18 +31,12 @@ final class MockURLProtocol: URLProtocol, @unchecked Sendable {
3131
}
3232
do {
3333
let (data, response) = try handler(self.request)
34-
await MainActor.run { [weak self] in
35-
guard let self else { return }
36-
self.client?.urlProtocol(
37-
self, didReceive: response, cacheStoragePolicy: .notAllowed)
38-
self.client?.urlProtocol(self, didLoad: data)
39-
self.client?.urlProtocolDidFinishLoading(self)
40-
}
34+
self.client?.urlProtocol(
35+
self, didReceive: response, cacheStoragePolicy: .notAllowed)
36+
self.client?.urlProtocol(self, didLoad: data)
37+
self.client?.urlProtocolDidFinishLoading(self)
4138
} catch {
42-
await MainActor.run { [weak self] in
43-
guard let self else { return }
44-
self.client?.urlProtocol(self, didFailWithError: error)
45-
}
39+
self.client?.urlProtocol(self, didFailWithError: error)
4640
}
4741
}
4842
}

0 commit comments

Comments
 (0)