- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestRunLoop.swift
206 lines (157 loc) · 7.42 KB
/
TestRunLoop.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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
classTestRunLoop:XCTestCase{
func test_constants(){
XCTAssertEqual(RunLoop.Mode.common.rawValue,"kCFRunLoopCommonModes",
"\(RunLoop.Mode.common.rawValue) is not equal to kCFRunLoopCommonModes")
XCTAssertEqual(RunLoop.Mode.default.rawValue,"kCFRunLoopDefaultMode",
"\(RunLoop.Mode.default.rawValue) is not equal to kCFRunLoopDefaultMode")
}
func test_runLoopInit(){
letmainRunLoop=RunLoop.main
letcurrentRunLoop=RunLoop.current
letsecondAccessOfMainLoop=RunLoop.main
XCTAssertEqual(mainRunLoop, secondAccessOfMainLoop,"fetching the main loop a second time should be equal")
XCTAssertTrue(mainRunLoop === secondAccessOfMainLoop,"fetching the main loop a second time should be identical")
letsecondAccessOfCurrentLoop=RunLoop.current
XCTAssertEqual(currentRunLoop, secondAccessOfCurrentLoop,"fetching the current loop a second time should be equal")
XCTAssertTrue(currentRunLoop === secondAccessOfCurrentLoop,"fetching the current loop a second time should be identical")
// We can assume that the tests will be run on the main run loop
// so the current loop should be the main loop
XCTAssertEqual(mainRunLoop, currentRunLoop,"the current run loop should be the main loop")
}
func test_runLoopRunMode(){
// RunLoop is not sendable, but we do want to verify that the current run loop here is the one we access below in the Timer block
nonisolated(unsafe)letrunLoop=RunLoop.current
lettimeInterval=TimeInterval(0.05)
letendDate=Date(timeInterval: timeInterval, since:Date())
// Protected by the ordering of the run loop - that is what this test verifies
nonisolated(unsafe)varflag=false
letdummyTimer=Timer.scheduledTimer(withTimeInterval:0.01, repeats:false){ _ in
flag =true
guardlet runLoopMode = runLoop.currentMode else{
XCTFail("Run loop mode is not defined")
return
}
XCTAssertEqual(runLoopMode,RunLoop.Mode.default)
}
runLoop.add(dummyTimer, forMode:.default)
letresult= runLoop.run(mode:.default, before: endDate)
XCTAssertTrue(result)
XCTAssertTrue(flag)
}
func test_runLoopLimitDate(){
letrunLoop=RunLoop.current
lettimeInterval=TimeInterval(1)
letexpectedTimeInterval=Date(timeInterval: timeInterval, since:Date()).timeIntervalSince1970
letdummyTimer=Timer.scheduledTimer(withTimeInterval: timeInterval, repeats:false){ _ in}
runLoop.add(dummyTimer, forMode:.default)
guardlet timerTickInterval = runLoop.limitDate(forMode:.default)?.timeIntervalSince1970 else{
return
}
XCTAssertLessThan(abs(timerTickInterval - expectedTimeInterval),0.01)
}
func test_runLoopPoll(){
letrunLoop=RunLoop.current
letstartDate=Date()
runLoop.run(until:Date())
letendDate=Date()
XCTAssertLessThan(endDate.timeIntervalSince(startDate),0.5)
}
func test_commonModes(){
letrunLoop=RunLoop.current
letdone=expectation(description:"The timer has fired")
lettimer=Timer(timeInterval:1, repeats:false){(_)in
done.fulfill()
}
runLoop.add(timer, forMode:.common)
waitForExpectations(timeout:10)
}
func test_addingRemovingPorts(){
letrunLoop=RunLoop.current
vardidDeallocate=false
do{
letport=TestPort{
didDeallocate =true
}
letcustomMode=RunLoop.Mode(rawValue:"Custom")
XCTAssertEqual(port.scheduledModes,[])
runLoop.add(port, forMode:.default)
XCTAssertEqual(port.scheduledModes,[.default])
runLoop.add(port, forMode:.default)
XCTAssertEqual(port.scheduledModes,[.default])
runLoop.add(port, forMode: customMode)
XCTAssertEqual(port.scheduledModes,[.default, customMode])
runLoop.remove(port, forMode: customMode)
XCTAssertEqual(port.scheduledModes,[.default])
runLoop.add(port, forMode: customMode)
XCTAssertEqual(port.scheduledModes,[.default, customMode])
port.invalidate()
}
XCTAssertTrue(didDeallocate)
}
nonisolated(unsafe)varasyncExecuted=false
func test_mainDispatchQueueCallout(){
letrunLoop=RunLoop.current
nonisolated(unsafe)letnonisolatedSelf=self
DispatchQueue.main.async{
nonisolatedSelf.asyncExecuted =true
}
// RunLoop should service main queue
_ = runLoop.run(mode:.default, before:Date(timeIntervalSinceNow:2))
XCTAssertTrue(asyncExecuted,"Main queue async code should be executed")
asyncExecuted =false
DispatchQueue.main.async{
nonisolatedSelf.asyncExecuted =true
}
// Second run to be sure RunLoop will not stuck
_ = runLoop.run(mode:.default, before:Date(timeIntervalSinceNow:2))
XCTAssertTrue(asyncExecuted,"Main queue async code should be executed")
// Protected by the ordering of the run loop - that is what this test verifies
nonisolated(unsafe)vartimerFired=false
letdummyTimer=Timer.scheduledTimer(withTimeInterval:0.5, repeats:false){ _ in
timerFired =true
}
runLoop.add(dummyTimer, forMode:.default)
// At this moment RunLoop has no work to do except waiting for timer.
// But RunLoop will exit prematurely if event from previous async calls
// got stuck in wrong state.
_ = runLoop.run(mode:.default, before:Date(timeIntervalSinceNow:2))
XCTAssertTrue(timerFired,"Time should fire already")
}
}
classTestPort:Port{
letsentinel:()->Void
init(sentinel:@escaping()->Void){
self.sentinel = sentinel
super.init()
}
// Required on Darwin
requiredinit?(coder:NSCoder){
fatalError("init(coder:) has not been implemented")
}
deinit{
invalidate()
sentinel()
}
privatevar_isValid=true
openoverridevarisValid:Bool{return _isValid }
openoverridefunc invalidate(){
guard isValid else{return}
_isValid =false
NotificationCenter.default.post(name:Port.didBecomeInvalidNotification, object:self)
}
varscheduledModes:[RunLoop.Mode]=[]
openoverridefunc schedule(in runLoop:RunLoop, forMode mode:RunLoop.Mode){
scheduledModes.append(mode)
}
openoverridefunc remove(from runLoop:RunLoop, forMode mode:RunLoop.Mode){
scheduledModes = scheduledModes.filter{ $0 != mode }
}
}