-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AsyncReducerTests.swift
165 lines (134 loc) · 5.48 KB
/
AsyncReducerTests.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import XCTest
@testable import Shared
private let timeoutPeriod: TimeInterval = 600
class AsyncReducerTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSimpleBehaviour() {
let expectation = self.expectation(description: #function)
happyCase(expectation, combine: simpleAdder)
}
func testWaitingFillerBehaviour() {
let expectation = self.expectation(description: #function)
happyCase(expectation, combine: waitingFillingAdder)
}
func testWaitingFillerAppendingBehaviour() {
let expectation = self.expectation(description: #function)
appendingCase(expectation, combine: waitingFillingAdder)
}
func testFailingCombine() {
let expectation = self.expectation(description: #function)
let combine = { (a: Int, b: Int) -> Deferred<Maybe<Int>> in
if a >= 6 {
return deferMaybe(TestError())
}
return deferMaybe(a + b)
}
let reducer = AsyncReducer(initialValue: 0, combine: combine)
reducer.terminal.upon { res in
XCTAssert(res.isFailure)
expectation.fulfill()
}
self.append(reducer, items: 1, 2, 3, 4, 5)
waitForExpectations(timeout: timeoutPeriod, handler: nil)
}
func testFailingAppend() {
let expectation = self.expectation(description: #function)
let reducer = AsyncReducer(initialValue: 0, combine: simpleAdder)
reducer.terminal.upon { res in
XCTAssert(res.isSuccess)
XCTAssertEqual(res.successValue!, 15)
}
self.append(reducer, items: 1, 2, 3, 4, 5)
delay(0.1) {
do {
_ = try reducer.append(6, 7, 8)
XCTFail("Can't append to a reducer that's already finished")
} catch let error {
XCTAssert(true, "Properly received error on finished reducer \(error)")
}
expectation.fulfill()
}
waitForExpectations(timeout: timeoutPeriod, handler: nil)
}
func testAccumulation() {
var addDuring: [String] = ["bar", "baz"]
var reducer: AsyncReducer<[String: Bool], String>!
func combine(_ t: [String: Bool], u: String) -> Deferred<Maybe<[String: Bool]>> {
var out = t
out[u] = true
// Pretend that some new work arrived while we were handling this.
if let nextUp = addDuring.popLast() {
_ = try! reducer.append(nextUp)
}
return deferMaybe(out)
}
// Start with 'foo'.
reducer = AsyncReducer(initialValue: deferMaybe([:]), combine: combine)
_ = try! reducer.append("foo")
// Wait for the result. We should have handled all three by the time this returns.
let result = reducer.terminal.value
XCTAssertTrue(result.isSuccess)
XCTAssertEqual(["foo": true, "bar": true, "baz": true], result.successValue!)
}
}
extension AsyncReducerTests {
func happyCase(_ expectation: XCTestExpectation, combine: @escaping (Int, Int) -> Deferred<Maybe<Int>>) {
let reducer = AsyncReducer(initialValue: 0, combine: combine)
reducer.terminal.upon { res in
XCTAssert(res.isSuccess)
XCTAssertEqual(res.successValue!, 15)
expectation.fulfill()
}
self.append(reducer, items: 1, 2, 3, 4, 5)
waitForExpectations(timeout: timeoutPeriod, handler: nil)
}
func appendingCase(_ expectation: XCTestExpectation, combine: @escaping (Int, Int) -> Deferred<Maybe<Int>>) {
let reducer = AsyncReducer(initialValue: 0, combine: combine)
reducer.terminal.upon { res in
XCTAssert(res.isSuccess)
XCTAssertEqual(res.successValue!, 15)
expectation.fulfill()
}
self.append(reducer, items: 1, 2)
delay(0.1) {
self.append(reducer, items: 3, 4, 5)
}
waitForExpectations(timeout: timeoutPeriod, handler: nil)
}
func append(_ reducer: AsyncReducer<Int, Int>, items: Int...) {
do {
_ = try reducer.append(items)
} catch let error {
XCTFail("Append failed with \(error)")
}
}
}
class TestError: MaybeErrorType {
var description = "Error"
}
private let serialQueue = DispatchQueue(label: "com.mozilla.test.serial", attributes: [])
private let concurrentQueue = DispatchQueue(label: "com.mozilla.test.concurrent", attributes: DispatchQueue.Attributes.concurrent)
func delay(_ delay: Double, closure: @escaping () -> Void) {
concurrentQueue.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
}
private func simpleAdder(_ a: Int, b: Int) -> Deferred<Maybe<Int>> {
return deferMaybe(a + b)
}
private func waitingFillingAdder(_ a: Int, b: Int) -> Deferred<Maybe<Int>> {
let deferred = Deferred<Maybe<Int>>()
delay(0.1) {
deferred.fill(Maybe(success: a + b))
}
return deferred
}