Lock room creation to ensure atomic creation & node assignment (#46)

* Lock room creation to ensure atomic creation & node assignment

* more tests
This commit is contained in:
David Zhao
2021-07-12 14:50:14 -07:00
committed by GitHub
parent c0bff83a36
commit d38fc43b89
9 changed files with 287 additions and 7 deletions
+53 -5
View File
@@ -1,18 +1,17 @@
package service_test
import (
"sync"
"sync/atomic"
"testing"
"time"
"github.com/livekit/livekit-server/pkg/service"
livekit "github.com/livekit/livekit-server/proto"
"github.com/stretchr/testify/require"
)
func TestParticipantPersisence(t *testing.T) {
if testing.Short() {
t.SkipNow()
return
}
func TestParticipantPersistence(t *testing.T) {
rs := service.NewRedisRoomStore(redisClient())
roomName := "room1"
@@ -57,3 +56,52 @@ func TestParticipantPersisence(t *testing.T) {
_, err = rs.GetParticipant(roomName, p.Identity)
require.Equal(t, err, service.ErrParticipantNotFound)
}
func TestRoomLock(t *testing.T) {
rs := service.NewRedisRoomStore(redisClient())
lockInterval := 5 * time.Millisecond
roomName := "myroom"
t.Run("normal locking", func(t *testing.T) {
token, err := rs.LockRoom(roomName, lockInterval)
require.NoError(t, err)
require.NotEmpty(t, token)
require.NoError(t, rs.UnlockRoom(roomName, token))
})
t.Run("waits before acquiring lock", func(t *testing.T) {
token, err := rs.LockRoom(roomName, lockInterval)
require.NoError(t, err)
require.NotEmpty(t, token)
unlocked := uint32(0)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
// attempt to lock again
defer wg.Done()
token2, err := rs.LockRoom(roomName, lockInterval)
require.NoError(t, err)
defer rs.UnlockRoom(roomName, token2)
require.Equal(t, uint32(1), atomic.LoadUint32(&unlocked))
}()
// release after 2 ms
time.Sleep(2 * time.Millisecond)
atomic.StoreUint32(&unlocked, 1)
rs.UnlockRoom(roomName, token)
wg.Wait()
})
t.Run("lock expires", func(t *testing.T) {
token, err := rs.LockRoom(roomName, lockInterval)
require.NoError(t, err)
defer rs.UnlockRoom(roomName, token)
time.Sleep(lockInterval + time.Millisecond)
token2, err := rs.LockRoom(roomName, lockInterval)
require.NoError(t, err)
rs.UnlockRoom(roomName, token2)
})
}