Repository: Alamofire/Alamofire
File: Tests/RequestInterceptorTests.swift (Swift)

Code before:
    }

    func testThatInterceptorCanRetryRequestAsynchronously() {
        // Given
        let url = URL(string: "https://httpbin.org/get")!
        let session = Session(startRequestsImmediately: false)
        let request = session.request(url)

        let retrier = Retrier { _, _, _, completion in
            DispatchQueue.main.async {
                completion(.retry)
            }
        }
        let interceptor = Interceptor(retriers: [retrier])

        var result: RetryResult!

        let completesExpectation = expectation(description: "interceptor completes")

        // When
        interceptor.retry(request, for: session, dueTo: MockError()) {
            result = $0
            completesExpectation.fulfill()
        }

        _ = waitForExpectations(timeout: 0.01)

        // Then
        XCTAssertEqual(result, .retry)
    }

    func testThatInterceptorStopsIteratingThroughPendingRetriersWithRetryResult() {
        // Given
        let url = URL(string: "https://httpbin.org/get")!
        let session = Session(startRequestsImmediately: false)
        let request = session.request(url)

        var retrier2Called = false

        let retrier1 = Retrier { _, _, _, completion in completion(.retry) }
        let retrier2 = Retrier { _, _, _, completion in retrier2Called = true; completion(.doNotRetry) }
        let interceptor = Interceptor(retriers: [retrier1, retrier2])

        var result: RetryResult!

        // When
        interceptor.retry(request, for: session, dueTo: MockError()) { result = $0 }

        // Then
        XCTAssertEqual(result, .retry)
        XCTAssertFalse(retrier2Called)

Review comment (suggestion):
Please use our default timeout. It should be available as `timeout`. Again, there doesn't seem to be a need for the assignment here.

Code after:
    }

    func testThatInterceptorCanRetryRequestAsynchronously() {
        // Given
        let url = URL(string: "https://httpbin.org/get")!
        let session = Session(startRequestsImmediately: false)
        let request = session.request(url)

        let retrier = Retrier { _, _, _, completion in
            DispatchQueue.main.async {
                completion(.retry)
            }
        }
        let interceptor = Interceptor(retriers: [retrier])

        var result: RetryResult!

        let completesExpectation = expectation(description: "interceptor completes")

        // When
        interceptor.retry(request, for: session, dueTo: MockError()) {
            result = $0
            completesExpectation.fulfill()
        }

        waitForExpectations(timeout: timeout)

        // Then
        XCTAssertEqual(result, .retry)
    }

    func testThatInterceptorStopsIteratingThroughPendingRetriersWithRetryResult() {
        // Given
        let url = URL(string: "https://httpbin.org/get")!
        let session = Session(startRequestsImmediately: false)
        let request = session.request(url)

        var retrier2Called = false

        let retrier1 = Retrier { _, _, _, completion in completion(.retry) }
        let retrier2 = Retrier { _, _, _, completion in retrier2Called = true; completion(.doNotRetry) }
        let interceptor = Interceptor(retriers: [retrier1, retrier2])

        var result: RetryResult!

        // When
        interceptor.retry(request, for: session, dueTo: MockError()) { result = $0 }

        // Then
        XCTAssertEqual(result, .retry)
        XCTAssertFalse(retrier2Called)