-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
273 lines (220 loc) · 6.89 KB
/
main_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"context"
"fmt"
"math/rand"
"net"
"os"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/rs/zerolog"
"github.com/schollz/progressbar/v3"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
os.Setenv("UDP_LISTEN_PORT", "5000")
os.Setenv("UDP_DESTINATIONS", "localhost:6000,localhost:6001")
os.Setenv("LOG_LEVEL", "debug")
code := m.Run()
os.Unsetenv("UDP_LISTEN_PORT")
os.Unsetenv("UDP_DESTINATIONS")
os.Unsetenv("LOG_LEVEL")
os.Exit(code)
}
func TestHandleConnections(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockConn := NewMockUDPConn(ctrl)
destConns := make([]DestinationConn, 2)
for i := range destConns {
serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
assert.NoError(t, err)
serverConn, err := net.ListenUDP("udp", serverAddr)
assert.NoError(t, err)
defer serverConn.Close()
clientConn, err := net.DialUDP("udp", nil, serverConn.LocalAddr().(*net.UDPAddr))
assert.NoError(t, err)
defer clientConn.Close()
destConns[i] = DestinationConn{
conn: clientConn,
addr: serverConn.LocalAddr().String(),
}
}
ctx, cancel := context.WithCancel(context.Background())
packet := []byte("test packet")
remoteAddr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 54321}
mockConn.EXPECT().SetReadDeadline(gomock.Any()).Return(nil).AnyTimes()
mockConn.EXPECT().ReadFromUDP(gomock.Any()).DoAndReturn(func(b []byte) (int, *net.UDPAddr, error) {
n := copy(b, packet)
return n, remoteAddr, nil
}).AnyTimes()
go handleConnections(ctx, mockConn, destConns)
time.Sleep(100 * time.Millisecond)
cancel()
time.Sleep(100 * time.Millisecond)
}
func TestForwardPacket(t *testing.T) {
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
assert.NoError(t, err)
serverConn, err := net.ListenUDP("udp", addr)
assert.NoError(t, err)
defer serverConn.Close()
clientConn, err := net.DialUDP("udp", nil, serverConn.LocalAddr().(*net.UDPAddr))
assert.NoError(t, err)
defer clientConn.Close()
destConn := DestinationConn{
conn: clientConn,
addr: clientConn.RemoteAddr().String(),
}
packet := []byte("test packet")
forwardPacket(packet, destConn)
buffer := make([]byte, 1024)
serverConn.SetReadDeadline(time.Now().Add(1 * time.Second))
n, _, err := serverConn.ReadFromUDP(buffer)
if err != nil {
t.Logf("Error reading from UDP: %v", err)
}
t.Logf("Bytes read: %d", n)
t.Logf("Buffer content: %v", buffer[:n])
assert.NoError(t, err)
assert.Equal(t, packet, buffer[:n])
}
func TestForwardPacketsWithDifferentSizes(t *testing.T) {
destCount := 2
servers := make([]*net.UDPConn, destCount)
destConns := make([]DestinationConn, destCount)
for i := 0; i < destCount; i++ {
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
assert.NoError(t, err)
serverConn, err := net.ListenUDP("udp", addr)
assert.NoError(t, err)
defer serverConn.Close()
servers[i] = serverConn
clientConn, err := net.DialUDP("udp", nil, serverConn.LocalAddr().(*net.UDPAddr))
assert.NoError(t, err)
defer clientConn.Close()
destConns[i] = DestinationConn{
conn: clientConn,
addr: clientConn.RemoteAddr().String(),
}
}
testSizes := []int{10, 100, 500, 1000, 1500}
for _, size := range testSizes {
t.Run(fmt.Sprintf("PacketSize_%d", size), func(t *testing.T) {
packet := make([]byte, size)
for i := range packet {
packet[i] = byte(i % 256)
}
forwardPackets(packet, destConns)
time.Sleep(100 * time.Millisecond) // Give some time for packets to be forwarded
for i, server := range servers {
buffer := make([]byte, 2000)
server.SetReadDeadline(time.Now().Add(1 * time.Second))
n, _, err := server.ReadFromUDP(buffer)
assert.NoError(t, err, "Error reading from UDP for destination %d", i)
assert.Equal(t, size, n, "Incorrect number of bytes read for destination %d", i)
assert.Equal(t, packet, buffer[:n], "Incorrect packet content for destination %d", i)
t.Logf("Destination %d received %d bytes", i, n)
}
})
}
}
func TestHighLoadStatsdMetrics(t *testing.T) {
if testing.Short() {
t.Skip("Skipping high load test in short mode")
}
// Disable all logging for this test
originalLogLevel := zerolog.GlobalLevel()
zerolog.SetGlobalLevel(zerolog.Disabled)
defer zerolog.SetGlobalLevel(originalLogLevel)
// Setup
serverAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
assert.NoError(t, err)
serverConn, err := net.ListenUDP("udp", serverAddr)
assert.NoError(t, err)
defer serverConn.Close()
clientConn, err := net.DialUDP("udp", nil, serverConn.LocalAddr().(*net.UDPAddr))
assert.NoError(t, err)
defer clientConn.Close()
destConn := DestinationConn{
conn: clientConn,
addr: serverConn.LocalAddr().String(),
}
// Test parameters
metricsPerSecond := 100000
testDuration := 5 * time.Minute
totalMetrics := metricsPerSecond * int(testDuration.Seconds())
fmt.Printf("Starting load test: sending %d metrics/second for %v\n", metricsPerSecond, testDuration)
fmt.Printf("Listening on %s\n", serverConn.LocalAddr().String())
ctx, cancel := context.WithTimeout(context.Background(), testDuration)
defer cancel()
var sentCount, receivedCount int64
var wg sync.WaitGroup
// Progress bar
bar := progressbar.NewOptions(totalMetrics,
progressbar.OptionSetDescription("Sending metrics"),
progressbar.OptionSetWidth(50),
progressbar.OptionThrottle(65*time.Millisecond),
progressbar.OptionShowCount(),
progressbar.OptionOnCompletion(func() {
fmt.Println("\nLoad test completed")
}),
)
// Sending goroutine
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
for i := 0; i < metricsPerSecond; i++ {
metric := fmt.Sprintf("test.metric:%d|c", rand.Intn(100))
forwardPacket([]byte(metric), destConn)
atomic.AddInt64(&sentCount, 1)
bar.Add(1)
}
}
}
}()
// Receiving goroutine
wg.Add(1)
go func() {
defer wg.Done()
buffer := make([]byte, 65536)
for {
select {
case <-ctx.Done():
return
default:
serverConn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
n, _, err := serverConn.ReadFromUDP(buffer)
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue
}
if ctx.Err() != nil {
return
}
continue
}
atomic.AddInt64(&receivedCount, 1)
assert.True(t, n > 0, "Received empty packet")
}
}
}()
// Wait for test completion
wg.Wait()
// Assert results
sentFinal := atomic.LoadInt64(&sentCount)
receivedFinal := atomic.LoadInt64(&receivedCount)
fmt.Printf("\nTest completed. Sent: %d, Received: %d, Duration: %v\n", sentFinal, receivedFinal, testDuration)
assert.InDelta(t, totalMetrics, sentFinal, float64(totalMetrics)*0.1, "Incorrect number of metrics sent")
assert.InDelta(t, sentFinal, receivedFinal, float64(sentFinal)*0.1, "More than 10% packet loss")
}