better debug logs around negotiations

This commit is contained in:
David Zhao
2021-02-02 00:40:55 -08:00
parent 712d0da6cc
commit 70574deaf7
7 changed files with 186 additions and 31 deletions
+21 -4
View File
@@ -30,6 +30,7 @@ const (
nodeId1 = "node-1"
nodeId2 = "node-2"
syncDelay = 100 * time.Millisecond
connectTimeout = 10 * time.Second
// if there are deadlocks, it's helpful to set a short test timeout (i.e. go test -timeout=30s)
// let connection timeout happen
@@ -40,8 +41,11 @@ var (
roomClient livekit.RoomService
)
func setupSingleNodeTest(roomName string) *service.LivekitServer {
func init() {
logger.InitDevelopment("")
}
func setupSingleNodeTest(roomName string) *service.LivekitServer {
s := createSingleNodeServer()
go func() {
s.Start()
@@ -58,7 +62,6 @@ func setupSingleNodeTest(roomName string) *service.LivekitServer {
}
func setupMultiNodeTest() (*service.LivekitServer, *service.LivekitServer) {
logger.InitDevelopment("")
s1 := createMultiNodeServer(nodeId1, defaultServerPort)
s2 := createMultiNodeServer(nodeId2, secondServerPort)
go s1.Start()
@@ -100,21 +103,23 @@ func waitForServerToStart(s *service.LivekitServer) {
}
}
func withTimeout(t *testing.T, description string, f func() bool) {
func withTimeout(t *testing.T, description string, f func() bool) bool {
ctx, _ := context.WithTimeout(context.Background(), connectTimeout)
for {
select {
case <-ctx.Done():
t.Fatal("timed out: " + description)
return false
case <-time.After(10 * time.Millisecond):
if f() {
return
return true
}
}
}
}
func waitUntilConnected(t *testing.T, clients ...*client.RTCClient) {
logger.Infow("waiting for clients to become connected")
wg := sync.WaitGroup{}
for i := range clients {
c := clients[i]
@@ -232,6 +237,18 @@ func createRoomToken() string {
return t
}
func stopWriters(writers ...*client.TrackWriter) {
for _, w := range writers {
w.Stop()
}
}
func stopClients(clients ...*client.RTCClient) {
for _, c := range clients {
c.Stop()
}
}
type StaticKeyProvider struct {
}
+25
View File
@@ -5,6 +5,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -14,6 +15,9 @@ func TestMultiNodeRouting(t *testing.T) {
return
}
logger.Infow("---Starting TestMultiNodeRouting---")
defer logger.Infow("---Finishing TestMultiNodeRouting---")
s1, s2 := setupMultiNodeTest()
defer s1.Stop()
defer s2.Stop()
@@ -76,6 +80,9 @@ func TestConnectWithoutCreation(t *testing.T) {
t.SkipNow()
return
}
logger.Infow("---Starting TestConnectWithoutCreation---")
defer logger.Infow("---Finishing TestConnectWithoutCreation---")
s1, s2 := setupMultiNodeTest()
defer s1.Stop()
defer s2.Stop()
@@ -85,3 +92,21 @@ func TestConnectWithoutCreation(t *testing.T) {
c1.Stop()
}
// testing multiple scenarios rooms
func TestMultinodeScenarios(t *testing.T) {
if testing.Short() {
t.SkipNow()
return
}
logger.Infow("---Starting TestScenarios---")
defer logger.Infow("---Finishing TestScenarios---")
s1, s2 := setupMultiNodeTest()
scenarioPublishingUponJoining(t, defaultServerPort, secondServerPort)
defer s1.Stop()
defer s2.Stop()
}
+100 -3
View File
@@ -1,20 +1,117 @@
package test
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/cmd/cli/client"
"github.com/livekit/livekit-server/pkg/logger"
)
// a scenario with lots of clients connecting, publishing, and leaving at random periods
func scenarioRandom(t *testing.T) {
func scenarioPublishingUponJoining(t *testing.T, ports ...int) {
c1 := createRTCClient("puj_1", ports[rand.Intn(len(ports))])
c2 := createRTCClient("puj_2", ports[rand.Intn(len(ports))])
c3 := createRTCClient("puj_3", ports[rand.Intn(len(ports))])
defer stopClients(c1, c2, c3)
waitUntilConnected(t, c1, c2, c3)
// c1 and c2 publishing, c3 just receiving
writers := publishTracksForClients(t, c1, c2)
defer stopWriters(writers...)
logger.Infow("waiting to receive tracks from c1 and c2")
success := withTimeout(t, "c3 should receive tracks from both clients", func() bool {
tracks := c3.SubscribedTracks()
if len(tracks[c1.ID()]) != 2 {
return false
}
if len(tracks[c2.ID()]) != 2 {
return false
}
return true
})
if !success {
t.FailNow()
}
// after a delay, c2 reconnects, then publishing
time.Sleep(syncDelay)
c2.Stop()
logger.Infow("waiting for c2 tracks to be gone")
success = withTimeout(t, "c2 tracks should be gone", func() bool {
tracks := c3.SubscribedTracks()
if len(tracks[c1.ID()]) != 2 {
return false
}
if len(tracks[c2.ID()]) != 0 {
return false
}
if len(c1.SubscribedTracks()[c2.ID()]) != 0 {
return false
}
return true
})
if !success {
t.FailNow()
}
logger.Infow("c2 reconnecting")
c2 = createRTCClient("puj_2", ports[rand.Intn(len(ports))])
defer c2.Stop()
waitUntilConnected(t, c2)
writers = publishTracksForClients(t, c2)
defer stopWriters(writers...)
logger.Infow("waiting for reconnected c2 tracks to publish")
success = withTimeout(t, "new c2 tracks should be published again", func() bool {
tracks := c3.SubscribedTracks()
if len(tracks[c2.ID()]) != 2 {
return false
}
if len(c1.SubscribedTracks()[c2.ID()]) != 2 {
return false
}
return true
})
if !success {
t.FailNow()
}
}
// websocket reconnects
func scenarioWSReconnect(t *testing.T) {
c1 := createRTCClient("wsr_c1", defaultServerPort)
c2 := createRTCClient("wsr_c2", defaultServerPort)
c1 := createRTCClient("wsr_1", defaultServerPort)
c2 := createRTCClient("wsr_2", defaultServerPort)
waitUntilConnected(t, c1, c2)
// c1 publishes track, but disconnects websockets and reconnects
}
func publishTracksForClients(t *testing.T, clients ...*client.RTCClient) []*client.TrackWriter {
logger.Infow("publishing tracks for clients")
var writers []*client.TrackWriter
for i, _ := range clients {
c := clients[i]
tw, err := c.AddStaticTrack("audio/opus", "audio", "webcam")
if !assert.NoError(t, err) {
t.FailNow()
return nil
}
writers = append(writers, tw)
tw, err = c.AddStaticTrack("video/vp8", "video", "webcam")
if !assert.NoError(t, err) {
t.FailNow()
return nil
}
writers = append(writers, tw)
}
return writers
}