Last active
December 24, 2022 05:31
-
-
Save oozoofrog/64904c765b26b424e13b099c30c2c53c to your computer and use it in GitHub Desktop.
NotfiicationCenter+Concurrency test case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Notification.Name { | |
static let testNotification: Notification.Name = .init("TestNotification") | |
} | |
final class NotificationConcurrencyTests: XCTestCase { | |
func testWaitNotification() async throws { | |
let expect = XCTestExpectation(description: "DisposableNotification") | |
let queue = DispatchQueue(label: "test", attributes: .concurrent) | |
for _ in 0...10 { | |
queue.async { | |
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now() + 1, execute: { | |
NotificationCenter.default.post(name: .testNotification, object: nil) | |
}) | |
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 1, execute: { | |
NotificationCenter.default.post(name: .testNotification, object: nil) | |
}) | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: { | |
NotificationCenter.default.post(name: .testNotification, object: nil) | |
}) | |
} | |
} | |
DispatchQueue.global().asyncAfter(deadline: .now() + 2, execute: { | |
expect.fulfill() | |
}) | |
try await NotificationCenter.default.receiveFirst(forName: .testNotification) | |
let waiter = XCTWaiter() | |
let result = waiter.wait(for: [expect], timeout: 5) | |
XCTAssertEqual(result, .completed) | |
} | |
func testWaitNotificationCancel() async throws { | |
let task = Task { | |
try await NotificationCenter.default.receiveFirst(forName: .testNotification) | |
} | |
DispatchQueue.global().asyncAfter(deadline: .now() + 1) { | |
task.cancel() | |
} | |
do { | |
let _ = try await task.value | |
XCTFail() | |
} catch { | |
XCTAssertTrue(error is CancellationError, error.localizedDescription) | |
} | |
} | |
func testWaitNotificationTimeout() async throws { | |
do { | |
try await NotificationCenter.default.receiveFirst(forName: .testNotification, timeout: 2) | |
XCTFail() | |
} catch { | |
XCTAssertTrue(error is CancellationError, error.localizedDescription) | |
} | |
} | |
func testWaitNotificationTimeoutAfterCancel() async throws { | |
do { | |
let task = Task { | |
try await NotificationCenter.default.receiveFirst(forName: .testNotification, timeout: 2) | |
} | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) { | |
task.cancel() | |
} | |
try await task.value | |
XCTFail() | |
} catch { | |
XCTAssertTrue(error is CancellationError, error.localizedDescription) | |
} | |
try await Task.sleep(nanoseconds: 2_500_000_000) | |
} | |
func testWaitNotificationNotTimeout() async throws { | |
DispatchQueue.global().asyncAfter(deadline: .now() + 1) { | |
NotificationCenter.default.post(name: .testNotification, object: nil) | |
} | |
do { | |
try await NotificationCenter.default.receiveFirst(forName: .testNotification, timeout: 2) | |
} catch { | |
XCTFail(error.localizedDescription) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment