-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathabssendtimeextension_test.go
More file actions
140 lines (120 loc) · 3.39 KB
/
abssendtimeextension_test.go
File metadata and controls
140 lines (120 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package rtp
import (
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const absSendTimeResolution = 3800 * time.Nanosecond
func TestNtpConversion(t *testing.T) {
loc := time.FixedZone("UTC-5", -5*60*60)
tests := []struct {
t time.Time
n uint64
}{
{t: time.Date(1985, time.June, 23, 4, 0, 0, 0, loc), n: 0xa0c65b1000000000},
{t: time.Date(1999, time.December, 31, 23, 59, 59, 500000, loc), n: 0xbc18084f0020c49b},
{t: time.Date(2019, time.March, 27, 13, 39, 30, 8675309, loc), n: 0xe04641e202388b88},
}
for i, in := range tests {
out := toNtpTime(in.t)
assert.Equalf(
t, in.n, out,
"[%d] Converted NTP time from time.Time differs", i,
)
}
for i, in := range tests {
out := toTime(in.n)
diff := in.t.Sub(out)
assert.GreaterOrEqualf(
t, diff, -absSendTimeResolution,
"[%d] Converted time.Time from NTP time differs", i,
)
assert.LessOrEqual(
t, diff, absSendTimeResolution,
"[%d] Converted time.Time from NTP time differs", i,
)
}
}
func TestAbsSendTimeExtension_Roundtrip(t *testing.T) {
tests := []AbsSendTimeExtension{
{
Timestamp: 123456,
},
{
Timestamp: 654321,
},
}
for i, in := range tests {
b, err := in.Marshal()
assert.NoError(t, err)
var out AbsSendTimeExtension
assert.NoError(t, out.Unmarshal(b))
assert.Equalf(
t, in.Timestamp, out.Timestamp,
"[%d] Timestamp differs", i,
)
}
}
func TestAbsSendTimeExtension_Estimate(t *testing.T) {
tests := []struct {
sendNTP uint64
receiveNTP uint64
}{ // FFFFFFC000000000 mask of second
{0xa0c65b1000100000, 0xa0c65b1001000000}, // not carried
{0xa0c65b3f00000000, 0xa0c65b4001000000}, // carried during transmission
}
for i, in := range tests {
inTime := toTime(in.sendNTP)
send := &AbsSendTimeExtension{in.sendNTP >> 14}
b, err := send.Marshal()
assert.NoError(t, err)
var received AbsSendTimeExtension
assert.NoError(t, received.Unmarshal(b))
estimated := received.Estimate(toTime(in.receiveNTP))
diff := estimated.Sub(inTime)
assert.GreaterOrEqualf(
t, diff, -absSendTimeResolution,
"[%d] Estimated time differs, expected: %v, estimated: %v (receive time: %v)",
i, inTime.UTC(), estimated.UTC(), toTime(in.receiveNTP).UTC(),
)
assert.LessOrEqual(
t, diff, absSendTimeResolution,
"[%d] Estimated time differs, expected: %v, estimated: %v (receive time: %v)",
i, inTime.UTC(), estimated.UTC(), toTime(in.receiveNTP).UTC(),
)
}
}
func TestAbsSendTimeExtensionMarshalTo(t *testing.T) {
ext := AbsSendTimeExtension{Timestamp: 123456}
buf := make([]byte, ext.MarshalSize())
n, err := ext.MarshalTo(buf)
assert.NoError(t, err)
assert.Equal(t, ext.MarshalSize(), n)
expected, _ := ext.Marshal()
assert.Equal(t, expected, buf)
_, err = ext.MarshalTo(nil)
assert.ErrorIs(t, err, io.ErrShortBuffer)
}
//nolint:gochecknoglobals
var (
absSendTimeSink []byte
absSendTimeBuf = make([]byte, absSendTimeExtensionSize)
absSendTimeSinkInt int
)
func BenchmarkAbsSendTimeExtension_Marshal(b *testing.B) {
ext := AbsSendTimeExtension{Timestamp: 123456}
b.ReportAllocs()
for b.Loop() {
absSendTimeSink, _ = ext.Marshal()
}
}
func BenchmarkAbsSendTimeExtension_MarshalTo(b *testing.B) {
ext := AbsSendTimeExtension{Timestamp: 123456}
b.ReportAllocs()
for b.Loop() {
absSendTimeSinkInt, _ = ext.MarshalTo(absSendTimeBuf)
}
}