-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTestTimer.swift
105 lines (82 loc) · 3.55 KB
/
TestTimer.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
// 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
//
import Synchronization
class TestTimer : XCTestCase {
func test_timerInit() {
let fireDate = Date()
let timeInterval: TimeInterval = 0.3
let timer = Timer(fire: fireDate, interval: timeInterval, repeats: false) { _ in }
XCTAssertEqual(timer.fireDate, fireDate)
XCTAssertEqual(timer.timeInterval, 0, "Time interval should be 0 for a non repeating Timer")
XCTAssert(timer.isValid)
let repeatingTimer = Timer(fire: fireDate, interval: timeInterval, repeats: true) { _ in }
XCTAssertEqual(repeatingTimer.fireDate, fireDate)
XCTAssertEqual(repeatingTimer.timeInterval, timeInterval)
XCTAssert(timer.isValid)
}
func test_timerTickOnce() {
let flag = Atomic(false)
let dummyTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: false) { timer in
let (exchanged, original) = flag.compareExchange(expected: false, desired: true, ordering: .relaxed)
XCTAssertFalse(original)
XCTAssertTrue(exchanged)
timer.invalidate()
}
let runLoop = RunLoop.current
runLoop.add(dummyTimer, forMode: .default)
runLoop.run(until: Date(timeIntervalSinceNow: 0.05))
XCTAssertTrue(flag.load(ordering: .relaxed))
}
func test_timerRepeats() {
let flag = Mutex(0)
let interval = TimeInterval(0.1)
let numberOfRepeats = 3
let previousInterval = Mutex(Date().timeIntervalSince1970)
let dummyTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { timer in
XCTAssertEqual(timer.timeInterval, interval)
let currentInterval = Date().timeIntervalSince1970
previousInterval.withLock {
XCTAssertEqual(currentInterval, $0 + interval, accuracy: 0.2)
$0 = currentInterval
}
let invalidate = flag.withLock {
$0 += 1
if $0 == numberOfRepeats {
return true
} else {
return false
}
}
if invalidate {
timer.invalidate()
}
}
let runLoop = RunLoop.current
runLoop.add(dummyTimer, forMode: .default)
runLoop.run(until: Date(timeIntervalSinceNow: interval * Double(numberOfRepeats + 1)))
flag.withLock {
XCTAssertEqual($0, numberOfRepeats)
}
}
func test_timerInvalidate() {
// Only mutated once, protected by behavior of RunLoop validated in this test
nonisolated(unsafe) var flag = false
let dummyTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { timer in
XCTAssertTrue(timer.isValid)
XCTAssertFalse(flag) // timer should tick only once
flag = true
timer.invalidate()
XCTAssertFalse(timer.isValid)
}
let runLoop = RunLoop.current
runLoop.add(dummyTimer, forMode: .default)
runLoop.run(until: Date(timeIntervalSinceNow: 0.05))
XCTAssertTrue(flag)
}
}