summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadRESTTests/RetryStrategyTests.swift
blob: f070a7ca6fe4f09690d661a6aedfdb9c833e01d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
//  RetryStrategyTests.swift
//  MullvadRESTTests
//
//  Created by Marco Nikic on 2024-06-07.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import XCTest

@testable import MullvadREST
@testable import MullvadTypes

class RetryStrategyTests: XCTestCase {
    func testJitteredBackoffDoesNotGoBeyondMaxDelay() throws {
        let maxDelay = REST.CodableDuration(seconds: 10, attoseconds: 0)
        let retryDelay = REST.RetryDelay.exponentialBackoff(initial: .seconds(1), multiplier: 2, maxDelay: maxDelay)
        let retry = REST.RetryStrategy(maxRetryCount: 0, delay: retryDelay, applyJitter: true)
        let iterator = retry.makeDelayIterator()
        var previousDelay = Duration(secondsComponent: 0, attosecondsComponent: 0)

        for _ in 0...10 {
            let currentDelay = try XCTUnwrap(iterator.next())
            XCTAssertLessThanOrEqual(previousDelay, currentDelay)
            XCTAssertLessThanOrEqual(currentDelay, maxDelay.duration)
            previousDelay = currentDelay
        }
    }

    func testJitteredConstantCannotBeMoreThanDouble() throws {
        let retryDelay = REST.RetryDelay.constant(.seconds(10))
        let retry = REST.RetryStrategy(maxRetryCount: 0, delay: retryDelay, applyJitter: true)
        let iterator = retry.makeDelayIterator()
        let minimumDelay = Duration(secondsComponent: 10, attosecondsComponent: 0)
        let maximumDelay = Duration(secondsComponent: 20, attosecondsComponent: 0)

        for _ in 0...10 {
            let currentDelay = try XCTUnwrap(iterator.next())
            let maximumJitterRange = minimumDelay...maximumDelay
            print(currentDelay)
            XCTAssertLessThanOrEqual(maximumJitterRange.lowerBound, currentDelay)
            XCTAssertGreaterThanOrEqual(maximumJitterRange.upperBound, currentDelay)
        }
    }

    func testCannotApplyJitterToNeverRetry() throws {
        let retryDelay = REST.RetryDelay.never
        let retry = REST.RetryStrategy(maxRetryCount: 0, delay: retryDelay, applyJitter: true)
        let iterator = retry.makeDelayIterator()
        XCTAssertNil(iterator.next())
    }
}