-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathJSTimerTests.swift
56 lines (48 loc) · 1.74 KB
/
JSTimerTests.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
import XCTest
@testable import JavaScriptKit
final class JSTimerTests: XCTestCase {
func testOneshotTimerCancelled() {
let timeoutMilliseconds = 5.0
var timeout: JSTimer!
timeout = JSTimer(millisecondsDelay: timeoutMilliseconds, isRepeating: false) {
XCTFail("timer should be cancelled")
}
_ = timeout
timeout = nil
}
func testRepeatingTimerCancelled() async throws {
var count = 0.0
let maxCount = 5.0
var interval: JSTimer?
let start = JSDate().valueOf()
let timeoutMilliseconds = 5.0
await withCheckedContinuation { continuation in
interval = JSTimer(millisecondsDelay: 5, isRepeating: true) {
// ensure that JSTimer is living
XCTAssertNotNil(interval)
// verify that at least `timeoutMilliseconds * count` passed since the `timeout`
// timer started
XCTAssertTrue(start + timeoutMilliseconds * count <= JSDate().valueOf())
guard count < maxCount else {
// stop the timer after `maxCount` reached
interval = nil
continuation.resume()
return
}
count += 1
}
}
withExtendedLifetime(interval) {}
}
func testTimer() async throws {
let start = JSDate().valueOf()
let timeoutMilliseconds = 5.0
var timeout: JSTimer!
await withCheckedContinuation { continuation in
timeout = JSTimer(millisecondsDelay: timeoutMilliseconds, isRepeating: false) {
continuation.resume()
}
}
withExtendedLifetime(timeout) {}
}
}