|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +@testable import AsyncMonitor |
| 4 | + |
| 5 | +@MainActor class AsyncMonitorTests { |
| 6 | + let center = NotificationCenter() |
| 7 | + let name = Notification.Name("a random notification") |
| 8 | + |
| 9 | + private var subject: AsyncMonitor? |
| 10 | + |
| 11 | + @Test func callsBlockWhenNotificationsArePosted() async throws { |
| 12 | + await withCheckedContinuation { [center, name] continuation in |
| 13 | + subject = center.notifications(named: name).map(\.name).monitor { receivedName in |
| 14 | + #expect(name == receivedName) |
| 15 | + continuation.resume() |
| 16 | + } |
| 17 | + Task { |
| 18 | + center.post(name: name, object: nil) |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + @Test func doesNotCallBlockWhenOtherNotificationsArePosted() async throws { |
| 24 | + subject = center.notifications(named: name).map(\.name).monitor { receivedName in |
| 25 | + Issue.record("Called for irrelevant notification \(receivedName)") |
| 26 | + } |
| 27 | + Task { |
| 28 | + center.post(name: Notification.Name("something else"), object: nil) |
| 29 | + } |
| 30 | + try await Task.sleep(for: .milliseconds(10)) |
| 31 | + } |
| 32 | + |
| 33 | + @Test func stopsCallingBlockWhenDeallocated() async throws { |
| 34 | + subject = center.notifications(named: name).map(\.name).monitor { _ in |
| 35 | + Issue.record("Called after deallocation") |
| 36 | + } |
| 37 | + |
| 38 | + Task { |
| 39 | + subject = nil |
| 40 | + center.post(name: name, object: nil) |
| 41 | + } |
| 42 | + |
| 43 | + try await Task.sleep(for: .milliseconds(10)) |
| 44 | + } |
| 45 | + |
| 46 | + class Owner { |
| 47 | + let deinitHook: () -> Void |
| 48 | + |
| 49 | + private var cancellable: (any AsyncCancellable)? |
| 50 | + |
| 51 | + @MainActor init(center: NotificationCenter, deinitHook: @escaping () -> Void) { |
| 52 | + self.deinitHook = deinitHook |
| 53 | + let name = Notification.Name("irrelevant name") |
| 54 | + cancellable = center.notifications(named: name).map(\.name) |
| 55 | + .monitor(context: self) { _, _ in } |
| 56 | + } |
| 57 | + |
| 58 | + deinit { |
| 59 | + deinitHook() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private var owner: Owner? |
| 64 | + |
| 65 | + @Test(.timeLimit(.minutes(1))) func doesNotCreateReferenceCyclesWithContext() async throws { |
| 66 | + await withCheckedContinuation { continuation in |
| 67 | + self.owner = Owner(center: center) { |
| 68 | + continuation.resume() |
| 69 | + } |
| 70 | + self.owner = nil |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test func stopsCallingBlockWhenContextIsDeallocated() async throws { |
| 75 | + var context: NSObject? = NSObject() |
| 76 | + subject = center.notifications(named: name).map(\.name) |
| 77 | + .monitor(context: context!) { context, receivedName in |
| 78 | + Issue.record("Called after context was deallocated") |
| 79 | + } |
| 80 | + context = nil |
| 81 | + Task { |
| 82 | + center.post(name: name, object: nil) |
| 83 | + } |
| 84 | + try await Task.sleep(for: .milliseconds(10)) |
| 85 | + } |
| 86 | +} |
0 commit comments