nativeaudio

audio playback for Go
Log | Files | Refs | README | LICENSE

commit 510674d8bb8e294f99a0f1e4550f67abeb78647a
parent b654445175fea5e719d2d00636bac17f70e1e82b
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date:   Fri,  3 May 2024 13:26:49 +0800

nativeaudio: test harness for memory leaks

This test verifies expectations about the heap after the decode is
performed.

Diffstat:
Minternal/test/audio_test.go | 44++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+), 0 deletions(-)

diff --git a/internal/test/audio_test.go b/internal/test/audio_test.go @@ -5,6 +5,9 @@ import ( _ "embed" "encoding/binary" "fmt" + "runtime" + "slices" + "strings" "testing" "git.sr.ht/~jackmordaunt/nativeaudio" @@ -83,6 +86,47 @@ func TestDecodeCorrupt(t *testing.T) { t.Logf("format: %+v", f) } +// TestMemoryLeak runs the decode several times, forces a GC and verifies +// that no data is left over from this package. +func TestMemoryLeak(t *testing.T) { + runtime.MemProfileRate = 1 + + for ii := 0; ii < 10; ii++ { + by, f, err := nativeaudio.Load("compressed.m4a") + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + _ = by + _ = f + _ = err + } + + runtime.GC() + runtime.GC() + + var profiles []runtime.MemProfileRecord + + for { + n, ok := runtime.MemProfile(profiles, false) + if ok { + profiles = profiles[:n] + break + } + profiles = slices.Grow(profiles, n)[:n] + } + + for _, p := range profiles { + f := runtime.FuncForPC(p.Stack0[0]) + + if !strings.Contains(f.Name(), "nativeaudio") { + continue + } + + t.Errorf("un-freed data: %s -> %d\n", f.Name(), p.InUseBytes()) + } + +} + // equal decodes the PCM samples and tests if they are "close enough" // using a heuristic tolerance. //