Skip to content

Commit

Permalink
SampleBuilder: add memory leak test
Browse files Browse the repository at this point in the history
Test that the input RTP packets are unreferenced
after all samples are popped.
  • Loading branch information
at-wat committed May 31, 2024
1 parent 09461d5 commit a9850b0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/media/samplebuilder/samplebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package samplebuilder

import (
"fmt"
"runtime"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -529,6 +531,45 @@ func TestSampleBuilderData(t *testing.T) {
assert.Equal(t, j, 0x1FFFF)
}

func TestSampleBuilderPacketUnreference(t *testing.T) {
s := New(10, &fakeDepacketizer{
headChecker: true,
}, 1)

var refs int64
finalizer := func(*rtp.Packet) {
atomic.AddInt64(&refs, -1)
}

for i := 0; i < 0x20000; i++ {
atomic.AddInt64(&refs, 1)
p := rtp.Packet{
Header: rtp.Header{
SequenceNumber: uint16(i),
Timestamp: uint32(i + 42),
},
Payload: []byte{byte(i)},
}
runtime.SetFinalizer(&p, finalizer)
s.Push(&p)
for {
sample := s.Pop()
if sample == nil {
break
}
}
}

runtime.GC()
time.Sleep(10 * time.Millisecond)

remainedRefs := atomic.LoadInt64(&refs)
runtime.KeepAlive(s)

// only the last packet should be still referenced
assert.Equal(t, int64(1), remainedRefs)
}

func TestSampleBuilder_Flush(t *testing.T) {
s := New(50, &fakeDepacketizer{
headChecker: true,
Expand Down

0 comments on commit a9850b0

Please sign in to comment.