summaryrefslogtreecommitdiffhomepage
path: root/ios/PacketTunnelCoreTests/TaskSleepTests.swift
blob: 58768021f38c9a770213136c418a7c0ec38f3ca1 (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
54
55
//
//  TaskSleepTests.swift
//  PacketTunnelCoreTests
//
//  Created by pronebird on 25/09/2023.
//  Copyright © 2023 Mullvad VPN AB. All rights reserved.
//

@testable import PacketTunnelCore
import XCTest

final class TaskSleepTests: XCTestCase {
    func testCancellation() async throws {
        let task = Task {
            try await Task.sleepUsingContinuousClock(for: .seconds(1))
        }

        task.cancel()

        do {
            try await task.value

            XCTFail("Task must be cancelled.")
        } catch {
            XCTAssert(error is CancellationError)
        }
    }

    /// This test triggers a race condition in `sleepUsingContinuousClock` where an `AutoCancellingTask` will
    /// cancel a `DispatchSourceTimer` in a `Task` trying to call `resume` on its continuation handler more than once
    func testSuccessfulEventHandlerRemovesCancellation() async throws {
        for _ in 0 ... 20 {
            let task = recoveryTask()
            try await Task.sleep(duration: .milliseconds(10))
            task.callDummyFunctionToForceConcurrencyWait()
        }
    }

    private func recoveryTask() -> AutoCancellingTask {
        AutoCancellingTask(Task.detached {
            while Task.isCancelled == false {
                try await Task.sleepUsingContinuousClock(for: .milliseconds(10))
            }
        })
    }
}

private extension AutoCancellingTask {
    /// This function is here to silence a warning about unused variables in `testSuccessfulEventHandlerRemovesCancellation`
    /// The following construct `_ = recoveryTask()` cannot be used as the resulting `AutoCancellingTask`
    /// would immediately get `deinit`ed, changing the test scenario.
    /// A dummy function is needed to make sure the task is not cancelled before concurrency is forced
    /// by having a call to `Task.sleep`
    func callDummyFunctionToForceConcurrencyWait() {}
}