-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaffeineKitTests.swift
More file actions
258 lines (234 loc) · 7.41 KB
/
CaffeineKitTests.swift
File metadata and controls
258 lines (234 loc) · 7.41 KB
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
import XCTest
@testable import CaffeineKit
@available(macOS 10.13, *)
extension Process {
convenience init(_ executablePath: String, _ arguments: String...) {
self.init()
self.executableURL = URL(fileURLWithPath: executablePath)
self.arguments = arguments
self.standardOutput = FileHandle.nullDevice
self.standardError = FileHandle.nullDevice
}
func runSynchronously() {
try! self.run()
self.waitUntilExit()
}
}
@available(macOS 10.13, *)
final class CaffeineKitTests: XCTestCase {
// MARK: - Utilities
var cafProcExists: Bool {
get {
let proc = Process("/usr/bin/killall", "-0", "caffeinate")
proc.runSynchronously()
return proc.terminationStatus == 0
}
}
// // Workaround for Swift (?) bug
// func execute(_ closure: () -> Void) {
// closure()
// }
// MARK: - Tests
// FIXME: Fails when run in batch tests, but succeeds when run independently
func testCaffeinateProcessSpawnsAndDies() {
let caf = Caffeination()
XCTAssert(!caf.isActive)
try! caf.start()
XCTAssert(caf.isActive)
XCTAssert(cafProcExists)
caf.stop()
XCTAssert(!cafProcExists)
XCTAssert(!caf.isActive)
}
func testApproximateTimedAccuracy() {
let expectation = self.expectation(description: "Caffeination finished")
let caf = Caffeination(withOpts: [.idle, .display, .timed(2)]) { caffeination in
expectation.fulfill()
}
try! caf.start()
wait(for: [expectation], timeout: 2.5)
caf.stop()
}
func testProcess() {
let proc = Process("/bin/cat")
try! proc.run()
let caf = Caffeination(withOpts: [.idle, .display, .process(proc.processIdentifier)])
try! caf.start()
Thread.sleep(forTimeInterval: 2)
proc.terminate()
Thread.sleep(forTimeInterval: 0.1)
XCTAssert(!cafProcExists)
XCTAssert(!caf.isActive)
}
func testNoErrorOnBadStopCall() {
let caf = Caffeination()
caf.stop()
try! caf.start()
caf.stop()
caf.stop()
}
func testSimpleCaffeinationReusability() {
let caf = Caffeination()
try! caf.start()
caf.stop()
do {
try caf.start()
} catch {
XCTFail("Caffeination couldn't be reused")
}
caf.stop()
}
func testCaffeinateEndedInTerminationHandler() {
let caf = Caffeination() { caffeination in
XCTAssert(!self.cafProcExists)
}
try! caf.start()
caf.stop()
}
func testChangeTerminationHandler() {
let exp1 = expectation(description: "Handler 1")
let exp2 = expectation(description: "Handler 2")
let caf = Caffeination() { caffeination in
exp1.fulfill()
}
try! caf.start()
caf.stop()
wait(for: [exp1], timeout: 1)
caf.terminationHandler = { caffeination in
exp2.fulfill()
}
try! caf.start()
caf.stop()
wait(for: [exp2], timeout: 1)
}
func testChangeTimedOptReusability() {
let exp1 = expectation(description: "Caffeination 1 finished")
let caf = Caffeination(withOpts: [.idle, .display, .timed(2)]) { caffeination in
exp1.fulfill()
}
try! caf.start()
XCTAssert(cafProcExists)
XCTAssert(caf.isActive)
wait(for: [exp1], timeout: 2.5)
XCTAssert(!cafProcExists)
XCTAssert(!caf.isActive)
caf.stop()
caf.opts[2] = .timed(1)
let exp2 = expectation(description: "Caffeination 2 finished")
caf.terminationHandler = { caffeination in
exp2.fulfill()
}
try! caf.start()
XCTAssert(cafProcExists)
XCTAssert(caf.isActive)
wait(for: [exp2], timeout: 1.5)
XCTAssert(!cafProcExists)
XCTAssert(!caf.isActive)
caf.stop()
}
func testThrowingWhenAlreadyStarted() {
let caf = Caffeination()
do {
try caf.start()
} catch {
XCTFail("First start didn't succeed")
}
do {
try caf.start()
XCTFail("Should have thrown")
} catch let e as CaffeinationError {
switch e {
case .alreadyActive:
break
default:
XCTFail("Wrong error thrown")
}
} catch {
XCTFail("Wrong, non-CaffeinationError error thrown")
}
XCTAssert(cafProcExists)
XCTAssert(caf.isActive)
caf.stop()
XCTAssert(!cafProcExists)
XCTAssert(!caf.isActive)
}
func testClosureLifeCycle() {
let caf = Caffeination()
XCTAssert(!cafProcExists)
let closure = try! caf.closure { () -> Void in
XCTAssert(self.cafProcExists)
}
closure(())
XCTAssert(!cafProcExists)
}
func testClosureIgnoresTimed() {
let caf = Caffeination(withOpts: [.idle, .display, .timed(5)])
let closure = try! caf.closure {
XCTAssert(self.cafProcExists)
}
closure(())
XCTAssert(!cafProcExists)
}
func testClosureIgnoresProc() {
let proc = Process("/bin/cat")
try! proc.run()
let caf = Caffeination(withOpts: [.idle, .display, .process(proc.processIdentifier)])
let closure = try! caf.closure {
XCTAssert(self.cafProcExists)
}
closure(())
XCTAssert(!cafProcExists)
proc.terminate()
}
// MARK: - Testing argument/Opt interop
var mappings: [String: Caffeination.Opt] = [
"-d": .display,
"-i": .idle,
"-m": .disk,
"-u": .user,
"-s": .system
]
func testGetRandomSingleArg() {
let arg = mappings.randomElement()!
guard let opt = Caffeination.Opt.from(arg.key) else {
XCTFail("Couldn't instantiate opt from \(arg.key)")
return
}
// Hacky, but good enough for unit testing
XCTAssert(String(describing: opt) == String(describing: arg.value))
}
func testGetPIDFromArray() {
let rand = Int32.random(in: 0..<Int32.max)
let opt = Caffeination.Opt.from(["-w", String(rand)])
if case .process(rand)? = opt {} else {
XCTFail()
}
let nilOpt = Caffeination.Opt.from([String(rand), "-w"])
XCTAssert(nilOpt == nil)
}
func testGetTimeFromArray() {
let rand = Double.random(in: 0..<60*60*24*365)
let opt = Caffeination.Opt.from(["-t", String(rand)])
if case .timed(rand)? = opt {} else {
XCTFail()
}
let nilOpt = Caffeination.Opt.from([String(rand), "-t"])
XCTAssert(nilOpt == nil)
}
func testDuplicateOptsThrows() {
let caf = Caffeination(withOpts: [.timed(2), .disk, .timed(4)])
do {
try caf.start()
XCTFail("Duplicate opts, so error should have been thrown")
} catch let e as CaffeinationError {
switch e {
case .duplicateOpts:
break
default:
XCTFail("Wrong error thrown for duplicate opts")
}
} catch {
XCTFail("Non-CaffeinationError thrown for duplicate opts")
}
}
}