Skip to content

Commit

Permalink
Fix an issue with mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Aug 3, 2024
1 parent a048a4e commit afa0129
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Demo/Sources/iOS/Pulse_Demo_iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ struct PulseDemo_iOS: App {

private final class AppViewModel: ObservableObject {
let log = OSLog(subsystem: "app", category: "AppViewModel")

init() {
// URLSessionProxyDelegate.enableAutomaticRegistration()
// URLSessionProxyDelegate.enableAutomaticRegistration(logger: NetworkLogger(store: .demo))
// DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) {
// sendRequest()
// }
Expand Down
13 changes: 8 additions & 5 deletions Sources/Pulse/NetworkLogger/URLSessionMockManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ final class URLSessionMockManager: @unchecked Sendable {
func getMock(for request: URLRequest) -> URLSessionMock? {
lock.lock()
defer { lock.unlock() }

return mocks.lazy.map(\.value).first {
$0.isMatch(request)
}
return _getMock(for: request)
}

func shouldMock(_ request: URLRequest) -> Bool {
lock.lock()
defer { lock.unlock() }

guard let mock = getMock(for: request) else {
guard let mock = _getMock(for: request) else {
return false
}
defer { numberOfHandledRequests[mock.mockID, default: 0] += 1 }
Expand All @@ -42,6 +39,12 @@ final class URLSessionMockManager: @unchecked Sendable {
return RemoteLogger.shared.connectionState == .connected
}

private func _getMock(for request: URLRequest) -> URLSessionMock? {
mocks.lazy.map(\.value).first {
$0.isMatch(request)
}
}

func update(_ mocks: [URLSessionMock]) {
lock.lock()
defer { lock.unlock() }
Expand Down

0 comments on commit afa0129

Please sign in to comment.