Skip to content

Commit 0b9c0d8

Browse files
authored
[Collections] Fix flakey tests (#3409)
Motivation: Some of the tests generate random HTTP status code to simulate bad collection URLs, but recent code changes distinguish status `404` from others and return a `notFound` error instead of `unavailable`. This causes tests to fail randomly. Modification: Change range of random generated status code to start from 500 such that `404` is excluded.
1 parent d9a4b8c commit 0b9c0d8

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

Tests/PackageCollectionsTests/JSONPackageCollectionProviderTests.swift

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,10 @@ class JSONPackageCollectionProviderTests: XCTestCase {
233233
})
234234
}
235235

236-
func testUnsuccessfulHead() throws {
236+
func testUnsuccessfulHead_unavailable() throws {
237237
let url = URL(string: "https://www.test.com/collection.json")!
238238
let source = PackageCollectionsModel.CollectionSource(type: .json, url: url)
239-
let statusCode = Int.random(in: 201 ... 550)
239+
let statusCode = Int.random(in: 500 ... 550) // Don't use 404 because it leads to a different error message
240240

241241
let handler: HTTPClient.Handler = { request, _, completion in
242242
XCTAssertEqual(request.url, url, "url should match")
@@ -253,10 +253,10 @@ class JSONPackageCollectionProviderTests: XCTestCase {
253253
})
254254
}
255255

256-
func testUnsuccessfulGet() throws {
256+
func testUnsuccessfulGet_unavailable() throws {
257257
let url = URL(string: "https://www.test.com/collection.json")!
258258
let source = PackageCollectionsModel.CollectionSource(type: .json, url: url)
259-
let statusCode = Int.random(in: 201 ... 550)
259+
let statusCode = Int.random(in: 500 ... 550) // Don't use 404 because it leads to a different error message
260260

261261
let handler: HTTPClient.Handler = { request, _, completion in
262262
XCTAssertEqual(request.url, url, "url should match")
@@ -279,6 +279,50 @@ class JSONPackageCollectionProviderTests: XCTestCase {
279279
})
280280
}
281281

282+
func testUnsuccessfulHead_notFound() throws {
283+
let url = URL(string: "https://www.test.com/collection.json")!
284+
let source = PackageCollectionsModel.CollectionSource(type: .json, url: url)
285+
286+
let handler: HTTPClient.Handler = { request, _, completion in
287+
XCTAssertEqual(request.url, url, "url should match")
288+
XCTAssertEqual(request.method, .head, "method should match")
289+
completion(.success(.init(statusCode: 404)))
290+
}
291+
292+
var httpClient = HTTPClient(handler: handler)
293+
httpClient.configuration.circuitBreakerStrategy = .none
294+
httpClient.configuration.retryStrategy = .none
295+
let provider = JSONPackageCollectionProvider(httpClient: httpClient, diagnosticsEngine: DiagnosticsEngine())
296+
XCTAssertThrowsError(try tsc_await { callback in provider.get(source, callback: callback) }, "expected error", { error in
297+
XCTAssertEqual(error as? JSONPackageCollectionProvider.Errors, .collectionNotFound(url))
298+
})
299+
}
300+
301+
func testUnsuccessfulGet_notFound() throws {
302+
let url = URL(string: "https://www.test.com/collection.json")!
303+
let source = PackageCollectionsModel.CollectionSource(type: .json, url: url)
304+
305+
let handler: HTTPClient.Handler = { request, _, completion in
306+
XCTAssertEqual(request.url, url, "url should match")
307+
switch request.method {
308+
case .head:
309+
completion(.success(.init(statusCode: 200, headers: .init([.init(name: "Content-Length", value: "1")]))))
310+
case .get:
311+
completion(.success(.init(statusCode: 404)))
312+
default:
313+
XCTFail("method should match")
314+
}
315+
}
316+
317+
var httpClient = HTTPClient(handler: handler)
318+
httpClient.configuration.circuitBreakerStrategy = .none
319+
httpClient.configuration.retryStrategy = .none
320+
let provider = JSONPackageCollectionProvider(httpClient: httpClient, diagnosticsEngine: DiagnosticsEngine())
321+
XCTAssertThrowsError(try tsc_await { callback in provider.get(source, callback: callback) }, "expected error", { error in
322+
XCTAssertEqual(error as? JSONPackageCollectionProvider.Errors, .collectionNotFound(url))
323+
})
324+
}
325+
282326
func testBadJSON() throws {
283327
let url = URL(string: "https://www.test.com/collection.json")!
284328
let data = "blah".data(using: .utf8)!

0 commit comments

Comments
 (0)