updated tests for participant update

This commit is contained in:
David Zhao
2021-03-16 22:31:09 -07:00
parent f7ed2cee60
commit 0de53f9a33
3 changed files with 85 additions and 1 deletions
+28
View File
@@ -1,6 +1,7 @@
package rtc_test
import (
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
livekit "github.com/livekit/livekit-server/proto"
"github.com/livekit/protocol/utils"
@@ -12,6 +13,33 @@ func newMockParticipant(identity string) *typesfakes.FakeParticipant {
p.IdentityReturns(identity)
p.StateReturns(livekit.ParticipantInfo_JOINED)
p.SetMetadataStub = func(m map[string]interface{}) error {
var f func(participant types.Participant)
if p.OnMetadataUpdateCallCount() > 0 {
f = p.OnMetadataUpdateArgsForCall(p.OnMetadataUpdateCallCount() - 1)
}
if f != nil {
f(p)
}
return nil
}
updateTrack := func() {
var f func(participant types.Participant, track types.PublishedTrack)
if p.OnTrackUpdatedCallCount() > 0 {
f = p.OnTrackUpdatedArgsForCall(p.OnTrackUpdatedCallCount() - 1)
}
if f != nil {
f(p, nil)
}
}
p.SetTrackMutedStub = func(sid string, muted bool) {
updateTrack()
}
p.AddTrackStub = func(s string, s2 string, trackType livekit.TrackType) {
updateTrack()
}
return p
}
+1 -1
View File
@@ -323,7 +323,7 @@ func (r *Room) onTrackAdded(participant types.Participant, track types.Published
func (r *Room) onTrackUpdated(p types.Participant, track types.PublishedTrack) {
// send track updates to everyone, especially if track was updated by admin
r.broadcastParticipantState(p, false)
r.broadcastParticipantState(p, true)
if r.onParticipantChanged != nil {
r.onParticipantChanged(p)
}
+56
View File
@@ -134,6 +134,62 @@ func TestRoomJoin(t *testing.T) {
})
}
// various state changes to participant and that others are receiving update
func TestParticipantUpdate(t *testing.T) {
tests := []struct {
name string
sendToSender bool // should sender receive it
action func(p types.Participant)
}{
{
"track mutes are sent to everyone",
false,
func(p types.Participant) {
p.SetTrackMuted("", true)
},
},
{
"track metadata updates are sent to everyone",
true,
func(p types.Participant) {
p.SetMetadata(map[string]interface{}{})
},
},
{
"track publishes are sent to existing participants",
false,
func(p types.Participant) {
p.AddTrack("", "", livekit.TrackType_VIDEO)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rm := newRoomWithParticipants(t, 3)
// remember how many times send has been called for each
callCounts := make(map[string]int)
for _, p := range rm.GetParticipants() {
fp := p.(*typesfakes.FakeParticipant)
callCounts[p.ID()] = fp.SendParticipantUpdateCallCount()
}
sender := rm.GetParticipants()[0]
test.action(sender)
// go through the other participants, make sure they've received update
for _, p := range rm.GetParticipants() {
expected := callCounts[p.ID()]
if p != sender || test.sendToSender {
expected += 1
}
fp := p.(*typesfakes.FakeParticipant)
require.Equal(t, expected, fp.SendParticipantUpdateCallCount())
}
})
}
}
func TestRoomClosure(t *testing.T) {
t.Run("room closes after participant leaves", func(t *testing.T) {
rm := newRoomWithParticipants(t, 1)