Skip to content

Commit

Permalink
Fix flaky tests
Browse files Browse the repository at this point in the history
The test is flaky because on subscription, it is done on main thread but
asynchronously. Thus, if we don't drain the main queue, we could miss
the first update (while always get the final deleted update). This will
cause our fulfill logic fail.

This PR does the wait for main queue to drain.

Now, running `bazel test src/tests:Tests --runs_per_test=5000` will pass
without any failures (about half-an-hour to run on Mac Mini 2020).
  • Loading branch information
liuliu committed Oct 5, 2021
1 parent 3666f4d commit 9f5491f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/tests/SubscribeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@ class SubscribeTests: XCTestCase {
]

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
func drainMainQueue() {
// Double dispatch to avoid nested main queue dispatch in previous blocks.
let mainQueueDrain = XCTestExpectation(description: "main")
DispatchQueue.main.async {
DispatchQueue.main.async {
mainQueueDrain.fulfill()
}
}
wait(for: [mainQueueDrain], timeout: 10.0)
}
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func testObjectPublisher() {
guard let dflat = dflat else { return }
Expand Down Expand Up @@ -432,6 +442,9 @@ class SubscribeTests: XCTestCase {
pubExpectation.fulfill()
}
}
// The subscription happens on main queue asynchronously. Drain it to avoid we skip
// directly to deletion.
drainMainQueue()
let firstExpectation = XCTestExpectation(description: "transcation done")
dflat.performChanges(
[MyGame.Sample.Monster.self],
Expand Down Expand Up @@ -494,6 +507,9 @@ class SubscribeTests: XCTestCase {
subExpectation.fulfill()
}
}
// The subscription happens on main queue asynchronously. Drain it to avoid we skip
// directly to deletion.
drainMainQueue()
// Add one.
let firstExpectation = XCTestExpectation(description: "transcation done")
dflat.performChanges(
Expand Down

0 comments on commit 9f5491f

Please sign in to comment.