// ALSARecoveryActionTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import LyrebirdSynth // The xrun policy decides whether the render loop recovers, retries, or dies. // It is pure arithmetic over ALSA's return value, so it is tested on every // platform even though the driver consuming it is Linux-only. @Suite("ALSA transfer recovery policy") struct ALSARecoveryActionTests { @Test func successReportsFramesTransferred() { #expect(ALSARecoveryAction.classify(512) == .transferred(frames: 512)) } @Test func shortTransferIsStillSuccess() { // A partial write is normal; the loop resumes from where it stopped. #expect(ALSARecoveryAction.classify(37) == .transferred(frames: 37)) } @Test func zeroFramesIsSuccessNotAnError() { #expect(ALSARecoveryAction.classify(0) == .transferred(frames: 0)) } @Test func signalInterruptionIsDistinctFromDeviceNotReady() { // Kept apart on purpose: a signal is not a device fault, so it reissues // immediately and must not spend the patience budget that a device // which never becomes ready does. #expect(ALSARecoveryAction.classify(-4) == .interrupted) // -EINTR #expect(ALSARecoveryAction.classify(-11) == .wouldBlock) // -EAGAIN } @Test func underrunAndSuspendRecover() { #expect(ALSARecoveryAction.classify(-32) == .recover) // -EPIPE (xrun) #expect(ALSARecoveryAction.classify(-86) == .recover) // -ESTRPIPE (suspended) } @Test func unknownErrorIsFatal() { #expect(ALSARecoveryAction.classify(-19) == .fail) // -ENODEV, device unplugged #expect(ALSARecoveryAction.classify(-77) == .fail) // -EBADFD } }