Skip to content

Commit d4d98b0

Browse files
authored
Fix TimerUtilsTest case (#278)
1 parent a3a0c14 commit d4d98b0

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

Tests/OpenSwiftUICoreTests/Util/TimerUtilsTests.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ struct TimerUtilsTests {
2828
var callbackExecuted = false
2929

3030
let startTime = Date()
31-
let delayInterval: TimeInterval = 0.2
31+
let delayInterval: TimeInterval = 2
3232

3333
let timer = withDelay(delayInterval) {
3434
callbackExecuted = true
3535
confirmation()
3636
}
3737
#expect(timer.isValid == true)
3838
#expect(callbackExecuted == false)
39-
try await Task.sleep(nanoseconds: 300_000_000)
39+
try await Task.sleep(for: .seconds(5))
4040

4141
#expect(callbackExecuted == true)
4242
let elapsedTime = Date().timeIntervalSince(startTime)
@@ -47,13 +47,13 @@ struct TimerUtilsTests {
4747
@Test
4848
func withDelayCanBeCancelled() async throws {
4949
var callbackExecuted = false
50-
let timer = withDelay(0.2) {
50+
let timer = withDelay(2) {
5151
callbackExecuted = true
5252
}
5353
#expect(timer.isValid == true)
5454

5555
timer.invalidate()
56-
try await Task.sleep(nanoseconds: 300_000_000)
56+
try await Task.sleep(for: .seconds(5))
5757

5858
#expect(timer.isValid == false)
5959
#expect(callbackExecuted == false)
@@ -62,11 +62,11 @@ struct TimerUtilsTests {
6262
@Test
6363
func withDelayRunsOnMainRunLoop() async throws {
6464
try await confirmation { confirmation in
65-
let _ = withDelay(0.2) {
65+
let _ = withDelay(2) {
6666
#expect(Thread.isMainThread)
6767
confirmation()
6868
}
69-
try await Task.sleep(nanoseconds: 300_000_000)
69+
try await Task.sleep(for: .seconds(5))
7070
}
7171
}
7272
}
@@ -78,20 +78,18 @@ final class TimerUtilsXCTests: XCTestCase {
7878
var callbackExecuted = false
7979

8080
let startTime = Date()
81-
let delayInterval: TimeInterval = 0.2
81+
let delayInterval: TimeInterval = 2
8282

8383
let timer = withDelay(delayInterval) {
8484
callbackExecuted = true
8585
expectation.fulfill()
8686
}
8787
XCTAssertTrue(timer.isValid)
8888
XCTAssertFalse(callbackExecuted)
89-
// 0.3s will sometimes fail for macOS + 2021 platform
90-
await fulfillment(of: [expectation], timeout: 1)
89+
await fulfillment(of: [expectation], timeout: 5)
9190

9291
XCTAssertTrue(callbackExecuted)
9392
let elapsedTime = Date().timeIntervalSince(startTime)
94-
print("Elapsed time: \(elapsedTime)")
9593
#if !canImport(Darwin)
9694
// FIXE: The elapsed time is somehow not correct on non-Darwin platform
9795
throw XCTSkip("The elapsed time is somehow not correct on non-Darwin platform")
@@ -101,26 +99,23 @@ final class TimerUtilsXCTests: XCTestCase {
10199

102100
func testWithDelayCanBeCancelled() async throws {
103101
var callbackExecuted = false
104-
let timer = withDelay(0.2) {
102+
let timer = withDelay(2) {
105103
callbackExecuted = true
106104
}
107105
XCTAssertTrue(timer.isValid)
108106

109107
timer.invalidate()
110-
try await Task.sleep(nanoseconds: 300_000_000)
108+
try await Task.sleep(for: .seconds(5))
111109
XCTAssertFalse(timer.isValid)
112110
XCTAssertFalse(callbackExecuted)
113111
}
114112

115113
func testWithDelayRunsOnMainRunLoop() async throws {
116-
#if canImport(Darwin)
117-
throw XCTSkip("Skip flaky test case temporary")
118-
#endif
119114
let expectation = expectation(description: "withDelay body call")
120-
let _ = withDelay(0.2) {
115+
let _ = withDelay(2) {
121116
XCTAssertTrue(Thread.isMainThread)
122117
expectation.fulfill()
123118
}
124-
await fulfillment(of: [expectation], timeout: 0.3)
119+
await fulfillment(of: [expectation], timeout: 5)
125120
}
126121
}

0 commit comments

Comments
 (0)