Skip to content

Commit

Permalink
stabilize unit tests by using Swift concurrency for sleeping
Browse files Browse the repository at this point in the history
  • Loading branch information
stonko1994 committed Aug 26, 2024
1 parent 34a1e48 commit 7e9eb80
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions Example/Tests/PlayerEventsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -721,3 +713,15 @@ class PlayerEventsTest: QuickSpec {
}
}
}

private extension Task<Never, Never> {
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))
}
}

0 comments on commit 7e9eb80

Please sign in to comment.