finished room test

This commit is contained in:
David Zhao
2020-12-22 20:59:39 -08:00
parent 3518435783
commit 026c8725a3
4 changed files with 73 additions and 18 deletions
+12 -14
View File
@@ -125,7 +125,7 @@ func Build() error {
}
func Test() error {
mg.Deps(Proto, generateTest)
mg.Deps(Proto)
cmd := exec.Command("go", "test", "./...")
connectStd(cmd)
return cmd.Run()
@@ -139,6 +139,17 @@ func Clean() {
os.Remove(goChecksumFile)
}
// regenerate code
func Generate() error {
mg.Deps(installDeps)
fmt.Println("generating...")
cmd := exec.Command("go", "generate", "./...")
connectStd(cmd)
return cmd.Run()
}
// code generation
func generateCmd() error {
mg.Deps(installDeps)
@@ -153,19 +164,6 @@ func generateCmd() error {
return cmd.Run()
}
func generateTest() error {
mg.Deps(installDeps)
if !checksummer.IsChanged() {
return nil
}
fmt.Println("generating for tests...")
cmd := exec.Command("go", "generate", "./pkg/...")
connectStd(cmd)
return cmd.Run()
}
// implicitly install deps
func installDeps() error {
return installTools(false)
+8
View File
@@ -14,3 +14,11 @@ func newMockParticipant(name string) *rtcfakes.FakeParticipant {
return p
}
func newMockTrack(kind livekit.TrackInfo_Type, name string) *rtcfakes.FakePublishedTrack {
t := &rtcfakes.FakePublishedTrack{}
t.IDReturns(utils.NewGuid(utils.TrackPrefix))
t.KindReturns(kind)
t.StreamIDReturns(name)
return t
}
+4
View File
@@ -144,6 +144,10 @@ func (r *Room) onTrackAdded(participant Participant, track PublishedTrack) {
// skip publishing peer
continue
}
if existingParticipant.State() != livekit.ParticipantInfo_JOINED {
// not fully joined. don't subscribe yet
continue
}
if err := track.AddSubscriber(existingParticipant); err != nil {
logger.GetLogger().Errorw("could not subscribe to remoteTrack",
"srcParticipant", participant.ID(),
+49 -4
View File
@@ -2,6 +2,7 @@ package rtc_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
@@ -10,18 +11,21 @@ import (
"github.com/livekit/livekit-server/proto/livekit"
)
const (
numParticipants = 3
)
func TestRoomJoin(t *testing.T) {
t.Run("joining returns existing participant data", func(t *testing.T) {
numExisting := 3
rm := newRoomWithParticipants(t, numExisting)
rm := newRoomWithParticipants(t, numParticipants)
p := newMockParticipant("new")
rm.Join(p)
// expect new participant to get a JoinReply
participants := p.SendJoinResponseArgsForCall(0)
assert.Len(t, participants, numExisting)
assert.Len(t, rm.GetParticipants(), numExisting+1)
assert.Len(t, participants, numParticipants)
assert.Len(t, rm.GetParticipants(), numParticipants+1)
})
t.Run("subscribe to existing channels upon join", func(t *testing.T) {
@@ -48,6 +52,47 @@ func TestRoomJoin(t *testing.T) {
assert.Equal(t, p, mockP.AddSubscriberArgsForCall(mockP.AddSubscriberCallCount()-1))
}
})
t.Run("participant removal is broadcasted to others", func(t *testing.T) {
rm := newRoomWithParticipants(t, numParticipants)
participants := rm.GetParticipants()
p := participants[0].(*rtcfakes.FakeParticipant)
rm.RemoveParticipant(p.ID())
time.Sleep(10 * time.Millisecond)
for _, op := range participants {
if op == p {
assert.Zero(t, p.SendParticipantUpdateCallCount())
continue
}
fakeP := op.(*rtcfakes.FakeParticipant)
assert.Equal(t, 1, fakeP.SendParticipantUpdateCallCount())
}
})
}
func TestNewTrack(t *testing.T) {
t.Run("new track should be added to connected participants", func(t *testing.T) {
rm := newRoomWithParticipants(t, 4)
participants := rm.GetParticipants()
p0 := participants[0].(*rtcfakes.FakeParticipant)
p0.StateReturns(livekit.ParticipantInfo_JOINING)
p1 := participants[1].(*rtcfakes.FakeParticipant)
p1.StateReturns(livekit.ParticipantInfo_DISCONNECTED)
p2 := participants[2].(*rtcfakes.FakeParticipant)
p2.StateReturns(livekit.ParticipantInfo_JOINED)
p3 := participants[3].(*rtcfakes.FakeParticipant)
// p3 adds track
track := newMockTrack(livekit.TrackInfo_VIDEO, "webcam")
trackCB := p3.OnTrackPublishedArgsForCall(0)
assert.NotNil(t, trackCB)
trackCB(p3, track)
// only p2 should've been called
assert.Equal(t, 1, track.AddSubscriberCallCount())
assert.Equal(t, p2, track.AddSubscriberArgsForCall(0))
})
}
func newRoomWithParticipants(t *testing.T, num int) *rtc.Room {