-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathMainThreadSerialDispatcherTests.swift
58 lines (45 loc) · 1.37 KB
/
MainThreadSerialDispatcherTests.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
import XCTest
@testable import DiffableDataSources
final class MainThreadSerialDispatcherTests: XCTestCase {
func testMainThread() {
let dispatcher = MainThreadSerialDispatcher()
let queue = DispatchQueue.global()
let expectation = self.expectation(description: "testMainThread")
queue.async {
dispatcher.dispatch {
XCTAssertTrue(Thread.isMainThread)
expectation.fulfill()
}
}
waitForExpectations(timeout: 1)
}
func testMainThreadSerial() {
let dispatcher = MainThreadSerialDispatcher()
let queue = DispatchQueue.global()
let expectation = self.expectation(description: "testMainThreadSerial")
var array = [Int]()
let group = DispatchGroup()
queue.async(group: group) {
dispatcher.dispatch {
array.append(0)
}
}
group.wait()
queue.async(group: group) {
dispatcher.dispatch {
array.append(1)
}
dispatcher.dispatch {
array.append(2)
}
}
group.wait()
dispatcher.dispatch {
array.append(3)
expectation.fulfill()
}
waitForExpectations(timeout: 1) { _ in
XCTAssertEqual(array, [0, 1, 2, 3])
}
}
}