Files
livekit/pkg/telemetry/statsworker_test.go
T
Raja SubramanianandClaude Fable 5.1 9a6c187afd telemetry: drop the stats worker re-key path (#4918)
Reverts #4816. Since livekit/cloud#4747 a room re-key closes every
participant's stats worker under the old room id and opens a new one
under the new id (LeaveSession/JoinSession, #4917), so RoomIDChanged
and reKeyRoom have nothing left to move, and with them go the sealed
stats batches, SetRoom, ForceClose and the reference hand-over that
only the re-key collision case needed. The worker map goes back to a
flat (room, participant) key.

The guard fixes that landed after #4816 (#4817, #4856, #4860) are kept.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-28 19:07:12 +05:30

59 lines
1.8 KiB
Go

package telemetry
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"github.com/livekit/protocol/livekit"
)
func TestStatsWorker(t *testing.T) {
t.Run("reference counted close works", func(t *testing.T) {
var g0, g1 ReferenceGuard
w := newStatsWorker(t.Context(), nil, "", "", "", "", &g0)
require.False(t, w.Closed(&g1))
require.False(t, w.Close(&g0))
require.False(t, w.Closed(&g1))
require.True(t, w.Close(&g1))
require.True(t, w.Closed(&g1))
})
t.Run("logging a nil worker does not panic", func(t *testing.T) {
var w *StatsWorker
require.NoError(t, w.MarshalLogObject(zapcore.NewMapObjectEncoder()))
})
}
func TestGetOrCreateWorkerReleasedGuard(t *testing.T) {
// ParticipantActive overtaken by the participant's close arrives with a guard that
// ParticipantLeft already released. It must not replace the closed worker with one
// nothing can release.
ts := &telemetryService{workers: make(map[statsWorkerKey]*StatsWorker)}
roomID, pID := livekit.RoomID("room"), livekit.ParticipantID("participant")
var g ReferenceGuard
w, found := ts.getOrCreateWorker(context.Background(), roomID, "", pID, "", &g)
require.False(t, found)
require.True(t, w.Close(&g))
t.Run("closed worker still in the map", func(t *testing.T) {
late, found := ts.getOrCreateWorker(context.Background(), roomID, "", pID, "", &g)
require.True(t, found)
require.Same(t, w, late)
require.Same(t, w, ts.workers[statsWorkerKey{roomID, pID}])
})
t.Run("closed worker already reaped", func(t *testing.T) {
delete(ts.workers, statsWorkerKey{roomID, pID})
late, found := ts.getOrCreateWorker(context.Background(), roomID, "", pID, "", &g)
require.True(t, found)
require.Nil(t, late)
require.Empty(t, ts.workers)
late.SetConnected()
})
}