import CoreFoundation import Darwin import Foundation import Synchronization import Testing @testable import ParcelPlatform struct AppThreadOperationValidityTests { @Test func queuedRestoreRejectsReusedIDOnOwnerThreadBeforeLookup() async throws { let validity = AppThreadOperationValidity() let registry = Mutex(1) let mutations = Mutex<[Int]>([]) let (started, start) = AsyncStream.makeStream() let (completed, complete) = AsyncStream.makeStream() let gate = DispatchSemaphore(value: 0) let executor = AppThreadExecutor(start: {}, stop: {}) defer { gate.signal() executor.shutdown() } executor.submit( run: { start.yield(()) gate.wait() }, cancel: { start.finish() }) var starts = started.makeAsyncIterator() _ = try #require(await starts.next()) let caller = pthread_mach_thread_np(pthread_self()) executor.submit( run: { #expect(pthread_mach_thread_np(pthread_self()) != caller) #expect(throws: AppThreadOperationInvalidated()) { try validity.perform( resolve: { Issue.record("A stale operation must not resolve its recycled window ID") return registry.withLock { $0 } }, operation: { window in mutations.withLock { $0.append(window) } }) } complete.yield(true) }, cancel: { complete.yield(false) }) registry.withLock { $0 = 2 } validity.invalidate() gate.signal() var results = completed.makeAsyncIterator() #expect(await results.next() == true) #expect(mutations.withLock { $0.isEmpty }) } @Test func invalidationDuringLookupPreventsMutation() { let validity = AppThreadOperationValidity() var mutations = 0 #expect(throws: AppThreadOperationInvalidated()) { try validity.perform( resolve: { validity.invalidate() return 2 }, operation: { _ in mutations += 1 }) } #expect(mutations == 0) } @Test func expiredQueuedJobReturnsWithoutTheBlockedOwnerAndNeverMutates() async throws { let (started, start) = AsyncStream.makeStream() let gate = DispatchSemaphore(value: 0) let mutations = Mutex(0) let aborted = Mutex(false) let executor = AppThreadExecutor(start: {}, stop: {}) defer { gate.signal() executor.shutdown() } executor.submit( run: { start.yield(()); gate.wait() }, cancel: { start.finish() }) var starts = started.makeAsyncIterator() _ = try #require(await starts.next()) await #expect(throws: AXPlatformError.timedOut) { try await executor.performBounded(timeout: 0.01, onAbort: { aborted.withLock { $0 = true } }) { mutations.withLock { $0 += 1 } } } #expect(aborted.withLock { $0 }) gate.signal() _ = try await executor.performBounded(timeout: 5) { 1 } #expect(mutations.withLock { $0 } == 0) } @Test func boundedInFlightJobReturnsBeforeANoncooperativeOperationAndIgnoresLateSuccess() async throws { let (started, start) = AsyncStream.makeStream() let gate = DispatchSemaphore(value: 0) let aborted = Mutex(false) let executor = AppThreadExecutor(start: {}, stop: {}) defer { gate.signal() executor.shutdown() } let job = AppThreadBoundedJob(onAbort: { aborted.withLock { $0 = true } }) executor.submit( run: { start.yield(()); gate.wait(); job.finish(.success(42)) }, cancel: { start.finish(); job.finish(.failure(AppThreadFailure.stopped), abort: true) }) var starts = started.makeAsyncIterator() _ = try #require(await starts.next()) await #expect(throws: AXPlatformError.timedOut) { try await job.wait(timeout: 0.01) } #expect(aborted.withLock { $0 }) gate.signal() #expect(try await executor.performBounded(timeout: 5) { 7 } == 7) } @Test func cancelledBoundedJobAbortsOnlyOnceAndCannotBeResumedByLateCompletion() async { let aborts = Mutex(0) let job = AppThreadBoundedJob(onAbort: { aborts.withLock { $0 += 1 } }) let waiter = Task { try await job.wait(timeout: 5) } waiter.cancel() await #expect(throws: CancellationError.self) { try await waiter.value } job.finish(.success(42)) job.finish(.failure(AXPlatformError.timedOut), abort: true) #expect(aborts.withLock { $0 } == 1) #expect(!job.isPending) } @Test func currentGenerationExecutesAndInvalidationIsPermanent() throws { let validity = AppThreadOperationValidity() #expect(try validity.perform(resolve: { 1 }, operation: { $0 + 1 }) == 2) validity.invalidate() validity.invalidate() #expect(throws: AppThreadOperationInvalidated()) { try validity.check() } #expect(try AppThreadOperationValidity().perform(resolve: { 2 }, operation: { $0 + 1 }) == 3) } }