From 416550fa959f8a73f8c1ceaec324ce5efc0fccea Mon Sep 17 00:00:00 2001 From: onyx-and-iris <75868496+onyx-and-iris@users.noreply.github.com> Date: Sat, 26 Feb 2022 03:07:29 +0000 Subject: [PATCH] add self.buff, runmany add self.buff and test for equality against private packet before returning as public packet. add runmany --- __main__.py | 5 +- tests/runmany.ps1 | 114 +++++++++++++++++++++++++++++++++++++++++++ vban_cmd/channel.py | 2 +- vban_cmd/vban_cmd.py | 19 ++++++-- 4 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 tests/runmany.ps1 diff --git a/__main__.py b/__main__.py index 7e89b95..dbbad0e 100644 --- a/__main__.py +++ b/__main__.py @@ -3,8 +3,9 @@ from time import sleep def main(): with vban_cmd.connect('potato', ip='ws.local') as vban: - for i in range(8): - print(vban.bus[i].levels.all) + for i in range(100): + print(vban.strip[5].A1) + print(vban.strip[5].A2) if __name__ == '__main__': main() diff --git a/tests/runmany.ps1 b/tests/runmany.ps1 new file mode 100644 index 0000000..47e2812 --- /dev/null +++ b/tests/runmany.ps1 @@ -0,0 +1,114 @@ +param( + [parameter(Mandatory=$false)] + [Int] $num = 1, + [switch]$cycle, + [switch]$Log + ) + +$global:failures = 0 + +Function RunTests { + param([int]$cycle_num) + if ($cycle_num) { + $num = $cycle_num + } + + $logfile = "nosetest.log" + $failures = $global:failures + $firstrun = $false + + if ($cycle_num -eq 20) { $firstrun = $true } + + (Get-Content '__init__.py') | ForEach-Object { + $m = [regex]::Match($_, '^(DELAY)\s=\s(\d.\d+)') + if ($m.captures.groups.count -gt 1) { + $delay = $m.captures.groups[2] + } + $m = [regex]::Match($_, '^(MAX_POLLS)\s=\s(\d+)') + if ($m.captures.groups.count -gt 1) { + $maxpolls = $m.captures.groups[2] + } + } + + 1..$num | ForEach-Object { ` + if ($Log) { "Running test $_ of ${num} runs" | Tee-Object -FilePath $logfile -Append } + else { Write-Host "Running test $_ of ${num} runs" } + + ForEach ($line in $(Invoke-Expression "nosetests --randomize -s tests 2>&1")) { + if ($Log) { + if ($line -NotMatch '^(System)') { + "${line}" | Tee-Object -FilePath $logfile -Append + } + } else { + if ($line -NotMatch '^(System)') { Write-Host "${line}" } + } + + $m = [regex]::Match($line, '^(FAILED)\s\([errors=\d+,\s]?failures=(\d+)\)') + if ($m.captures.groups.count -gt 1) { + $failures += $m.captures.groups[2].value + } + } + } + + if ($Log) { + $log_backup = LogRotate -logfile $logfile + $log_backupfile = Split-Path $log_backup -leaf + + $summary_file = "summary.log" + if ($firstrun -eq $true) { + "===========================================================`n" + ` + "NOTES:" | Tee-Object -FilePath $summary_file -Append + } + "===========================================================`n" + ` + "${num} test run with ${delay} delay and ${maxpolls} max_polls`n" + ` + "Total failures: ${failures}`n" + ` + "Logfile for this test run: ${log_backupfile}`n" + ` + "===========================================================" | ` + Tee-Object -FilePath $summary_file -Append + } else { + "===========================================================", + "${num} test run with ${delay} delay and ${maxpolls} max_polls", + "Total failures: ${failures}", + "===========================================================" | Write-Host + } + $global:failures = $failures +} + +Function LogRotate { + param([string]$logfile) + Get-ChildItem ./ -recurse ` + | Where-Object {$_.basename -ne 'summary' -and $_.extension -eq ".log" } ` + | ForEach-Object { + $i = 1 + $StopLoop = $false + + do { + try { + $savefile = "$($_.Fullname)_$i.backup" + Rename-Item -Path $_.FullName ` + -NewName $savefile -ErrorAction "Stop" + + $StopLoop = $true + } + catch { + Start-Sleep -m 100 + $i++ + } + } until ($StopLoop -eq $true) + } + $savefile +} + +if ($MyInvocation.InvocationName -ne ".") +{ + & '..\venv_vban_cmd\Scripts\activate.ps1' + + if ($cycle) { + @(20, 50, 100, 200, 500, 1000) | ForEach-Object { + RunTests -cycle_num $_ + if ($global:failures -gt 0) { break } + } + } else { RunTests } + + & 'deactivate' +} diff --git a/vban_cmd/channel.py b/vban_cmd/channel.py index 64d79a5..97dd3ea 100644 --- a/vban_cmd/channel.py +++ b/vban_cmd/channel.py @@ -62,7 +62,7 @@ class Channel(abc.ABC): def getter(self): """ Returns an RT data packet. """ - return self._remote.get_rt() + return self._remote.public_packet def setter(self, param, val): """ Sends a string request RT packet. """ self._remote.set_rt(f'{self.identifier}', param, val) diff --git a/vban_cmd/vban_cmd.py b/vban_cmd/vban_cmd.py index 72c7e85..0d6f6cc 100644 --- a/vban_cmd/vban_cmd.py +++ b/vban_cmd/vban_cmd.py @@ -35,6 +35,7 @@ class VbanCmd(abc.ABC): self._text_header = TextRequestHeader() self._register_rt_header = RegisterRTHeader() self.expected_packet = VBAN_VMRT_Packet_Header() + self.buff = None self._rt_register_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._rt_packet_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -94,15 +95,21 @@ class VbanCmd(abc.ABC): ) return False + @property + def public_packet(self): + self.buff = self.get_rt() + return self.buff + def get_rt(self): def fget(): data = False while not data: data = self._fetch_rt_packet() return data - for i in range(self._max_polls): - data = fget() - return data + private_packet = fget() + if private_packet.__eq__(self.buff): + private_packet = fget() + return private_packet def set_rt(self, id_, param, val): cmd = f'{id_}.{param}={val}' @@ -137,10 +144,14 @@ class VbanCmd(abc.ABC): """ Restarts Voicemeeter's audio engine. """ self.set_rt('Command', 'Restart', 1) - def __exit__(self, exc_type, exc_value, exc_traceback): + def close(self): + sleep(self._delay) self._rt_packet_socket.close() sys.exit() + def __exit__(self, exc_type, exc_value, exc_traceback): + self.close() + def _make_remote(kind: NamedTuple) -> VbanCmd: """