Improve readability of test failures (#386)
This commit is contained in:
+22
-19
@@ -323,24 +323,27 @@ func TestActiveSpeakers(t *testing.T) {
|
||||
require.NotEmpty(t, speakers)
|
||||
require.Equal(t, string(p.ID()), speakers[0].Sid)
|
||||
|
||||
testutils.WithTimeout(t, "ensure everyone has gotten an audio update", func() bool {
|
||||
testutils.WithTimeout(t, func() string {
|
||||
for _, op := range participants {
|
||||
op := op.(*typesfakes.FakeLocalParticipant)
|
||||
updates := getActiveSpeakerUpdates(op)
|
||||
if len(updates) == 0 {
|
||||
return false
|
||||
return fmt.Sprintf("%s did not get any audio updates", op.Identity())
|
||||
}
|
||||
}
|
||||
return true
|
||||
return ""
|
||||
})
|
||||
|
||||
// no longer speaking, send update with empty items
|
||||
p.GetAudioLevelReturns(127, false)
|
||||
|
||||
testutils.WithTimeout(t, "ensure no one is speaking", func() bool {
|
||||
testutils.WithTimeout(t, func() string {
|
||||
updates := getActiveSpeakerUpdates(p)
|
||||
lastUpdate := updates[len(updates)-1]
|
||||
return len(lastUpdate.Speakers) == 0
|
||||
if len(lastUpdate.Speakers) != 0 {
|
||||
return fmt.Sprintf("expected no speakers, but found %d", len(lastUpdate.Speakers))
|
||||
}
|
||||
return ""
|
||||
})
|
||||
})
|
||||
|
||||
@@ -353,48 +356,48 @@ func TestActiveSpeakers(t *testing.T) {
|
||||
p.GetAudioLevelReturns(30, true)
|
||||
convertedLevel := rtc.ConvertAudioLevel(30)
|
||||
|
||||
testutils.WithTimeout(t, "checking first update is received", func() bool {
|
||||
testutils.WithTimeout(t, func() string {
|
||||
updates := getActiveSpeakerUpdates(op)
|
||||
if len(updates) == 0 {
|
||||
return false
|
||||
return "no speaker updates received"
|
||||
}
|
||||
lastSpeakers := updates[len(updates)-1].Speakers
|
||||
if len(lastSpeakers) == 0 {
|
||||
return false
|
||||
return "no speakers in the update"
|
||||
}
|
||||
if lastSpeakers[0].Level > convertedLevel {
|
||||
return true
|
||||
return ""
|
||||
}
|
||||
return false
|
||||
return "level mismatch"
|
||||
})
|
||||
|
||||
testutils.WithTimeout(t, "eventually reaches actual levels", func() bool {
|
||||
testutils.WithTimeout(t, func() string {
|
||||
updates := getActiveSpeakerUpdates(op)
|
||||
if len(updates) == 0 {
|
||||
return false
|
||||
return "no updates received"
|
||||
}
|
||||
lastSpeakers := updates[len(updates)-1].Speakers
|
||||
if len(lastSpeakers) == 0 {
|
||||
return false
|
||||
return "no speakers found"
|
||||
}
|
||||
if lastSpeakers[0].Level > convertedLevel {
|
||||
return true
|
||||
return ""
|
||||
}
|
||||
return false
|
||||
return "did not match expected levels"
|
||||
})
|
||||
|
||||
p.GetAudioLevelReturns(127, false)
|
||||
|
||||
testutils.WithTimeout(t, "eventually goes back to 0", func() bool {
|
||||
testutils.WithTimeout(t, func() string {
|
||||
updates := getActiveSpeakerUpdates(op)
|
||||
if len(updates) == 0 {
|
||||
return false
|
||||
return "no speaker updates received"
|
||||
}
|
||||
lastSpeakers := updates[len(updates)-1].Speakers
|
||||
if len(lastSpeakers) == 0 {
|
||||
return true
|
||||
return ""
|
||||
}
|
||||
return false
|
||||
return "speakers didn't go back to zero"
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -37,9 +37,15 @@ func TestMissingAnswerDuringICERestart(t *testing.T) {
|
||||
require.NoError(t, transportA.CreateAndSendOffer(nil))
|
||||
|
||||
// ensure we are connected the first time
|
||||
testutils.WithTimeout(t, "initial ICE connectivity", func() bool {
|
||||
return transportA.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected &&
|
||||
transportB.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if transportA.pc.ICEConnectionState() != webrtc.ICEConnectionStateConnected {
|
||||
return "transportA did not become connected"
|
||||
}
|
||||
|
||||
if transportB.pc.ICEConnectionState() != webrtc.ICEConnectionStateConnected {
|
||||
return "transportB did not become connected"
|
||||
}
|
||||
return ""
|
||||
})
|
||||
require.Equal(t, webrtc.ICEConnectionStateConnected, transportA.pc.ICEConnectionState())
|
||||
require.Equal(t, webrtc.ICEConnectionStateConnected, transportB.pc.ICEConnectionState())
|
||||
@@ -57,9 +63,14 @@ func TestMissingAnswerDuringICERestart(t *testing.T) {
|
||||
ICERestart: true,
|
||||
}))
|
||||
|
||||
testutils.WithTimeout(t, "restarted ICE connectivity", func() bool {
|
||||
return transportA.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected &&
|
||||
transportB.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if transportA.pc.ICEConnectionState() != webrtc.ICEConnectionStateConnected {
|
||||
return "transportA did not reconnect after ICE restart"
|
||||
}
|
||||
if transportB.pc.ICEConnectionState() != webrtc.ICEConnectionStateConnected {
|
||||
return "transportB did not reconnect after ICE restart"
|
||||
}
|
||||
return ""
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sfu
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -21,8 +22,12 @@ func TestStreamTracker(t *testing.T) {
|
||||
// observe first packet
|
||||
tracker.Observe(1)
|
||||
|
||||
testutils.WithTimeout(t, "first packet makes stream active", func() bool {
|
||||
return callbackCalled.get()
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if callbackCalled.get() {
|
||||
return ""
|
||||
} else {
|
||||
return "first packet didn't activate stream"
|
||||
}
|
||||
})
|
||||
|
||||
require.Equal(t, StreamStatusActive, tracker.Status())
|
||||
@@ -34,8 +39,12 @@ func TestStreamTracker(t *testing.T) {
|
||||
require.Equal(t, StreamStatusStopped, tracker.Status())
|
||||
|
||||
tracker.Observe(1)
|
||||
testutils.WithTimeout(t, "first packet makes stream active", func() bool {
|
||||
return tracker.Status() == StreamStatusActive
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if tracker.Status() == StreamStatusActive {
|
||||
return ""
|
||||
} else {
|
||||
return "first packet did not activate stream"
|
||||
}
|
||||
})
|
||||
|
||||
callbackCalled := atomicBool(0)
|
||||
@@ -55,8 +64,12 @@ func TestStreamTracker(t *testing.T) {
|
||||
require.Equal(t, StreamStatusStopped, tracker.Status())
|
||||
|
||||
tracker.Observe(1)
|
||||
testutils.WithTimeout(t, "first packet makes stream active", func() bool {
|
||||
return tracker.Status() == StreamStatusActive
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if tracker.Status() == StreamStatusActive {
|
||||
return ""
|
||||
} else {
|
||||
return "first packet did not activate stream"
|
||||
}
|
||||
})
|
||||
|
||||
tracker.maybeSetStopped()
|
||||
@@ -73,8 +86,12 @@ func TestStreamTracker(t *testing.T) {
|
||||
t.Run("does not change to inactive when paused", func(t *testing.T) {
|
||||
tracker := NewStreamTracker(5, 60, 500*time.Millisecond)
|
||||
tracker.Observe(1)
|
||||
testutils.WithTimeout(t, "first packet makes stream active", func() bool {
|
||||
return tracker.Status() == StreamStatusActive
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if tracker.Status() == StreamStatusActive {
|
||||
return ""
|
||||
} else {
|
||||
return "first packet did not activate stream"
|
||||
}
|
||||
})
|
||||
|
||||
tracker.SetPaused(true)
|
||||
@@ -93,8 +110,12 @@ func TestStreamTracker(t *testing.T) {
|
||||
// observe first packet
|
||||
tracker.Observe(1)
|
||||
|
||||
testutils.WithTimeout(t, "first packet makes stream active", func() bool {
|
||||
return callbackCalled.get() == 1
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if callbackCalled.get() == 1 {
|
||||
return ""
|
||||
} else {
|
||||
return fmt.Sprintf("expected onStatusChanged to be called once, actual: %d", callbackCalled.get())
|
||||
}
|
||||
})
|
||||
|
||||
require.Equal(t, StreamStatusActive, tracker.Status())
|
||||
@@ -117,8 +138,12 @@ func TestStreamTracker(t *testing.T) {
|
||||
// first packet after reset
|
||||
tracker.Observe(1)
|
||||
|
||||
testutils.WithTimeout(t, "first packet after reset makes stream active", func() bool {
|
||||
return callbackCalled.get() == 2
|
||||
testutils.WithTimeout(t, func() string {
|
||||
if callbackCalled.get() == 2 {
|
||||
return ""
|
||||
} else {
|
||||
return fmt.Sprintf("expected onStatusChanged to be called twice, actual %d", callbackCalled.get())
|
||||
}
|
||||
})
|
||||
|
||||
require.Equal(t, StreamStatusActive, tracker.Status())
|
||||
|
||||
@@ -4,26 +4,24 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
ConnectTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
func WithTimeout(t *testing.T, description string, f func() bool) bool {
|
||||
logger.Infow(description)
|
||||
func WithTimeout(t *testing.T, f func() string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), ConnectTimeout)
|
||||
defer cancel()
|
||||
lastErr := ""
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Fatal("timed out: " + description)
|
||||
return false
|
||||
t.Fatal("timed out: " + lastErr)
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
if f() {
|
||||
return true
|
||||
lastErr = f()
|
||||
if lastErr == "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user