Fix concurrent pc.GracefulClose

This commit is contained in:
Eric Daniels
2024-08-26 07:38:32 -04:00
parent 64a837f688
commit 9a61d68237
2 changed files with 113 additions and 35 deletions

View File

@@ -7,6 +7,8 @@
package webrtc
import (
"fmt"
"sync"
"testing"
"time"
@@ -180,7 +182,7 @@ func TestPeerConnection_Close_DuringICE(t *testing.T) {
}
}
func TestPeerConnection_CloseWithIncomingMessages(t *testing.T) {
func TestPeerConnection_GracefulCloseWithIncomingMessages(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 20)
defer lim.Stop()
@@ -287,3 +289,41 @@ func TestPeerConnection_GracefulCloseWhileOpening(t *testing.T) {
t.Fatal(err)
}
}
func TestPeerConnection_GracefulCloseConcurrent(t *testing.T) {
// Limit runtime in case of deadlocks
lim := test.TimeOut(time.Second * 10)
defer lim.Stop()
for _, mixed := range []bool{false, true} {
t.Run(fmt.Sprintf("mixed_graceful=%t", mixed), func(t *testing.T) {
report := test.CheckRoutinesStrict(t)
defer report()
pc, err := NewPeerConnection(Configuration{})
if err != nil {
t.Fatal(err)
}
const gracefulCloseConcurrency = 50
var wg sync.WaitGroup
wg.Add(gracefulCloseConcurrency)
for i := 0; i < gracefulCloseConcurrency; i++ {
go func() {
defer wg.Done()
assert.NoError(t, pc.GracefulClose())
}()
}
if !mixed {
if err := pc.Close(); err != nil {
t.Fatal(err)
}
} else {
if err := pc.GracefulClose(); err != nil {
t.Fatal(err)
}
}
wg.Wait()
})
}
}