close rooms after delay instead of immediately after participant departure, to allow reconnects

This commit is contained in:
David Zhao
2021-01-31 14:19:16 -08:00
parent 084ec378f0
commit 76b0dc62a7
6 changed files with 149 additions and 39 deletions
+42 -5
View File
@@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
"github.com/livekit/livekit-server/proto/livekit"
@@ -17,15 +18,49 @@ const (
defaultDelay = 10 * time.Millisecond
)
func init() {
logger.InitDevelopment("")
}
func TestJoinedState(t *testing.T) {
t.Run("new room should return joinedAt 0", func(t *testing.T) {
rm := newRoomWithParticipants(t, 0)
assert.Equal(t, int64(0), rm.FirstJoinedAt())
assert.Equal(t, int64(0), rm.LastLeftAt())
})
t.Run("should be current time when a participant joins", func(t *testing.T) {
s := time.Now().Unix()
rm := newRoomWithParticipants(t, 1)
assert.Equal(t, s, rm.FirstJoinedAt())
assert.Equal(t, int64(0), rm.LastLeftAt())
})
t.Run("should be set when a participant leaves", func(t *testing.T) {
rm := newRoomWithParticipants(t, 1)
p0 := rm.GetParticipants()[0]
s := time.Now().Unix()
rm.RemoveParticipant(p0.Identity())
assert.Equal(t, s, rm.LastLeftAt())
})
t.Run("LastLeftAt should not be set when there are still participants in the room", func(t *testing.T) {
rm := newRoomWithParticipants(t, 2)
p0 := rm.GetParticipants()[0]
rm.RemoveParticipant(p0.Identity())
assert.EqualValues(t, 0, rm.LastLeftAt())
})
}
func TestRoomJoin(t *testing.T) {
t.Run("joining returns existing participant data", func(t *testing.T) {
rm := newRoomWithParticipants(t, numParticipants)
p := newMockParticipant("new")
pNew := newMockParticipant("new")
rm.Join(p)
rm.Join(pNew)
// expect new participant to get a JoinReply
info, participants := p.SendJoinResponseArgsForCall(0)
info, participants := pNew.SendJoinResponseArgsForCall(0)
assert.Equal(t, info.Sid, rm.Sid)
assert.Len(t, participants, numParticipants)
assert.Len(t, rm.GetParticipants(), numParticipants+1)
@@ -98,10 +133,13 @@ func TestRoomClosure(t *testing.T) {
isClosed = true
})
p := rm.GetParticipants()[0]
// allows immediate close after
rm.EmptyTimeout = 0
rm.RemoveParticipant(p.Identity())
time.Sleep(defaultDelay)
rm.CloseIfEmpty()
assert.Len(t, rm.GetParticipants(), 0)
assert.True(t, isClosed)
@@ -114,7 +152,7 @@ func TestRoomClosure(t *testing.T) {
rm.OnClose(func() {
isClosed = true
})
assert.NotZero(t, rm.EmptyTimeout)
rm.CloseIfEmpty()
assert.False(t, isClosed)
})
@@ -156,7 +194,6 @@ func TestNewTrack(t *testing.T) {
}
func newRoomWithParticipants(t *testing.T, num int) *rtc.Room {
rm := rtc.NewRoom(
&livekit.Room{Name: "room"},
rtc.WebRTCConfig{},