mirror of
https://github.com/pion/webrtc.git
synced 2025-09-26 19:21:12 +08:00
SampleBuilder: add memory leak test
Test that the input RTP packets are unreferenced after all samples are popped.
This commit is contained in:

committed by
Eric Daniels

parent
d9994bdf6c
commit
c4d56d4759
@@ -5,6 +5,8 @@ package samplebuilder
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -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,
|
||||
|
Reference in New Issue
Block a user