Full Concurrency-safety#6
Conversation
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
There was a problem hiding this comment.
Pull Request Overview
This PR implements full concurrency safety for the HPNetwork library, particularly focusing on the HPNetworkMock component. The changes modernize the codebase to use Swift 6's concurrency features and ensure thread safety across all networking operations.
- Refactors
NetworkClientMockto use an internal actor for thread-safe state management - Updates test files to use async/await patterns and removes callback-based approaches
- Adds
Sendableconformance to protocols and types throughout the networking stack
Reviewed Changes
Copilot reviewed 13 out of 14 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| Tests/HPNetworkTests/NetworkClientMockTests.swift | Converts from lazy property to factory method, updates all tests to use async/await |
| Tests/HPNetworkTests/DownloadRequestTests.swift | Simplifies test to use async/await and updates mock registration |
| Tests/HPNetworkTests/DataRequestTests.swift | Similar async/await conversion and mock registration update |
| Sources/HPNetworkMock/URLSessionMock.swift | Restructures with MockedRequestStore actor and adds availability constraints |
| Sources/HPNetworkMock/NetworkClientMock.swift | Major refactor using internal actor for state management and async APIs |
| Sources/HPNetwork/Responses/NetworkResponse.swift | Removes initializer and adds Sendable conformance |
| Sources/HPNetwork/Requests/NetworkRequest.swift | Updates protocol with Sendable conformance and simplifies API |
| Sources/HPNetwork/Requests/DownloadRequest.swift | Updates to use new async task-based API |
| Sources/HPNetwork/Requests/DataRequest.swift | Similar task-based API updates |
| Sources/HPNetwork/NetworkClient.swift | Simplifies protocol and implementation for new async patterns |
| Sources/HPNetwork/ConnectionMonitor.swift | Adds @unchecked Sendable and main actor state updates |
| Sources/HPNetwork/Authorization/Authorization.swift | Adds Sendable conformance |
| Package.swift | Updates Swift tools version to 6.1 |
| @@ -21,19 +21,6 @@ public struct NetworkResponse<Output> { | |||
| /// The time that elapsed during the processing of the network request's result. | |||
| public let processingDuration: TimeInterval | |||
|
|
|||
There was a problem hiding this comment.
The public initializer for NetworkResponse has been removed, but this breaks the ability to create NetworkResponse instances. The initializer should be restored or an alternative creation method should be provided.
| /// Public initializer for creating a `NetworkResponse` instance. | |
| public init( | |
| output: Output, | |
| url: URL, | |
| response: HTTPResponse, | |
| networkingDuration: TimeInterval, | |
| processingDuration: TimeInterval | |
| ) { | |
| self.output = output | |
| self.url = url | |
| self.response = response | |
| self.networkingDuration = networkingDuration | |
| self.processingDuration = processingDuration | |
| } |
There was a problem hiding this comment.
No, NetworkResponse should not be constructible manually by the API consumer
| import Network | ||
|
|
||
| public final class ConnectionMonitor: ObservableObject { | ||
| public final class ConnectionMonitor: ObservableObject, @unchecked Sendable { |
There was a problem hiding this comment.
Using @unchecked Sendable requires careful justification. The class has mutable state (currentPath) that's accessed from multiple threads. Consider using proper synchronization or making this a proper Sendable type with actor isolation.
| public final class ConnectionMonitor: ObservableObject, @unchecked Sendable { | |
| public actor ConnectionMonitor: ObservableObject { |
| do { | ||
| _ = try await networkClient.schedule(request).value | ||
| XCTFail("Request should not succeed") | ||
| } catch { |
There was a problem hiding this comment.
This XCTFail will never be reached because the previous line try await networkClient.schedule(request).value will throw an exception if the request fails. The success case should be handled differently or the test logic should be restructured.
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #6 +/- ##
===========================================
+ Coverage 80.28% 90.79% +10.51%
===========================================
Files 13 14 +1
Lines 284 315 +31
===========================================
+ Hits 228 286 +58
+ Misses 56 29 -27 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
This PR is bringing full concurrency-safety, especially to
HPNetworkMock. It breaks the API though, so it'll be a new major version