diff --git a/Example/Tests/PlayerEventsTest.swift b/Example/Tests/PlayerEventsTest.swift index a6feecd..d312143 100644 --- a/Example/Tests/PlayerEventsTest.swift +++ b/Example/Tests/PlayerEventsTest.swift @@ -14,7 +14,7 @@ import Nimble import Quick // swiftlint:disable:next type_body_length -class PlayerEventsTest: QuickSpec { +class PlayerEventsTest: AsyncSpec { // swiftlint:disable:next function_body_length override class func spec() { var playerDouble: BitmovinPlayerTestDouble! @@ -101,10 +101,9 @@ class PlayerEventsTest: QuickSpec { let spy = Spy(aClass: CISVideoAnalyticsTestDouble.self, functionName: "reportPlaybackEnded") playerDouble.fakePlayEvent() playerDouble.fakePlaybackFinishedEvent() - waitUntil { done in - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - done() - } + await waitUntil { done in + try? await Task.sleep(seconds: 0.1) + done() } expect(spy).to(haveBeenCalled()) } @@ -115,10 +114,9 @@ class PlayerEventsTest: QuickSpec { playerDouble.fakePlayEvent() playerDouble.fakePlaybackFinishedEvent() playerDouble.fakeAdBreakStartedEvent() - waitUntil { done in - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - done() - } + await waitUntil { done in + try? await Task.sleep(seconds: 0.1) + done() } expect(spy).toNot(haveBeenCalled()) } @@ -174,10 +172,9 @@ class PlayerEventsTest: QuickSpec { playerDouble.fakePlayingEvent() playerDouble.fakeStallStartedEvent() playerDouble.fakeStallEndedEvent() - waitUntil { done in - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - done() - } + await waitUntil { done in + try? await Task.sleep(seconds: 0.1) + done() } expect(spy).notTo( haveBeenCalled( @@ -203,11 +200,9 @@ class PlayerEventsTest: QuickSpec { it("on stall started / Stall Ended after 0.10 seconds") { playerDouble.fakePlayingEvent() playerDouble.fakeStallStartedEvent() - waitUntil { done in - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - playerDouble.fakeStallEndedEvent() - done() - } + await waitUntil { done in + try? await Task.sleep(seconds: 0.1) + done() } expect(spy).to( haveBeenCalled( @@ -219,10 +214,9 @@ class PlayerEventsTest: QuickSpec { } it("on stall started") { playerDouble.fakeStallStartedEvent() - waitUntil { done in - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - done() - } + await waitUntil { done in + try? await Task.sleep(seconds: 0.1) + done() } expect(spy).to( haveBeenCalled( @@ -348,10 +342,9 @@ class PlayerEventsTest: QuickSpec { playerDouble.fakePlayEvent() playerDouble.fakePlaybackFinishedEvent() - waitUntil { done in - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - done() - } + await waitUntil { done in + try? await Task.sleep(seconds: 0.1) + done() } expect(spy).to(haveBeenCalled()) expect(playbackStateSpy).to( @@ -370,10 +363,9 @@ class PlayerEventsTest: QuickSpec { playerDouble.fakePlayEvent() playerDouble.fakePlaylistTransitionEvent() - waitUntil { done in - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { - done() - } + await waitUntil { done in + try? await Task.sleep(seconds: 0.1) + done() } expect(spy).to(haveBeenCalled()) expect(playbackStateSpy).to( @@ -721,3 +713,15 @@ class PlayerEventsTest: QuickSpec { } } } + +private extension Task { + static func sleep(seconds: TimeInterval) async throws { + try await Task.sleep(nanoseconds: .seconds(seconds)) + } +} + +private extension UInt64 { + static func seconds(_ seconds: TimeInterval) -> UInt64 { + UInt64(seconds * Double(NSEC_PER_SEC)) + } +}