Files
livekit/pkg/service/rtcservice_test.go
T
Raja SubramanianandClaude Opus 5.5 77eeb22b62 agent: stamp the accept and assignment times of a job attempt (#5123)
* agent: stamp the accept and assignment times of a job attempt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* deps: pin protocol with the job accept and assignment times

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* service: stamp a new user's join request time as an attribute

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* service: use the lk.rtc.joinRequestTime name

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* deps: pin protocol with the join request time attribute

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* deps: pin protocol main, keep turn and srtp on pion/transport v4

The warp forks of webrtc, dtls and ice are still on pion/transport v4.
Protocol main requires turn 5.1 and srtp 3.1, which moved to v5.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* deps: pin protocol v1.53.2

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: expect the join request time among a participant's attributes

The server now stamps lk.rtc.joinRequestTime on a new standard participant, so
other participants see it beside the token's attributes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 20:54:48 +05:30

134 lines
4.3 KiB
Go

// Copyright 2026 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
"github.com/livekit/livekit-server/pkg/routing"
)
func leaveResponse(action livekit.LeaveRequest_Action) *livekit.SignalResponse {
return &livekit.SignalResponse{
Message: &livekit.SignalResponse_Leave{
Leave: &livekit.LeaveRequest{
Action: action,
Reason: livekit.DisconnectReason_MIGRATION,
},
},
}
}
func TestDrainMessageSource(t *testing.T) {
collect := func(msgs *[]proto.Message) func(proto.Message) bool {
return func(msg proto.Message) bool {
*msgs = append(*msgs, msg)
return true
}
}
t.Run("drains queued messages of a closed source", func(t *testing.T) {
source := routing.NewDefaultMessageChannel("CO_test")
require.NoError(t, source.WriteMessage(leaveResponse(livekit.LeaveRequest_RESUME)))
require.NoError(t, source.WriteMessage(leaveResponse(livekit.LeaveRequest_RECONNECT)))
source.Close()
var got []proto.Message
require.True(t, drainMessageSource(source, time.Second, collect(&got)))
require.Len(t, got, 2)
require.Equal(
t,
livekit.LeaveRequest_RESUME,
got[0].(*livekit.SignalResponse).GetLeave().GetAction(),
)
require.Equal(
t,
livekit.LeaveRequest_RECONNECT,
got[1].(*livekit.SignalResponse).GetLeave().GetAction(),
)
})
t.Run("drains messages written while draining", func(t *testing.T) {
source := routing.NewDefaultMessageChannel("CO_test")
// mimics the relay pushing a message that was still in flight when the
// request direction went away, and closing the source right after
go func() {
time.Sleep(20 * time.Millisecond)
_ = source.WriteMessage(leaveResponse(livekit.LeaveRequest_RESUME))
source.Close()
}()
var got []proto.Message
require.True(t, drainMessageSource(source, time.Second, collect(&got)))
require.Len(t, got, 1)
})
t.Run("gives up on the deadline when the source stays open", func(t *testing.T) {
source := routing.NewDefaultMessageChannel("CO_test")
require.NoError(t, source.WriteMessage(leaveResponse(livekit.LeaveRequest_RESUME)))
var got []proto.Message
start := time.Now()
require.False(t, drainMessageSource(source, 50*time.Millisecond, collect(&got)))
require.GreaterOrEqual(t, time.Since(start), 50*time.Millisecond)
// what was queued is still flushed
require.Len(t, got, 1)
})
t.Run("stops when the write fails", func(t *testing.T) {
source := routing.NewDefaultMessageChannel("CO_test")
require.NoError(t, source.WriteMessage(leaveResponse(livekit.LeaveRequest_RESUME)))
require.NoError(t, source.WriteMessage(leaveResponse(livekit.LeaveRequest_RECONNECT)))
source.Close()
var got []proto.Message
require.False(t, drainMessageSource(source, time.Second, func(msg proto.Message) bool {
got = append(got, msg)
return false
}))
require.Len(t, got, 1)
})
}
func TestStampJoinRequestTime(t *testing.T) {
at := time.UnixMilli(1_700_000_000_123)
stamped := func(pi routing.ParticipantInit) map[string]string {
stampJoinRequestTime(&pi, at)
return pi.Grants.Attributes
}
t.Run("new user", func(t *testing.T) {
attrs := stamped(routing.ParticipantInit{Grants: &auth.ClaimGrants{Attributes: map[string]string{"a": "b"}}})
require.Equal(t, map[string]string{"a": "b", livekit.AttrRTCJoinRequestTime: "1700000000123"}, attrs)
})
t.Run("reconnect", func(t *testing.T) {
require.Empty(t, stamped(routing.ParticipantInit{Reconnect: true, Grants: &auth.ClaimGrants{}}))
})
t.Run("agent", func(t *testing.T) {
grants := &auth.ClaimGrants{}
grants.SetParticipantKind(livekit.ParticipantInfo_AGENT)
require.Empty(t, stamped(routing.ParticipantInit{Grants: grants}))
})
}