Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid flaky expectations capturing values during a hardcoded time interval #1099

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Tests/AnalyticsTests/ComScore/ComScorePageViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ final class ComScorePageViewTests: ComScoreTestCase {

func testAutomaticTrackingWithoutProtocolImplementation() {
let viewController = UIViewController()
expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
viewController.simulateViewAppearance()
}
}

func testManualTrackingWithoutProtocolImplementation() {
let viewController = UIViewController()
expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
viewController.trackPageView()
}
}
Expand Down Expand Up @@ -179,7 +179,7 @@ final class ComScorePageViewTests: ComScoreTestCase {
let viewController2 = AutomaticMockViewController(title: "title2")
let tabBarController = UITabBarController()
tabBarController.viewControllers = [viewController1, viewController2]
expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
viewController2.simulateViewAppearance()
}
}
Expand Down Expand Up @@ -209,14 +209,14 @@ final class ComScorePageViewTests: ComScoreTestCase {
UINavigationController(rootViewController: viewController1),
UINavigationController(rootViewController: viewController2)
]
expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
viewController2.simulateViewAppearance()
}
}

func testManualTracking() {
let viewController = ManualMockViewController()
expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
viewController.simulateViewAppearance()
}
expectAtLeastHits(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class ComScoreTrackerMetadataTests: ComScoreTestCase {
]
))

expectNoHits(during: .seconds(3)) {
expectNoHits(during: .milliseconds(500)) {
player.play()
}
}
Expand All @@ -53,7 +53,7 @@ final class ComScoreTrackerMetadataTests: ComScoreTestCase {
]
))

