mirror of
https://github.com/livekit/livekit.git
synced 2026-12-05 18:28:09 +00:00
lk.rtc.joinRequestTime was set only for standard participants. SIP, agent, egress and other participants now get it too, so the time a participant reached the signal node is known for every kind. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
140 lines
4.5 KiB
Go
140 lines
4.5 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("every participant kind", func(t *testing.T) {
|
|
for _, kind := range []livekit.ParticipantInfo_Kind{
|
|
livekit.ParticipantInfo_SIP,
|
|
livekit.ParticipantInfo_AGENT,
|
|
livekit.ParticipantInfo_EGRESS,
|
|
} {
|
|
grants := &auth.ClaimGrants{}
|
|
grants.SetParticipantKind(kind)
|
|
require.Equal(t, map[string]string{livekit.AttrRTCJoinRequestTime: "1700000000123"}, stamped(routing.ParticipantInit{Grants: grants}), kind.String())
|
|
}
|
|
})
|
|
}
|