58 lines
2.2 KiB
Swift
58 lines
2.2 KiB
Swift
// RTThreadIdentityTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Darwin
|
|
import Dispatch
|
|
@testable import LyrebirdSynth
|
|
|
|
/// Verifies that the pthread-TLS marker used by ``RTThreadIdentity`` is
|
|
/// thread-local: marking one thread does not bleed into another. The
|
|
/// DEBUG-only `_lyrebirdAssertNotOnRTThread` trap is not exercised here
|
|
/// (`preconditionFailure` aborts the test process); the contract that
|
|
/// underpins it is fully covered by these read/write tests.
|
|
@Suite("RTThreadIdentity")
|
|
struct RTThreadIdentityTests {
|
|
|
|
@Test func warmUpDoesNotMarkCallerAsRT() {
|
|
RTThreadIdentity.warmUp()
|
|
#expect(RTThreadIdentity.isCurrentThreadRT == false)
|
|
}
|
|
|
|
@Test func markingIsThreadLocalNotProcessGlobal() async {
|
|
// Pre-condition: this (test) thread is not marked.
|
|
#expect(RTThreadIdentity.isCurrentThreadRT == false)
|
|
|
|
// Mark a *different* pthread, confirm via that thread's own read.
|
|
let markerSawRT: Bool = await withCheckedContinuation { continuation in
|
|
let queue: DispatchQueue = DispatchQueue(label: "RTThreadIdentityTests.marker")
|
|
queue.async {
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
let observed: Bool = RTThreadIdentity.isCurrentThreadRT
|
|
continuation.resume(returning: observed)
|
|
}
|
|
}
|
|
#expect(markerSawRT == true)
|
|
|
|
// This (test) thread must still be unmarked — the other thread's
|
|
// pthread_setspecific must not leak into our TLS slot.
|
|
#expect(RTThreadIdentity.isCurrentThreadRT == false)
|
|
}
|
|
|
|
@Test func markIsIdempotent() async {
|
|
let observedMarked: Bool = await withCheckedContinuation { continuation in
|
|
let queue: DispatchQueue = DispatchQueue(label: "RTThreadIdentityTests.idempotent")
|
|
queue.async {
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
RTThreadIdentity.markCurrentThreadAsRT()
|
|
continuation.resume(returning: RTThreadIdentity.isCurrentThreadRT)
|
|
}
|
|
}
|
|
#expect(observedMarked == true)
|
|
}
|
|
}
|