expectNoHits(during: .seconds(3)) {
expectNoHits(during: .milliseconds(500)) {
player.play()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class ComScoreTrackerRateTests: ComScoreTestCase {
player.play()
expect(player.playbackState).toEventually(equal(.playing))

expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player.setDesiredPlaybackSpeed(1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class ComScoreTrackerSeekTests: ComScoreTestCase {

expect(player.playbackState).toEventually(equal(.paused))

expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player.seek(at(.init(value: 7, timescale: 1)))
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/AnalyticsTests/ComScore/ComScoreTrackerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class ComScoreTrackerTests: ComScoreTestCase {
ComScoreTracker.adapter { _ in .test }
]
))
expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player.pause()
}
}
Expand Down Expand Up @@ -132,7 +132,7 @@ final class ComScoreTrackerTests: ComScoreTestCase {
ComScoreTracker.adapter { _ in .test }
]
))
expectNoHits(during: .seconds(3)) {
expectNoHits(during: .milliseconds(500)) {
player.play()
}
}
Expand Down Expand Up @@ -163,7 +163,7 @@ final class ComScoreTrackerTests: ComScoreTestCase {

player.isTrackingEnabled = false

expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player.play()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class CommandersActHeartbeatTests: CommandersActTestCase {

func testNoHeartbeatInitially() {
_ = Self.player(from: .onDemand, into: &cancellables)
expectNoHits(during: .milliseconds(300))
expectNoHits(during: .milliseconds(500))
}

func testOnDemandHeartbeatAfterPlay() {
Expand Down Expand Up @@ -63,7 +63,7 @@ final class CommandersActHeartbeatTests: CommandersActTestCase {
expectAtLeastHits(pos()) {
player.play()
}
expectNoHits(during: .milliseconds(300)) {
expectNoHits(during: .milliseconds(500)) {
player.pause()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,12 @@ final class CommandersActTrackerDvrPropertiesTests: CommandersActTestCase {
CommandersActTracker.adapter { _ in .test }
]
))

player?.play()
expect(player?.playbackState).toEventually(equal(.playing))

player?.pause()
wait(for: .seconds(2))

expectAtLeastHits(play()) {
player?.play()
}
expectAtLeastHits(
stop { labels in
expect(labels.media_position).to(equal(0))
expect(labels.media_position).notTo(beNil())
}
) {
player = nil
Expand All @@ -119,7 +115,7 @@ final class CommandersActTrackerDvrPropertiesTests: CommandersActTestCase {
))
expect(player?.playbackState).toEventually(equal(.paused))

expectNoHits(during: .seconds(5)) {
expectNoHits(during: .milliseconds(500)) {
player = nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class CommandersActTrackerSeekTests: CommandersActTestCase {

expect(player.playbackState).toEventually(equal(.paused))

expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player.seek(at(.init(value: 7, timescale: 1)))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class CommandersActTrackerTests: CommandersActTestCase {
CommandersActTracker.adapter { _ in [:] }
]
))
expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player.pause()
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ final class CommandersActTrackerTests: CommandersActTestCase {
player?.play()
}

expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player = nil
}
}
Expand All @@ -141,7 +141,7 @@ final class CommandersActTrackerTests: CommandersActTestCase {
CommandersActTracker.adapter { _ in [:] }
]
))
expectNoHits(during: .seconds(3)) {
expectNoHits(during: .milliseconds(500)) {
player.play()
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ final class CommandersActTrackerTests: CommandersActTestCase {

player.isTrackingEnabled = false

expectNoHits(during: .seconds(2)) {
expectNoHits(during: .milliseconds(500)) {
player.play()
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/CoreTests/DispatchPublisherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ final class DispatchPublisherTests: XCTestCase {
let delayedPublisher = [1, 2, 3].publisher
.delayIfNeeded(for: 0.1, scheduler: DispatchQueue.main)
let subject = CurrentValueSubject<Int, Never>(0)
expectEqualPublished(values: [0, 1, 2, 3], from: Publishers.Merge(delayedPublisher, subject), during: .milliseconds(100))
expectAtLeastEqualPublished(values: [0, 1, 2, 3], from: Publishers.Merge(delayedPublisher, subject))
}

func testDelayIfNeededOutputOrderingWithZeroDelay() {
let delayedPublisher = [1, 2, 3].publisher
.delayIfNeeded(for: 0, scheduler: DispatchQueue.main)
let subject = CurrentValueSubject<Int, Never>(0)
expectEqualPublished(values: [1, 2, 3, 0], from: Publishers.Merge(delayedPublisher, subject), during: .milliseconds(100))
expectAtLeastEqualPublished(values: [1, 2, 3, 0], from: Publishers.Merge(delayedPublisher, subject))
}
}
4 changes: 2 additions & 2 deletions Tests/CoreTests/MeasurePublisherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ final class MeasurePublisherTests: XCTestCase {
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main)
.measureDateInterval()
.map(\.duration)
expectPublished(values: [0.5], from: publisher, to: beClose(within: 0.1), during: .seconds(1))
expectAtLeastPublished(values: [0.5], from: publisher, to: beClose(within: 0.1))
}

func testWithMultipleEvents() {
let publisher = [1, 2].publisher
.delay(for: .milliseconds(500), scheduler: DispatchQueue.main)
.measureDateInterval()
.map(\.duration)
expectPublished(values: [0.5, 0], from: publisher, to: beClose(within: 0.1), during: .seconds(1))
expectAtLeastPublished(values: [0.5, 0], from: publisher, to: beClose(within: 0.1))
}

func testWithoutEvents() {
Expand Down
16 changes: 8 additions & 8 deletions Tests/CoreTests/ReplaySubjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ final class ReplaySubjectTests: XCTestCase {
subject.send(1)
subject.send(2)
subject.send(3)
expectEqualPublished(values: [2, 3], from: subject, during: .milliseconds(100))
expectAtLeastEqualPublished(values: [2, 3], from: subject)
}

func testNewValuesWithBufferOfZero() {
let subject = ReplaySubject<Int, Never>(bufferSize: 0)
subject.send(1)
expectEqualPublished(values: [2, 3], from: subject, during: .milliseconds(100)) {
expectAtLeastEqualPublished(values: [2, 3], from: subject) {
subject.send(2)
subject.send(3)
}
Expand All @@ -50,7 +50,7 @@ final class ReplaySubjectTests: XCTestCase {
subject.send(1)
subject.send(2)
subject.send(3)
expectEqualPublished(values: [2, 3, 4, 5], from: subject, during: .milliseconds(100)) {
expectAtLeastEqualPublished(values: [2, 3, 4, 5], from: subject) {
subject.send(4)
subject.send(5)
}
Expand All @@ -61,8 +61,8 @@ final class ReplaySubjectTests: XCTestCase {
subject.send(1)
subject.send(2)
subject.send(3)
expectEqualPublished(values: [2, 3], from: subject, during: .milliseconds(100))
expectEqualPublished(values: [2, 3], from: subject, during: .milliseconds(100))
expectAtLeastEqualPublished(values: [2, 3], from: subject)
expectAtLeastEqualPublished(values: [2, 3], from: subject)
}

func testSubscriptionRelease() {
Expand All @@ -79,10 +79,10 @@ final class ReplaySubjectTests: XCTestCase {
subject.send(1)
subject.send(2)
subject.send(3)
expectEqualPublished(values: [2, 3, 4], from: subject, during: .milliseconds(100)) {
expectAtLeastEqualPublished(values: [2, 3, 4], from: subject) {
subject.send(4)
}
expectEqualPublished(values: [3, 4], from: subject, during: .milliseconds(100))
expectAtLeastEqualPublished(values: [3, 4], from: subject)
}

func testCompletion() {
Expand All @@ -98,7 +98,7 @@ final class ReplaySubjectTests: XCTestCase {
subject.send(1)
subject.send(completion: .finished)
subject.send(2)
expectEqualPublished(values: [1], from: subject, during: .milliseconds(100))
expectAtLeastEqualPublished(values: [1], from: subject)
}

func testCompletionWithMultipleSubscribers() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreTests/SlicePublisherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ final class SlicePublisherTests: XCTestCase {
Person(firstName: "Jane", lastName: "Smith"),
Person(firstName: "John", lastName: "Bridges")
].publisher.slice(at: \.firstName)
expectEqualPublished(values: ["Jane", "John"], from: publisher, during: .milliseconds(100))
expectAtLeastEqualPublished(values: ["Jane", "John"], from: publisher)
}
}
2 changes: 1 addition & 1 deletion Tests/CoreTests/TriggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class TriggerTests: XCTestCase {

func testHashableActivationIndex() {
let trigger = Trigger()
expectEqualPublished(values: ["out"], from: trigger.signal(activatedBy: "index").map { _ in "out" }, during: .seconds(1)) {
expectAtLeastEqualPublished(values: ["out"], from: trigger.signal(activatedBy: "index").map { _ in "out" }) {
trigger.activate(for: "index")
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreTests/WaitPublisherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class WaitPublisherTests: XCTestCase {
.wait(untilOutputFrom: signal)
expectNothingPublished(from: publisher, during: .milliseconds(100))

expectEqualPublished(values: ["Received"], from: publisher, during: .milliseconds(100)) {
expectAtLeastEqualPublished(values: ["Received"], from: publisher) {
signal.send(())
}
}
Expand Down
5 changes: 2 additions & 3 deletions Tests/PlayerTests/Asset/ResourceItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ final class ResourceItemTests: TestCase {
func testFailingPlayerItem() {
let item = Resource.failing(error: StructError()).playerItem(configuration: .default, limits: .none)
_ = AVPlayer(playerItem: item)
expectEqualPublished(
expectAtLeastEqualPublished(
values: [.unknown],
from: item.statusPublisher(),
during: .seconds(1)
from: item.statusPublisher()
)
}
}
5 changes: 2 additions & 3 deletions Tests/PlayerTests/Player/ErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ final class ErrorTests: TestCase {

func testInvalidStream() {
let player = Player(item: .simple(url: Stream.unavailable.url))
expectEqualPublishedNext(
expectAtLeastEqualPublishedNext(
values: [.init(rawValue: NSURLErrorFileDoesNotExist)],
from: Self.errorCodePublisher(for: player),
during: .seconds(1)
from: Self.errorCodePublisher(for: player)
)
}

Expand Down
5 changes: 2 additions & 3 deletions Tests/PlayerTests/Player/PlaybackTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ final class PlaybackTests: XCTestCase {
func testUnknown() {
let item = PlayerItem.simple(url: Stream.unavailable.url)
let player = Player(item: item)
expectEqualPublished(
expectAtLeastEqualPublished(
values: [.idle],
from: playbackStatePublisher(for: player),
during: .seconds(1)
from: playbackStatePublisher(for: player)
)
}
}
5 changes: 2 additions & 3 deletions Tests/PlayerTests/Player/PlayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ final class PlayerTests: TestCase {
func testRetrieveCurrentValueOnSubscription() {
let player = Player(item: .simple(url: Stream.onDemand.url))
expect(player.properties.isBuffering).toEventually(beFalse())
expectEqualPublished(
expectAtLeastEqualPublished(
values: [false],
from: player.propertiesPublisher.slice(at: \.isBuffering),
during: .seconds(1)
from: player.propertiesPublisher.slice(at: \.isBuffering)
)
}

Expand Down
Loading