111 lines
6.1 KiB
Swift
111 lines
6.1 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import AVFoundation
|
|
import Foundation
|
|
import XCTest
|
|
|
|
final class CoCoVideoBudgetTests: XCTestCase {
|
|
private func directory() throws -> URL {
|
|
let root = FileManager.default.temporaryDirectory.appendingPathComponent("CoCoVideoBudget-" + UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: false)
|
|
addTeardownBlock { try? FileManager.default.removeItem(at: root) }
|
|
return root
|
|
}
|
|
private func frames(count: Int, audioFrames: Int = 735) -> [CoCoVideoFrame] {
|
|
let pixels = Data(repeating: 0x40, count: 640 * 480 * 4)
|
|
return (0..<count).map { n in
|
|
CoCoVideoFrame(pixels: pixels, area: CGRect(x:64,y:48,width:512,height:384),
|
|
audio: Data(count: audioFrames * 8), sampleOffset: Int64(n * audioFrames))
|
|
}
|
|
}
|
|
|
|
func testOversizedIngressFailsOnceAndEveryFinishCallbackCompletesOnce() async throws {
|
|
let root = try directory()
|
|
let encoder = try CoCoVideoEncoder(workingURL: root.appendingPathComponent("Working.mov"))
|
|
let failure = expectation(description: "bounded ingress reports one failure")
|
|
failure.assertForOverFulfill = true
|
|
encoder.failure = { message in
|
|
XCTAssertTrue(message.localizedCaseInsensitiveContains("bounded"))
|
|
failure.fulfill()
|
|
}
|
|
// A valid three-second stereo batch exceeds the recorder's public
|
|
// two-second retention contract before it reaches the encoder queue.
|
|
let oversized = frames(count: 1, audioFrames: 3 * 44_100)
|
|
for _ in 0..<100 { encoder.append(oversized) }
|
|
let first = expectation(description: "first finish returns failure")
|
|
let second = expectation(description: "repeat finish returns failure")
|
|
first.assertForOverFulfill = true; second.assertForOverFulfill = true
|
|
encoder.finish { result in
|
|
if case .success = result { XCTFail("Over-budget input must not claim a completed movie") }
|
|
XCTAssertTrue(encoder.writerIsTerminal, "Failure completion must await AVFoundation writer closure")
|
|
first.fulfill()
|
|
}
|
|
encoder.finish { result in
|
|
if case .success = result { XCTFail("Repeated Stop must not manufacture a successful movie") }
|
|
XCTAssertTrue(encoder.writerIsTerminal, "Duplicate Stop must share the same writer-closure barrier")
|
|
second.fulfill()
|
|
}
|
|
await fulfillment(of: [failure, first, second], timeout: 8)
|
|
}
|
|
|
|
func testRapidValidFrameProducerIsRejectedWithoutUnboundedQueueing() async throws {
|
|
let root = try directory()
|
|
let encoder = try CoCoVideoEncoder(workingURL: root.appendingPathComponent("Working.mov"))
|
|
let failure = expectation(description: "unpaced producer reaches bounded ingress")
|
|
failure.assertForOverFulfill = true
|
|
encoder.failure = { message in
|
|
XCTAssertTrue(message.localizedCaseInsensitiveContains("bounded"))
|
|
failure.fulfill()
|
|
}
|
|
// This is deliberately not real-time pacing: a producer must not hide
|
|
// thousands of otherwise valid frames inside pending dispatch blocks.
|
|
let pixels = Data(repeating:0x60,count:640 * 480 * 4)
|
|
let audio = Data(count:735 * 8)
|
|
for n in 0..<2_000 {
|
|
encoder.append([CoCoVideoFrame(pixels:pixels,area:CGRect(x:64,y:48,width:512,height:384),audio:audio,sampleOffset:Int64(n * 735))])
|
|
}
|
|
let stopped = expectation(description: "stop finishes after input rejection")
|
|
stopped.assertForOverFulfill = true
|
|
encoder.finish { result in
|
|
if case .success = result { XCTFail("Dropped over-budget input cannot be reported as a complete recording") }
|
|
stopped.fulfill()
|
|
}
|
|
await fulfillment(of:[failure,stopped],timeout:8)
|
|
}
|
|
|
|
func testOrderingFailureRetainsPrecedingMovieBytesAndNeverClaimsSuccessfulCompletion() async throws {
|
|
let root = try directory(), working = root.appendingPathComponent("Working.mov")
|
|
let neighbor = root.appendingPathComponent("Existing.mp4"), original = Data("unrelated original".utf8)
|
|
try original.write(to: neighbor)
|
|
let encoder = try CoCoVideoEncoder(workingURL: working)
|
|
let failure = expectation(description: "ordering failure is reported once")
|
|
failure.assertForOverFulfill = true
|
|
encoder.failure = { message in
|
|
XCTAssertTrue(message.contains("ordering"))
|
|
failure.fulfill()
|
|
}
|
|
// First establish a real encoded movie, rather than asserting that an
|
|
// empty writer created a pathname. The next packet violates ordering.
|
|
for frame in frames(count: 20) {
|
|
encoder.append([frame])
|
|
try await Task.sleep(for: .milliseconds(17))
|
|
}
|
|
var invalid = frames(count: 1)[0]
|
|
invalid.sampleOffset = -1
|
|
encoder.append([invalid])
|
|
await fulfillment(of: [failure], timeout: 5)
|
|
let stopped: Result<URL, Error> = await withCheckedContinuation { c in encoder.finish { c.resume(returning: $0) } }
|
|
if case .success = stopped { XCTFail("Writer failure must not be advertised as a completed movie") }
|
|
XCTAssertTrue(encoder.writerIsTerminal)
|
|
let drained: Result<Void, Error> = await withCheckedContinuation { c in encoder.whenDrained { c.resume(returning: $0) } }
|
|
if case .success = drained { XCTFail("Failed input cannot be acknowledged as a successful drain") }
|
|
// The failure notification can precede AVFoundation's closure, but the
|
|
// finish callback cannot. No polling/retry is permitted after finish.
|
|
let duration = try await AVURLAsset(url: working).load(.duration).seconds
|
|
XCTAssertGreaterThan(duration, 0.2, "A failed writer retains its preceding playable content")
|
|
XCTAssertGreaterThan(try Data(contentsOf: working).count, 0)
|
|
XCTAssertEqual(try Data(contentsOf: neighbor), original)
|
|
let contents = try FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: nil)
|
|
XCTAssertEqual(Set(contents.map(\.lastPathComponent)), ["Working.mov", "Existing.mp4"])
|
|
}
|
|
}
|