-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn_test.go
189 lines (169 loc) · 4.18 KB
/
conn_test.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package tomtp
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
// mockAddr implements net.Addr for testing
type mockAddr struct{}
func (m mockAddr) Network() string { return "mock" }
func (m mockAddr) String() string { return "mock-address" }
func TestConnection_RTTCalculations(t *testing.T) {
tests := []struct {
name string
measurements []time.Duration
alpha float64
beta float64
wantSRTT time.Duration
wantRTO time.Duration
}{
{
name: "initial measurement",
measurements: []time.Duration{100 * time.Millisecond},
alpha: 0.125,
beta: 0.25,
wantSRTT: 100 * time.Millisecond,
wantRTO: 300 * time.Millisecond, // SRTT + 4*RTTVAR where RTTVAR is SRTT/2
},
{
name: "multiple measurements",
measurements: []time.Duration{100 * time.Millisecond, 120 * time.Millisecond, 90 * time.Millisecond},
alpha: 0.125,
beta: 0.25,
wantSRTT: 101 * time.Millisecond, // Approximate due to EMA calculation
wantRTO: 300 * time.Millisecond, // Should hit minRTO
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
conn := &Connection{
RTT: RTT{
alpha: tt.alpha,
beta: tt.beta,
minRTO: 300 * time.Millisecond,
maxRTO: 60 * time.Second,
},
}
for _, measurement := range tt.measurements {
conn.UpdateRTT(measurement)
}
gotSRTT := conn.GetSRTT()
gotRTO := conn.GetRTO()
// Allow for small floating-point differences
assert.InDelta(t, tt.wantSRTT.Nanoseconds(), gotSRTT.Nanoseconds(), float64(time.Millisecond.Nanoseconds()))
assert.InDelta(t, tt.wantRTO.Nanoseconds(), gotRTO.Nanoseconds(), float64(time.Millisecond.Nanoseconds()))
})
}
}
func TestConnection_GetOrNewStreamRcv(t *testing.T) {
tests := []struct {
name string
streamID uint32
setup bool
}{
{
name: "new stream",
streamID: 1,
setup: true,
},
{
name: "existing stream",
streamID: 1,
setup: false,
},
}
conn := &Connection{
streams: make(map[uint32]*Stream),
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stream, isNew := conn.GetOrNewStreamRcv(tt.streamID)
assert.NotNil(t, stream)
assert.Equal(t, tt.streamID, stream.streamId)
assert.Equal(t, tt.setup, isNew)
})
}
}
func TestConnection_Close(t *testing.T) {
conn := &Connection{
streams: make(map[uint32]*Stream),
}
// Create some test streams
streamIDs := []uint32{1, 2, 3}
for _, id := range streamIDs {
stream, isNew := conn.GetOrNewStreamRcv(id)
assert.True(t, isNew)
assert.NotNil(t, stream)
}
// Verify streams were created
assert.Equal(t, len(streamIDs), len(conn.streams))
// Close connection
err := conn.Close()
assert.NoError(t, err)
// Verify all streams were cleared
assert.Equal(t, 0, len(conn.streams))
}
func TestConnection_SetAlphaBeta(t *testing.T) {
conn := &Connection{}
testCases := []struct {
name string
alpha float64
beta float64
}{
{
name: "standard TCP values",
alpha: 0.125,
beta: 0.25,
},
{
name: "custom values",
alpha: 0.1,
beta: 0.2,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
conn.SetAlphaBeta(tc.alpha, tc.beta)
assert.Equal(t, tc.alpha, conn.alpha)
assert.Equal(t, tc.beta, conn.beta)
})
}
}
func TestConnection_RTOBounds(t *testing.T) {
conn := &Connection{
RTT: RTT{
alpha: 0.125,
beta: 0.25,
minRTO: time.Second,
maxRTO: 5 * time.Second,
},
}
tests := []struct {
name string
measurement time.Duration
wantRTO time.Duration
}{
{
name: "below minimum RTO",
measurement: 100 * time.Millisecond,
wantRTO: time.Second, // Should be capped at minRTO
},
{
name: "above maximum RTO",
measurement: 10 * time.Second,
wantRTO: 5 * time.Second, // Should be capped at maxRTO
},
{
name: "within bounds",
measurement: 100 * time.Millisecond,
wantRTO: 5 * time.Second, // Should remain unchanged
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
conn.UpdateRTT(tt.measurement)
gotRTO := conn.GetRTO()
assert.Equal(t, tt.wantRTO, gotRTO)
})
}
}