From 19fc7cfe13de72efc95ee8d58852b965fe57eb1a Mon Sep 17 00:00:00 2001 From: Calvin Cestari Date: Wed, 30 Oct 2024 13:52:04 -0700 Subject: [PATCH] ci: Fix `URLSessionClientTest` tests (apollographql/apollo-ios-dev#524) --- .../MockURLProtocol.swift | 8 ++--- .../Network/URLSessionClientTests.swift | 30 +++++++------------ 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/Tests/ApolloInternalTestHelpers/MockURLProtocol.swift b/Tests/ApolloInternalTestHelpers/MockURLProtocol.swift index 9cca0fe7a..dcf4ae049 100644 --- a/Tests/ApolloInternalTestHelpers/MockURLProtocol.swift +++ b/Tests/ApolloInternalTestHelpers/MockURLProtocol.swift @@ -11,10 +11,10 @@ public class MockURLProtocol: URLProtocol } override public func startLoading() { - guard let url = self.request.url, - let handler = RequestProvider.requestHandlers[url] else { - fatalError("No MockRequestHandler available for URL.") - } + guard + let url = self.request.url, + let handler = RequestProvider.requestHandlers[url] + else { return } DispatchQueue.main.asyncAfter(deadline: .now() + Double.random(in: 0.0...0.25)) { defer { diff --git a/Tests/ApolloTests/Network/URLSessionClientTests.swift b/Tests/ApolloTests/Network/URLSessionClientTests.swift index bb91936bc..fccdbc41f 100644 --- a/Tests/ApolloTests/Network/URLSessionClientTests.swift +++ b/Tests/ApolloTests/Network/URLSessionClientTests.swift @@ -233,7 +233,13 @@ class URLSessionClientTests: XCTestCase { self.client.cancel(task: task) - self.wait(for: [expectation], timeout: 5) + // Instead of waiting an arbitrary amount of time for the completion to maybe be called + // the following mimics what Apple's documentation for URLSessionTask.cancel() states + // happens when a task is cancelled, i.e.: manually calling the delegate method + // urlSession(_:task:didCompleteWithError:) + self.client.urlSession(self.client.session, task: task, didCompleteWithError: NSError(domain: NSURLErrorDomain, code: NSURLErrorCancelled)) + + self.wait(for: [expectation], timeout: 0.5) } @@ -349,33 +355,19 @@ class URLSessionClientTests: XCTestCase { responseData: nil, statusCode: -1 ) - - let expectation = self.expectation(description: "Described task completed") - expectation.isInverted = true - let task = self.client.sendRequest(request) { result in - // This shouldn't get hit since we cancel the task immediately - expectation.fulfill() - } + let task = self.client.sendRequest(request) { result in } self.client.cancel(task: task) - + // Should be nil by default. XCTAssertNil(task.taskDescription) let expected = "test task description" - let describedTask = self.client.sendRequest( - request, - taskDescription: expected - ) { result in - // This shouldn't get hit since we cancel the task immediately - expectation.fulfill() - } + let describedTask = self.client.sendRequest(request, taskDescription: expected) { result in } self.client.cancel(task: describedTask) - + // The returned task should have the provided taskDescription. XCTAssertEqual(expected, describedTask.taskDescription) - - self.wait(for: [expectation], timeout: 5) } // MARK: Multipart Tests