summaryrefslogtreecommitdiffhomepage
path: root/ios/OperationsTests/AsyncBlockOperationTests.swift
blob: 00a7796f1c46cc17c3cb3ee349f5ca7e1e7eea9a (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
//  AsyncBlockOperationTests.swift
//  OperationsTests
//
//  Created by pronebird on 26/04/2023.
//  Copyright © 2023 Mullvad VPN AB. All rights reserved.
//

@testable import MullvadMockData
import MullvadTypes
import Operations
import XCTest

final class AsyncBlockOperationTests: XCTestCase {
    let operationQueue = AsyncOperationQueue()

    func testBlockOperation() async {
        let executionExpectation = expectation(description: "Should execute")
        let finishExpectation = expectation(description: "Should finish")

        let operation = AsyncBlockOperation(block: { finish in
            executionExpectation.fulfill()
            finish(nil)
        })

        operation.completionBlock = {
            finishExpectation.fulfill()
        }

        operationQueue.addOperation(operation)

        await fulfillment(of: [executionExpectation, finishExpectation], timeout: .UnitTest.timeout)
    }

    func testSynchronousBlockOperation() async {
        let executionExpectation = expectation(description: "Should execute")
        let finishExpectation = expectation(description: "Should finish")

        let operation = AsyncBlockOperation {
            executionExpectation.fulfill()
        }

        operation.completionBlock = {
            finishExpectation.fulfill()
        }

        operationQueue.addOperation(operation)

        await fulfillment(of: [executionExpectation, finishExpectation], timeout: .UnitTest.timeout)
    }

    func testCancellableTaskBlockOperation() async {
        let executionExpectation = expectation(description: "Should execute")
        let cancelExpectation = expectation(description: "Should cancel")
        let finishExpectation = expectation(description: "Should finish")

        let operation = AsyncBlockOperation { finish -> Cancellable in
            executionExpectation.fulfill()

            return AnyCancellable {
                cancelExpectation.fulfill()
                finish(nil)
            }
        }

        operation.completionBlock = {
            finishExpectation.fulfill()
        }

        operation.onStart { op in
            op.cancel()
        }

        operationQueue.addOperation(operation)

        await fulfillment(of: [executionExpectation, cancelExpectation, finishExpectation], timeout: .UnitTest.timeout)
    }

    func testCancellationShouldNotFireBeforeOperationIsEnqueued() async throws {
        let expect = expectation(description: "Cancellation should not fire.")
        expect.isInverted = true

        let operation = AsyncBlockOperation {}
        operation.onCancel { _ in expect.fulfill() }
        operation.cancel()

        await fulfillment(of: [expect], timeout: .UnitTest.invertedTimeout)
    }

    func testCancellationShouldFireAfterCancelledOperationIsEnqueued() async throws {
        let expect = expectation(description: "Cancellation should fire.")

        let operation = AsyncBlockOperation {}
        operation.onCancel { _ in expect.fulfill() }
        operation.cancel()
        operationQueue.addOperation(operation)

        await fulfillment(of: [expect], timeout: .UnitTest.timeout)
    }
}