Skip to content

Commit

Permalink
ci: Fix URLSessionClientTest tests (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored and gh-action-runner committed Oct 30, 2024
1 parent 235de07 commit 19fc7cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Tests/ApolloInternalTestHelpers/MockURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class MockURLProtocol<RequestProvider: MockRequestProvider>: 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 {
Expand Down
30 changes: 11 additions & 19 deletions Tests/ApolloTests/Network/URLSessionClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 19fc7cf

Please sign in to comment.