Skip to content

Commit

Permalink
Fix race condition in test
Browse files Browse the repository at this point in the history
In TestPeerConnection_Zero_PayloadType, WriteSample might be called
after closePairNow, resulting in failure. This patch fixes the issue.
  • Loading branch information
aler9 authored and Sean-Der committed Jun 16, 2024
1 parent 43a6a69 commit 4430f41
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions peerconnection_media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1751,26 +1751,28 @@ func TestPeerConnection_Zero_PayloadType(t *testing.T) {

assert.NoError(t, signalPair(pcOffer, pcAnswer))

onTrackFired, onTrackFiredCancel := context.WithCancel(context.Background())
trackFired := make(chan struct{})

pcAnswer.OnTrack(func(track *TrackRemote, _ *RTPReceiver) {
require.Equal(t, track.Codec().MimeType, MimeTypePCMU)
onTrackFiredCancel()
close(trackFired)
})

go func() {
func() {
ticker := time.NewTicker(20 * time.Millisecond)
defer ticker.Stop()

select {
case <-onTrackFired.Done():
return
case <-ticker.C:
if routineErr := audioTrack.WriteSample(media.Sample{Data: []byte{0x00}, Duration: time.Second}); routineErr != nil {
fmt.Println(routineErr)
for {
select {
case <-trackFired:
return
case <-ticker.C:
if routineErr := audioTrack.WriteSample(media.Sample{Data: []byte{0x00}, Duration: time.Second}); routineErr != nil {
fmt.Println(routineErr)
}
}
}
}()

<-onTrackFired.Done()
closePairNow(t, pcOffer, pcAnswer)
}

0 comments on commit 4430f41

Please sign in to comment.