From 0de53f9a336a456d80c9434d1d3a463c09101904 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Tue, 16 Mar 2021 22:31:09 -0700 Subject: [PATCH] updated tests for participant update --- pkg/rtc/helper_test.go | 28 +++++++++++++++++++++ pkg/rtc/room.go | 2 +- pkg/rtc/room_test.go | 56 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/pkg/rtc/helper_test.go b/pkg/rtc/helper_test.go index f2649a7b..f877f547 100644 --- a/pkg/rtc/helper_test.go +++ b/pkg/rtc/helper_test.go @@ -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 } diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index 03a1829e..b1fb4701 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -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) } diff --git a/pkg/rtc/room_test.go b/pkg/rtc/room_test.go index 4c6de2d2..ba2bcc66 100644 --- a/pkg/rtc/room_test.go +++ b/pkg/rtc/room_test.go @@ -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)