FreeDATA/test/test_audiobuffer.py
Paul Kronenwetter 5c6cee1c21
Enhance tests (#185)
* Initial attempt to create unit tests for DATA class (tnc).

* Completed initial set of tests.

* Adding pytest to install packages.

* Corrects issue #173 fix

I didn't look carefully enough at `helpers.check_callsign` to see that it returns a list. The first element of the list is `True`/`False`.

* Making check_callsign evaluation more consistent.

* Update .gitignore

this is more a test commit to see if GitHub Client for MacOS is working,

* mkfifo test prototype

First partially working prototype for testing the full tnc with mkfifo named pipes.

* single tnc test file

moved to a single file for running tnc tests

* fixed typo

* Added parameters to tests.

Make other minor tweaks and documentation.

* Clean up two existing tests.

Adapted both tests to pytest and maintained
compatibility with existing ctest method.
Tweaked CMakeLists.txt .

* Adding pure python  highsnr_stdio_P_P_multi test.

Intended to replace highsnr_stdio_P_P_multi which uses POSIX shell.

* Adding pure python  highsnr_stdio_P_P_datac0 test.

Intended to replace highsnr_stdio_P_P_datac0 which uses POSIX shell.

* Parameterize recent tests.

Renamed datac0 to datacx after including all data codecs in test.

* Parameterize mode as well.

Add ability to run tests from main directory as well as within test/.

* Add list of tests and brief descriptions.

* Add more native python tests conversions.

* Update README with new tests.

* Tweak README again.

* Rename test to be findable by pytest.

* Rename test for ctest.

* Update correct file this time.

* Minor test tweaks.

* Add modem test proof-of-concept.

* Adjustment to ARQ short test.

* Various refactorings.

Type hints, trailing backslash, range usage, etc.

* Ignore unknown arguments in argparse.

* Minor cleanups.

* Update test/README.md.

* Update test_pa to quiet pylint.

* Give up trying to suppress structlog output.

* Correct module comments.

* Remove excess trailing spaces.

* Remove excess newlines.

* Various refactorings.

Type hints, trailing backslash, range usage, etc.

* mkfifo test prototype

First partially working prototype for testing the full tnc with mkfifo named pipes.

* Update test_tnc and tweak IRS/ISS.

* Correct test_modem to detect failures.

* Trying to be less dependent on env variables.

* Add IRS/ISS tests to ctests

* Pin codec2 revision to v1.0.3.

* Correcting git mistake.

* Pin codec2 revision to master.

This should be a specific release, that implements freedv_set_tuning_range.

Co-authored-by: DJ2LS <75909252+DJ2LS@users.noreply.github.com>
2022-05-21 23:04:17 +00:00

88 lines
2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# tests audio buffer thread safety
# pylint: disable=global-statement, invalid-name
import sys
import threading
from time import sleep
import codec2
import numpy as np
import pytest
BUFFER_SZ = 1024
N_MAX = 100 # write a repeating sequence of 0..N_MAX-1
WRITE_SZ = 10 # different read and write sized buffers
READ_SZ = 8
NTESTS = 10000
running = True
audio_buffer = codec2.audio_buffer(BUFFER_SZ)
n_write = 0
def t_writer():
"""
Subprocess to handle writes to the NumPY audio "device."
"""
global n_write
print("writer starting")
n = 0
buf = np.zeros(WRITE_SZ, dtype=np.int16)
while running:
nfree = audio_buffer.size - audio_buffer.nbuffer
if nfree >= WRITE_SZ:
for index in range(WRITE_SZ):
buf[index] = n
n += 1
if n == N_MAX:
n = 0
n_write += 1
audio_buffer.push(buf)
def test_audiobuffer():
"""
Test for the audiobuffer
"""
global running
# Start the writer in a new thread.
writer_thread = threading.Thread(target=t_writer)
writer_thread.start()
n_out = n_read = errors = 0
for _ in range(NTESTS):
while audio_buffer.nbuffer < READ_SZ:
sleep(0.001)
for i in range(READ_SZ):
if audio_buffer.buffer[i] != n_out:
errors += 1
n_out += 1
if n_out == N_MAX:
n_out = 0
n_read += 1
audio_buffer.pop(READ_SZ)
print(f"n_write: {n_write} n_read: {n_read} errors: {errors} ")
# Indirectly stop the thread
running = False
sleep(0.1)
assert not writer_thread.is_alive()
assert n_write - n_read < BUFFER_SZ
assert errors == 0
if __name__ == "__main__":
# Run pytest with the current script as the filename.
ecode = pytest.main(["-v", sys.argv[0]])
if ecode == 0:
print("errors: 0")
else:
print(ecode)