Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@
pkgs.alsa-plugins
# A private clocked null device for native playback tests.
(pkgs.lib.getBin pkgs.pulseaudio)
# A shared session bus for the concurrent private audio server test.
pkgs.dbus
# moq-video's `pipewire` screen-capture feature: the pipewire crate
# links libpipewire-0.3 via pkg-config and generates bindings at build
# time (bindgenHook above provides libclang). Linux-only; macOS uses
Expand Down
1 change: 0 additions & 1 deletion quest/m0/test-flakes-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,5 @@ Public API: none. Wire: none.
- [moq-ffi's panicked-origin supervision test survives load](/quest/m0/test-flakes-2/ffi-origin-panic.md) - a read the origin driver's panic ends always reports `MoqError::Task`
- [The scheduled macOS WebSocket version test completes reliably](/quest/m0/test-flakes-2/macos-websocket.md) - fix the scheduled timeout while preserving newest-version negotiation assertions
- [@moq/net's lite subscribe tests wait for the whole request](/quest/m0/test-flakes-2/js-subscribe-fixture.md) - the three lite subscribe-with-track cases pass under forced yields
- [Concurrent PulseAudio harness runs don't share a D-Bus name](/quest/m0/test-flakes-2/pulse-dbus.md) - parallel media runs no longer collide on `org.PulseAudio1`
- [@moq/net's uni-stream flood test survives reruns](/quest/m0/test-flakes-2/js-stream-flood.md) - "a flood of uni streams yields" passes under `--rerun-each` and load
- [The JS receive-credit qmux test never completes an oversized frame](/quest/m0/test-flakes-2/receive-credit-qmux.md) - `javascript_partial_frame_gives_way_qmux` stops completing a frame larger than the window on moq-transport-18
27 changes: 0 additions & 27 deletions quest/m0/test-flakes-2/pulse-dbus.md

This file was deleted.

1 change: 1 addition & 0 deletions test/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ default:
# from plain `bun test` so unit tests do not launch a browser.
harness:
./lib/harness.test.sh
python3 lib/pulse.test.py
./interop/relay-log.test.sh
python3 interop/staging.test.py
python3 ../rs/moq-gst/smoke.test.py
Expand Down
7 changes: 6 additions & 1 deletion test/lib/pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def run():
"DISPLAY": "",
"WAYLAND_DISPLAY": "",
}
# The daemon claims org.PulseAudio1 even without a D-Bus module. With no
# session address (and no DISPLAY to autolaunch one), it uses only our socket.
# Keep the wrapped command's session bus available for its other services.
daemon_env = env.copy()
daemon_env.pop("DBUS_SESSION_BUS_ADDRESS", None)

# By default the null sink renders 2 s ahead and sleeps through it, and a new stream does not
# rewind that, so a stream's first callback waits up to 2 s: Firefox reports its AudioContext
Expand All @@ -68,7 +73,7 @@ def run():
f"--load=module-native-protocol-unix socket={socket} auth-anonymous=1",
"--load=module-null-sink sink_name=moq-null rate=48000 channels=2 norewinds=1",
],
env=env,
env=daemon_env,
stderr=subprocess.PIPE,
)
try:
Expand Down
132 changes: 132 additions & 0 deletions test/lib/pulse.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
"""Private audio servers start together and play on their own sinks under one session bus."""

import array
import json
import math
import os
import signal
import string
import subprocess
import sys
import tempfile
import unittest
import wave
from contextlib import ExitStack
from pathlib import Path

HERE = Path(__file__).resolve().parent
# Bytes a D-Bus address value may carry unescaped; the rest are %-encoded, including XML's `&` and `<`.
DBUS_SAFE = frozenset((string.ascii_letters + string.digits + "-_/.\\*").encode())


def dbus_unix_path(path):
return "unix:path=" + "".join(chr(b) if b in DBUS_SAFE else f"%{b:02x}" for b in os.fsencode(path))


def play(tone):
assert os.environ["DBUS_SESSION_BUS_ADDRESS"]
info = json.loads(subprocess.check_output(["pactl", "--format=json", "info"]))
assert info["server_string"] == os.environ["PULSE_SERVER"]
assert info["default_sink_name"] == "moq-null"
# Hold both daemons alive before either playback can finish and release its D-Bus name.
print("ready", flush=True)
assert sys.stdin.readline() == "play\n"
subprocess.run(["paplay", "--device=moq-null", str(tone)], check=True)


def parallel(root):
with ExitStack() as children:
runs = [
children.enter_context(
subprocess.Popen(
[
sys.executable,
str(HERE / "pulse.py"),
"--dir",
str(root / name),
"--",
sys.executable,
str(Path(__file__).resolve()),
"--play",
str(root / "tone.wav"),
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
)
for name in ("one", "two")
]
for child in runs:
line = child.stdout.readline()
if line != b"ready\n":
# Surface why, such as a startup rejection or a failed pactl check, not only that it failed.
output, _ = child.communicate()
raise AssertionError("private server exited before playback:\n" + (line + output).decode())
for child in runs:
child.stdin.write(b"play\n")
child.stdin.flush()
for child in runs:
output, _ = child.communicate()
assert child.returncode == 0, output.decode()


@unittest.skipUnless(sys.platform == "linux", "private PulseAudio is Linux-only")
class Pulse(unittest.TestCase):
def test_parallel_playback(self):
# The system temp dir keeps both Unix sockets well inside sockaddr_un's 108 bytes, however deep the checkout.
with tempfile.TemporaryDirectory(prefix="pulse-") as directory:
root = Path(directory)
bus_config = root / "bus.conf"
bus_config.write_text(
f"<busconfig><type>session</type><listen>{dbus_unix_path(root / 'b')}</listen><auth>EXTERNAL</auth>"
'<policy context="default"><allow own="*"/><allow send_destination="*"/>'
'<allow receive_sender="*"/></policy></busconfig>'
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
with wave.open(str(root / "tone.wav"), "wb") as tone:
tone.setparams((1, 2, 48000, 0, "NONE", "not compressed"))
samples = array.array("h", (int(8000 * math.sin(2 * math.pi * 440 * i / 48000)) for i in range(12000)))
if sys.byteorder != "little":
samples.byteswap()
tone.writeframes(samples.tobytes())
# The shared bus is private to this test, never the desktop's session bus.
with subprocess.Popen(
[
"dbus-run-session",
f"--config-file={bus_config}",
"--",
sys.executable,
str(Path(__file__).resolve()),
"--parallel",
str(root),
],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
start_new_session=True,
) as session:
timed_out = False
try:
# Each pulse.py may spend 30 s on startup alone; leave room for playback after it.
session.communicate(timeout=60)
except subprocess.TimeoutExpired:
timed_out = True
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
finally:
# Reap the whole test tree even if startup or a playback client hangs.
try:
os.killpg(session.pid, signal.SIGKILL)
except ProcessLookupError:
pass
output, _ = session.communicate()
logs = "\n".join(f"{log}:\n{log.read_text()}" for log in root.glob("*/pulse.log"))
diagnostics = output.decode() + logs
self.assertFalse(timed_out, "private audio test timed out\n" + diagnostics)
self.assertEqual(session.returncode, 0, diagnostics)


if __name__ == "__main__":
if sys.argv[1:2] == ["--play"]:
play(Path(sys.argv[2]))
elif sys.argv[1:2] == ["--parallel"]:
parallel(Path(sys.argv[2]))
else:
unittest.main()
Loading