-
-
Notifications
You must be signed in to change notification settings - Fork 603
/
Copy pathAsyncAwaitTest+Require.swift
289 lines (242 loc) · 10.9 KB
/
AsyncAwaitTest+Require.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#if !os(WASI)
import XCTest
import Nimble
#if SWIFT_PACKAGE
import NimbleSharedTestHelpers
#endif
final class AsyncAwaitRequireTest: XCTestCase { // swiftlint:disable:this type_body_length
func testToPositiveMatches() async throws {
func someAsyncFunction() async throws -> Int {
try await Task.sleep(nanoseconds: 1_000_000) // 1 millisecond
return 1
}
try await require { try await someAsyncFunction() }.to(equal(1))
}
class Error: Swift.Error {}
let errorToThrow = Error()
private func doThrowError() throws -> Int {
throw errorToThrow
}
func testToEventuallyPositiveMatches() async throws {
var value = 0
deferToMainQueue { value = 1 }
try await require { value }.toEventually(equal(1))
deferToMainQueue { value = 0 }
try await require { value }.toEventuallyNot(equal(1))
}
func testToEventuallyNegativeMatches() async {
let value = 0
await failsWithErrorMessage("expected to eventually not equal <0>, got <0>") {
try await require { value }.toEventuallyNot(equal(0))
}
await failsWithErrorMessage("expected to eventually equal <1>, got <0>") {
try await require { value }.toEventually(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await require { try self.doThrowError() }.toEventually(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await require { try self.doThrowError() }.toEventuallyNot(equal(0))
}
}
func testPollUnwrapPositiveCase() async {
func someAsyncFunction() async throws -> Int {
try await Task.sleep(nanoseconds: 1_000_000) // 1 millisecond
return 1
}
await expect {
try await pollUnwrapa(await someAsyncFunction())
}.to(equal(1))
}
func testPollUnwrapNegativeCase() async {
await failsWithErrorMessage("expected to eventually not be nil, got <nil>") {
try await pollUnwrap { nil as Int? }
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await pollUnwrap { try self.doThrowError() as Int? }
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await pollUnwrap { try self.doThrowError() as Int? }
}
}
func testToEventuallyWithAsyncExpressions() async throws {
actor ExampleActor {
private var count = 0
func value() -> Int {
count += 1
return count
}
}
let subject = ExampleActor()
try await require { await subject.value() }.toEventually(equal(2))
}
func testToEventuallySyncCase() async throws {
try await require(1).toEventually(equal(1), timeout: .seconds(300))
}
func testToEventuallyWaitingOnMainTask() async throws {
class EncapsulatedValue {
static var executed = false
static func execute() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
Self.executed = true
}
}
}
EncapsulatedValue.execute()
try await require(EncapsulatedValue.executed).toEventually(beTrue())
}
@MainActor
func testToEventuallyOnMain() async throws {
try await require(1).toEventually(equal(1), timeout: .seconds(300))
try await require { try? await Task.sleep(nanoseconds: 10_000); return 1 }.toEventually(equal(1))
}
@MainActor
func testToEventuallyMatcherIsAlwaysExecutedOnMainActor() async throws {
// This prevents an issue where the main thread checker can get tripped up by us `stringify`ing
// the expression in ``execute`` (Polling+AsyncAwait version). Which is annoying if the test
// is otherwise correctly executing on the main thread.
// Double-y so if your CI automatically reads backtraces (like what the main thread checker will output) as test crashes,
// and fails your build.
struct MySubject: CustomDebugStringConvertible, Equatable {
var debugDescription: String {
expect(Thread.isMainThread).to(beTrue())
return "Test"
}
static func == (lhs: MySubject, rhs: MySubject) -> Bool {
Thread.isMainThread
}
}
try await require(MySubject()).toEventually(equal(MySubject()))
}
func testToEventuallyWithSyncExpectationAlwaysExecutesExpressionOnMainActor() async throws {
try await require(Thread.isMainThread).toEventually(beTrue())
try await require(Thread.isMainThread).toEventuallyNot(beFalse())
try await require(Thread.isMainThread).toAlways(beTrue(), until: .seconds(1))
try await require(Thread.isMainThread).toNever(beFalse(), until: .seconds(1))
}
func testToEventuallyWithAsyncExpectationDoesNotNecessarilyExecutesExpressionOnMainActor() async throws {
// This prevents a "Class property 'isMainThread' is unavailable from asynchronous contexts; Work intended for the main actor should be marked with @MainActor; this is an error in Swift 6" warning.
// However, the functionality actually works as you'd expect it to, you're just expected to tag things to use the main actor.
func isMainThread() -> Bool { Thread.isMainThread }
try await requirea(isMainThread()).toEventually(beFalse())
try await requirea(isMainThread()).toEventuallyNot(beTrue())
try await requirea(isMainThread()).toAlways(beFalse(), until: .seconds(1))
try await requirea(isMainThread()).toNever(beTrue(), until: .seconds(1))
}
@MainActor
func testToEventuallyWithAsyncExpectationDoesExecuteExpressionOnMainActorWhenTestRunsOnMainActor() async throws {
// This prevents a "Class property 'isMainThread' is unavailable from asynchronous contexts; Work intended for the main actor should be marked with @MainActor; this is an error in Swift 6" warning.
// However, the functionality actually works as you'd expect it to, you're just expected to tag things to use the main actor.
func isMainThread() -> Bool { Thread.isMainThread }
try await requirea(isMainThread()).toEventually(beTrue())
try await requirea(isMainThread()).toEventuallyNot(beFalse())
try await requirea(isMainThread()).toAlways(beTrue(), until: .seconds(1))
try await requirea(isMainThread()).toNever(beFalse(), until: .seconds(1))
}
func testToEventuallyWithCustomDefaultTimeout() async throws {
PollingDefaults.timeout = .seconds(2)
defer {
PollingDefaults.timeout = .seconds(1)
}
var value = 0
let sleepThenSetValueTo: (Int) -> Void = { newValue in
Thread.sleep(forTimeInterval: 1.1)
value = newValue
}
let task = Task {
sleepThenSetValueTo(1)
}
try await require { value }.toEventually(equal(1))
let secondTask = Task {
sleepThenSetValueTo(0)
}
try await require { value }.toEventuallyNot(equal(1))
_ = await task.value
_ = await secondTask.result
}
final class ClassUnderTest {
var deinitCalled: (() -> Void)?
var count = 0
deinit { deinitCalled?() }
}
func testSubjectUnderTestIsReleasedFromMemory() async throws {
var subject: ClassUnderTest? = ClassUnderTest()
if let sub = subject {
try await require(sub.count).toEventually(equal(0), timeout: .milliseconds(100))
try await require(sub.count).toEventuallyNot(equal(1), timeout: .milliseconds(100))
}
await waitUntil(timeout: .milliseconds(500)) { done in
subject?.deinitCalled = {
done()
}
deferToMainQueue { subject = nil }
}
}
func testToNeverPositiveMatches() async throws {
var value = 0
deferToMainQueue { value = 1 }
try await require { value }.toNever(beGreaterThan(1))
deferToMainQueue { value = 0 }
try await require { value }.neverTo(beGreaterThan(1))
}
func testToNeverNegativeMatches() async {
var value = 0
await failsWithErrorMessage("expected to never equal <0>, got <0>") {
try await require { value }.toNever(equal(0))
}
await failsWithErrorMessage("expected to never equal <0>, got <0>") {
try await require { value }.neverTo(equal(0))
}
await failsWithErrorMessage("expected to never equal <1>, got <1>") {
deferToMainQueue { value = 1 }
try await require { value }.toNever(equal(1))
}
await failsWithErrorMessage("expected to never equal <1>, got <1>") {
deferToMainQueue { value = 1 }
try await require { value }.neverTo(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await require { try self.doThrowError() }.toNever(equal(0))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await require { try self.doThrowError() }.neverTo(equal(0))
}
await failsWithErrorMessage("expected to never equal <1>, got <1>") {
try await require(1).toNever(equal(1))
}
}
func testToAlwaysPositiveMatches() async throws {
var value = 1
deferToMainQueue { value = 2 }
try await require { value }.toAlways(beGreaterThan(0))
deferToMainQueue { value = 2 }
try await require { value }.alwaysTo(beGreaterThan(1))
}
func testToAlwaysNegativeMatches() async {
var value = 1
await failsWithErrorMessage("expected to always equal <0>, got <1>") {
try await require { value }.toAlways(equal(0))
}
await failsWithErrorMessage("expected to always equal <0>, got <1>") {
try await require { value }.alwaysTo(equal(0))
}
await failsWithErrorMessage("expected to always equal <1>, got <0>") {
deferToMainQueue { value = 0 }
try await require { value }.toAlways(equal(1))
}
await failsWithErrorMessage("expected to always equal <1>, got <0>") {
deferToMainQueue { value = 0 }
try await require { value }.alwaysTo(equal(1))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await require { try self.doThrowError() }.toAlways(equal(0))
}
await failsWithErrorMessage("unexpected error thrown: <\(errorToThrow)>") {
try await require { try self.doThrowError() }.alwaysTo(equal(0))
}
await failsWithErrorMessage("expected to always equal <0>, got <nil> (use beNil() to match nils)") {
try await require(nil).toAlways(equal(0))
}
}
}
#endif