Skip to content

Commit 4c2969d

Browse files
committed
test: verify buffer operations are in-place (no reallocation)
Assert that recv() and overflow trim modify the same bytearray object rather than creating a new one via slice-copy.
1 parent 7262262 commit 4c2969d

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

tests/test_audio_stream_track.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,60 @@ async def _continuous_reader(self, track):
302302
frames_received += 1
303303
assert frame.samples == 960
304304

305+
@pytest.mark.asyncio
306+
async def test_recv_does_not_reallocate_buffer(self):
307+
"""Test that recv consumes data in-place without creating a new buffer object."""
308+
track = AudioStreamTrack(sample_rate=48000, channels=1, format="s16")
309+
310+
# Write 40ms of data (enough for 2 frames)
311+
samples = np.zeros(1920, dtype=np.int16)
312+
pcm = PcmData(
313+
samples=samples,
314+
sample_rate=48000,
315+
format=AudioFormat.S16,
316+
channels=1,
317+
)
318+
await track.write(pcm)
319+
320+
# Store reference to the buffer object
321+
buffer_id = id(track._buffer)
322+
323+
# Receive a frame (consumes 20ms from buffer)
324+
await track.recv()
325+
326+
assert id(track._buffer) == buffer_id, "recv should modify buffer in-place, not create a new one"
327+
assert len(track._buffer) == 960 * 2, "should have 20ms of data remaining (960 samples * 2 bytes)"
328+
329+
@pytest.mark.asyncio
330+
async def test_buffer_overflow_does_not_reallocate(self):
331+
"""Test that buffer overflow trims in-place without creating a new buffer object."""
332+
track = AudioStreamTrack(
333+
sample_rate=48000, channels=1, format="s16", audio_buffer_size_ms=100
334+
)
335+
336+
# Write 50ms of data first to get a buffer reference
337+
samples_50ms = np.zeros(2400, dtype=np.int16)
338+
pcm = PcmData(
339+
samples=samples_50ms,
340+
sample_rate=48000,
341+
format=AudioFormat.S16,
342+
channels=1,
343+
)
344+
await track.write(pcm)
345+
buffer_id = id(track._buffer)
346+
347+
# Write 200ms of data (exceeds 100ms limit, triggers overflow trim)
348+
samples_200ms = np.zeros(9600, dtype=np.int16)
349+
pcm_large = PcmData(
350+
samples=samples_200ms,
351+
sample_rate=48000,
352+
format=AudioFormat.S16,
353+
channels=1,
354+
)
355+
await track.write(pcm_large)
356+
357+
assert id(track._buffer) == buffer_id, "overflow trim should modify buffer in-place, not create a new one"
358+
305359
@pytest.mark.asyncio
306360
async def test_media_stream_error(self):
307361
"""Test that MediaStreamError is raised when track is not live."""

0 commit comments

Comments
 (0)