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

[PM-17710] Fix NotificationCenterServiceTests race condition failure #1313

Merged
merged 1 commit into from
Jan 30, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,70 @@ final class NotificationCenterServiceTests: BitwardenTestCase {

var notificationCenter: NotificationCenter!
var subject: DefaultNotificationCenterService!
var didEnterBackgroundTask: Task<Void, Never>!
var didEnterBackgroundPublished: Bool = false
var willEnterForegroundTask: Task<Void, Never>!
var willEnterBackgroundPublished: Bool = false

// MARK: Setup & Teardown

override func setUp() {
notificationCenter = NotificationCenter()
subject = DefaultNotificationCenterService(notificationCenter: notificationCenter)

didEnterBackgroundTask = Task {
for await _ in subject.didEnterBackgroundPublisher() {
didEnterBackgroundPublished = true
}
}

willEnterForegroundTask = Task {
for await _ in subject.willEnterForegroundPublisher() {
willEnterBackgroundPublished = true
}
}
}

override func tearDown() {
didEnterBackgroundPublished = false
didEnterBackgroundTask?.cancel()
didEnterBackgroundTask = nil

willEnterBackgroundPublished = false
willEnterForegroundTask?.cancel()
willEnterForegroundTask = nil

notificationCenter = nil
subject = nil
}

// MARK: Tests

/// `didEnterBackgroundPublisher` publishes a notification when the app enters the background.
func testDidEnterBackgroundPublisher() async throws {
var iterator = subject.didEnterBackgroundPublisher().makeAsyncIterator()
Task {
notificationCenter.post(
name: UIApplication.didEnterBackgroundNotification,
object: nil
)
func test_didEnterBackgroundPublisher() async throws {
try await waitForAsync { [weak self] in
let task = Task {
self?.notificationCenter.post(
name: UIApplication.didEnterBackgroundNotification,
object: nil
)
}
defer { task.cancel() }
return self?.didEnterBackgroundPublished == true
}
let result: Void? = await iterator.next()

XCTAssertNotNil(result)
}

/// `willEnterForegroundPublisher` publishes a notification when the app will enter the foreground.
func testWillEnterForegroundPublisher() async throws {
var iterator = subject.willEnterForegroundPublisher().makeAsyncIterator()
Task {
notificationCenter.post(
name: UIApplication.willEnterForegroundNotification,
object: nil
)
func test_willEnterForegroundPublisher() async throws {
try await waitForAsync { [weak self] in
let task = Task {
self?.notificationCenter.post(
name: UIApplication.willEnterForegroundNotification,
object: nil
)
}
defer { task.cancel() }
return self?.willEnterBackgroundPublished == true
}
let result: Void? = await iterator.next()

XCTAssertNotNil(result)
}
}