-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathTestBroadcast.swift
56 lines (53 loc) · 1.46 KB
/
TestBroadcast.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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
@preconcurrency import XCTest
import AsyncAlgorithms
final class TestBroadcast: XCTestCase {
func test_basic_broadcasting() async {
let base = [1, 2, 3, 4].async
let a = base.broadcast()
let b = a
let results = await withTaskGroup(of: [Int].self) { group in
group.addTask {
await Array(a)
}
group.addTask {
await Array(b)
}
return await Array(group)
}
XCTAssertEqual(results[0], results[1])
}
func test_basic_broadcasting_from_channel() async {
let base = AsyncChannel<Int>()
let a = base.broadcast()
let b = a
let results = await withTaskGroup(of: [Int].self) { group in
group.addTask {
var sent = [Int]()
for i in 0..<10 {
sent.append(i)
await base.send(i)
}
base.finish()
return sent
}
group.addTask {
await Array(a)
}
group.addTask {
await Array(b)
}
return await Array(group)
}
XCTAssertEqual(results[0], results[1])
}
}