using counterfeiter instead of gomocks, much simpler

This commit is contained in:
David Zhao
2020-12-22 17:26:26 -08:00
parent 1b396d2000
commit 3518435783
18 changed files with 3352 additions and 913 deletions
+40 -20
View File
@@ -1,45 +1,65 @@
package rtc
package rtc_test
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/rtcfakes"
"github.com/livekit/livekit-server/proto/livekit"
)
func TestRoomJoin(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
t.Run("joining returns existing participant data", func(t *testing.T) {
numExisting := 3
rm := newRoomWithParticipants(mockCtrl, numExisting)
assert.NotNil(t, rm)
p := newMockParticipant(mockCtrl)
assert.NotNil(t, p)
// expect new participant to get a JoinReply
p.EXPECT().SendJoinResponse(gomock.Any()).Return(nil).Do(
func(otherParticipants []Participant) {
assert.Len(t, otherParticipants, numExisting)
})
rm := newRoomWithParticipants(t, numExisting)
p := newMockParticipant("new")
rm.Join(p)
assert.Len(t, rm.participants, numExisting+1)
// expect new participant to get a JoinReply
participants := p.SendJoinResponseArgsForCall(0)
assert.Len(t, participants, numExisting)
assert.Len(t, rm.GetParticipants(), numExisting+1)
})
t.Run("subscribe to existing channels upon join", func(t *testing.T) {
numExisting := 3
rm := newRoomWithParticipants(t, numExisting)
p := newMockParticipant("new")
err := rm.Join(p)
assert.NoError(t, err)
stateChangeCB := p.OnStateChangeArgsForCall(0)
assert.NotNil(t, stateChangeCB)
p.StateReturns(livekit.ParticipantInfo_JOINED)
stateChangeCB(p, livekit.ParticipantInfo_JOINING)
// it should become a subscriber when connectivity changes
for _, op := range rm.GetParticipants() {
if p == op {
continue
}
mockP := op.(*rtcfakes.FakeParticipant)
assert.NotZero(t, mockP.AddSubscriberCallCount())
// last call should be to add the newest participant
assert.Equal(t, p, mockP.AddSubscriberArgsForCall(mockP.AddSubscriberCallCount()-1))
}
})
}
func newRoomWithParticipants(mockCtrl *gomock.Controller, num int) *Room {
rm, err := NewRoomForRequest(&livekit.CreateRoomRequest{}, &WebRTCConfig{})
func newRoomWithParticipants(t *testing.T, num int) *rtc.Room {
rm, err := rtc.NewRoomForRequest(&livekit.CreateRoomRequest{}, &rtc.WebRTCConfig{})
if err != nil {
panic("could not create a room")
}
for i := 0; i < num; i++ {
participant := newMockParticipant(mockCtrl)
rm.participants[participant.ID()] = participant
participant := newMockParticipant("")
err := rm.Join(participant)
assert.NoError(t, err)
//rm.participants[participant.ID()] = participant
}
return rm
}