forked from apple/swift-async-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestValidator.swift
138 lines (129 loc) · 4.35 KB
/
TestValidator.swift
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import XCTest
import AsyncAlgorithms
final class TestValidator: XCTestCase {
func test_gate() async {
let gate = Gate()
let state = ManagedCriticalState(false)
let entered = expectation(description: "entered")
Task {
await gate.enter()
state.withCriticalRegion { $0 = true }
entered.fulfill()
}
XCTAssertFalse(state.withCriticalRegion { $0 })
gate.open()
await fulfillment(of: [entered], timeout: 1.0)
XCTAssertTrue(state.withCriticalRegion { $0 })
}
func test_gatedSequence() async {
var gated = GatedSequence([1, 2, 3])
let expectations = [
expectation(description: "item 1"),
expectation(description: "item 2"),
expectation(description: "item 3")
]
let started = expectation(description: "started")
let finished = expectation(description: "finished")
let state = ManagedCriticalState([Int]())
let seq = gated
Task {
var iterator = seq.makeAsyncIterator()
var index = 0
started.fulfill()
while let value = await iterator.next() {
state.withCriticalRegion {
$0.append(value)
}
expectations[index].fulfill()
index += 1
}
finished.fulfill()
}
await fulfillment(of: [started], timeout: 1.0)
XCTAssertEqual(state.withCriticalRegion { $0 }, [])
gated.advance()
await fulfillment(of: [expectations[0]], timeout: 1.0)
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
gated.advance()
await fulfillment(of: [expectations[1]], timeout: 1.0)
XCTAssertEqual(state.withCriticalRegion { $0 }, [1, 2])
gated.advance()
await fulfillment(of: [expectations[2]], timeout: 1.0)
XCTAssertEqual(state.withCriticalRegion { $0 }, [1, 2, 3])
await fulfillment(of: [finished], timeout: 1.0)
}
func test_gatedSequence_throwing() async {
var gated = GatedSequence([1, 2, 3])
let expectations = [
expectation(description: "item 1")
]
let started = expectation(description: "started")
let finished = expectation(description: "finished")
let state = ManagedCriticalState([Int]())
let failure = ManagedCriticalState<Error?>(nil)
let seq = gated.map { try throwOn(2, $0) }
Task {
var iterator = seq.makeAsyncIterator()
var index = 0
started.fulfill()
do {
while let value = try await iterator.next() {
state.withCriticalRegion {
$0.append(value)
}
expectations[index].fulfill()
index += 1
}
} catch {
failure.withCriticalRegion { $0 = error }
}
finished.fulfill()
}
await fulfillment(of: [started], timeout: 1.0)
XCTAssertEqual(state.withCriticalRegion { $0 }, [])
gated.advance()
await fulfillment(of: [expectations[0]], timeout: 1.0)
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
gated.advance()
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
await fulfillment(of: [finished], timeout: 1.0)
XCTAssertEqual(state.withCriticalRegion { $0 }, [1])
XCTAssertEqual(failure.withCriticalRegion { $0 as? Failure }, Failure())
}
func test_validator() async {
var a = GatedSequence([1, 2, 3])
let finished = expectation(description: "finished")
let sequence = a.map { $0 + 1 }
let validator = Validator<Int>()
validator.test(sequence) { iterator in
let pastEnd = await iterator.next()
XCTAssertNil(pastEnd)
finished.fulfill()
}
var value = await validator.validate()
XCTAssertEqual(value, [])
a.advance()
value = await validator.validate()
XCTAssertEqual(value, [2])
a.advance()
value = await validator.validate()
XCTAssertEqual(value, [2, 3])
a.advance()
value = await validator.validate()
XCTAssertEqual(value, [2, 3, 4])
a.advance()
await fulfillment(of: [finished], timeout: 1.0)
value = validator.current
XCTAssertEqual(value, [2, 3, 4])
}
}