-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathSwiftConcurrencyTests.swift
129 lines (118 loc) · 3.5 KB
/
SwiftConcurrencyTests.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
//
// SwiftConcurrencyTests.swift
// ReactiveSwift
//
// Created by Marco Cancellieri on 2021-11-11.
// Copyright (c) 2021 GitHub. All rights reserved.
//
#if compiler(>=5.5) && canImport(_Concurrency)
import Foundation
import ReactiveSwift
import XCTest
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, macCatalyst 15, *)
class SwiftConcurrencyTests: XCTestCase {
func testValuesAsyncSignalProducer() async {
let values = [1,2,3]
var sum = 0
let asyncStream = SignalProducer(values).asyncStream
for await number in asyncStream {
sum += number
}
XCTAssertEqual(sum, 6)
}
func testValuesAsyncThrowingSignalProducer() async throws {
let values = [1,2,3]
var sum = 0
let asyncStream = SignalProducer(values).asyncThrowingStream
for try await number in asyncStream {
sum += number
}
XCTAssertEqual(sum, 6)
}
func testCompleteAsyncSignalProducer() async {
let asyncStream = SignalProducer<String, Never>.empty.asyncStream
let first = await asyncStream.first(where: { _ in true })
XCTAssertEqual(first, nil)
}
func testCompleteAsyncThrowingSignalProducer() async throws {
let asyncStream = SignalProducer<String, Error>.empty.asyncThrowingStream
let first = try await asyncStream.first(where: { _ in true })
XCTAssertEqual(first, nil)
}
func testErrorSignalProducer() async {
let error = NSError(domain: "domain", code: 0, userInfo: nil)
let asyncStream = SignalProducer<String, Error>(error: error).asyncThrowingStream
await XCTAssertThrowsError(try await asyncStream.first(where: { _ in true }))
}
func testValuesAsyncSignal() async {
let signal = Signal<Int, Never> { observer, _ in
Task {
for number in [1, 2, 3] {
observer.send(value: number)
}
observer.sendCompleted()
}
}
var sum = 0
let asyncStream = signal.asyncStream
for await number in asyncStream {
sum += number
}
XCTAssertEqual(sum, 6)
}
func testValuesAsyncThrowingSignal() async throws {
let signal = Signal<Int, Never> { observer, _ in
Task {
for number in [1, 2, 3] {
observer.send(value: number)
}
observer.sendCompleted()
}
}
var sum = 0
let asyncStream = signal.asyncThrowingStream
for try await number in asyncStream {
sum += number
}
XCTAssertEqual(sum, 6)
}
func testCompleteAsyncSignal() async {
let asyncStream = Signal<String, Never>.empty.asyncStream
let first = await asyncStream.first(where: { _ in true })
XCTAssertEqual(first, nil)
}
func testCompleteAsyncThrowingSignal() async throws {
let asyncStream = Signal<String, Error>.empty.asyncThrowingStream
let first = try await asyncStream.first(where: { _ in true })
XCTAssertEqual(first, nil)
}
func testErrorSignal() async {
let error = NSError(domain: "domain", code: 0, userInfo: nil)
let signal = Signal<String, Error> { observer, _ in
Task {
observer.send(error: error)
}
}
let asyncStream = signal.asyncThrowingStream
await XCTAssertThrowsError(try await asyncStream.first(where: { _ in true }))
}
}
// Extension to allow Throw assertion for async expressions
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, macCatalyst 15, *)
fileprivate extension XCTest {
func XCTAssertThrowsError<T: Sendable>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line,
_ errorHandler: (_ error: Error) -> Void = { _ in }
) async {
do {
_ = try await expression()
XCTFail(message(), file: file, line: line)
} catch {
errorHandler(error)
}
}
}
#endif