| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
//go:build !js |
| 5 |
// +build !js |
| 6 |
|
| 7 |
// Package udp implements DTLS specific UDP networking primitives. |
| 8 |
package udp |
| 9 |
|
| 10 |
import ( |
| 11 |
"encoding/json" |
| 12 |
"errors" |
| 13 |
"fmt" |
| 14 |
"io" |
| 15 |
"net" |
| 16 |
"sync" |
| 17 |
"testing" |
| 18 |
"time" |
| 19 |
|
| 20 |
dtlsnet "github.com/pion/dtls/v3/pkg/net" |
| 21 |
"github.com/pion/transport/v4/test" |
| 22 |
"github.com/stretchr/testify/assert" |
| 23 |
) |
| 24 |
|
| 25 |
var ( |
| 26 |
errHandshakeFailed = errors.New("handshake failed") |
| 27 |
errUDPCastFailed = fmt.Errorf("failed to cast listener Addr to *net.UDPAddr") |
| 28 |
) |
| 29 |
|
| 30 |
func TestStressDuplex(t *testing.T) { |
| 31 |
// Limit runtime in case of deadlocks |
| 32 |
lim := test.TimeOut(time.Second * 20) |
| 33 |
defer lim.Stop() |
| 34 |
|
| 35 |
// Check for leaking routines |
| 36 |
report := test.CheckRoutines(t) |
| 37 |
defer report() |
| 38 |
|
| 39 |
// Run the test |
| 40 |
stressDuplex(t) |
| 41 |
} |
| 42 |
|
| 43 |
type rw struct { |
| 44 |
p net.PacketConn |
| 45 |
raddr net.Addr |
| 46 |
} |
| 47 |
|
| 48 |
func fromPC(p net.PacketConn, raddr net.Addr) *rw { |
| 49 |
return &rw{ |
| 50 |
p: p, |
| 51 |
raddr: raddr, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func (r *rw) Read(p []byte) (int, error) { |
| 56 |
n, _, err := r.p.ReadFrom(p) |
| 57 |
|
| 58 |
return n, err |
| 59 |
} |
| 60 |
|
| 61 |
func (r *rw) Write(p []byte) (int, error) { |
| 62 |
return r.p.WriteTo(p, r.raddr) |
| 63 |
} |
| 64 |
|
| 65 |
func stressDuplex(t *testing.T) { |
| 66 |
t.Helper() |
| 67 |
|
| 68 |
listener, ca, cb, err := pipe() |
| 69 |
assert.NoError(t, err) |
| 70 |
|
| 71 |
defer func() { |
| 72 |
assert.NoError(t, ca.Close()) |
| 73 |
assert.NoError(t, cb.Close()) |
| 74 |
assert.NoError(t, listener.Close()) |
| 75 |
}() |
| 76 |
|
| 77 |
opt := test.Options{ |
| 78 |
MsgSize: 2048, |
| 79 |
MsgCount: 1, // Can't rely on UDP message order in CI |
| 80 |
} |
| 81 |
|
| 82 |
assert.NoError(t, test.StressDuplex(fromPC(ca, cb.LocalAddr()), cb, opt)) |
| 83 |
} |
| 84 |
|
| 85 |
func TestListenerCloseTimeout(t *testing.T) { |
| 86 |
// Limit runtime in case of deadlocks |
| 87 |
lim := test.TimeOut(time.Second * 5) |
| 88 |
defer lim.Stop() |
| 89 |
|
| 90 |
// Check for leaking routines |
| 91 |
report := test.CheckRoutines(t) |
| 92 |
defer report() |
| 93 |
|
| 94 |
listener, ca, _, err := pipe() |
| 95 |
assert.NoError(t, err) |
| 96 |
|
| 97 |
assert.NoError(t, listener.Close()) |
| 98 |
|
| 99 |
// Close client after server closes to cleanup |
| 100 |
assert.NoError(t, ca.Close()) |
| 101 |
} |
| 102 |
|
| 103 |
func TestListenerCloseUnaccepted(t *testing.T) { |
| 104 |
// Limit runtime in case of deadlocks |
| 105 |
lim := test.TimeOut(time.Second * 20) |
| 106 |
defer lim.Stop() |
| 107 |
|
| 108 |
// Check for leaking routines |
| 109 |
report := test.CheckRoutines(t) |
| 110 |
defer report() |
| 111 |
|
| 112 |
const backlog = 2 |
| 113 |
|
| 114 |
network, addr := getConfig() |
| 115 |
listener, err := (&ListenConfig{ |
| 116 |
Backlog: backlog, |
| 117 |
}).Listen(network, addr) |
| 118 |
assert.NoError(t, err) |
| 119 |
|
| 120 |
for i := 0; i < backlog; i++ { |
| 121 |
aAddr, ok := listener.Addr().(*net.UDPAddr) |
| 122 |
assert.True(t, ok) |
| 123 |
conn, dErr := net.DialUDP(network, nil, aAddr) |
| 124 |
if dErr != nil { |
| 125 |
assert.Fail(t, "dial failed: %v", dErr) |
| 126 |
|
| 127 |
continue |
| 128 |
} |
| 129 |
_, wErr := conn.Write([]byte{byte(i)}) |
| 130 |
assert.NoError(t, wErr) |
| 131 |
assert.NoError(t, conn.Close()) |
| 132 |
} |
| 133 |
|
| 134 |
time.Sleep(100 * time.Millisecond) // Wait all packets being processed by readLoop |
| 135 |
|
| 136 |
// Unaccepted connections must be closed by listener.Close() |
| 137 |
assert.NoError(t, listener.Close()) |
| 138 |
} |
| 139 |
|
| 140 |
func TestListenerAcceptFilter(t *testing.T) { |
| 141 |
// Limit runtime in case of deadlocks |
| 142 |
lim := test.TimeOut(time.Second * 20) |
| 143 |
defer lim.Stop() |
| 144 |
|
| 145 |
// Check for leaking routines |
| 146 |
report := test.CheckRoutines(t) |
| 147 |
defer report() |
| 148 |
|
| 149 |
testCases := map[string]struct { |
| 150 |
packet []byte |
| 151 |
accept bool |
| 152 |
}{ |
| 153 |
"CreateConn": { |
| 154 |
packet: []byte{0xAA}, |
| 155 |
accept: true, |
| 156 |
}, |
| 157 |
"Discarded": { |
| 158 |
packet: []byte{0x00}, |
| 159 |
accept: false, |
| 160 |
}, |
| 161 |
} |
| 162 |
|
| 163 |
for name, testCase := range testCases { |
| 164 |
testCase := testCase |
| 165 |
t.Run(name, func(t *testing.T) { |
| 166 |
network, addr := getConfig() |
| 167 |
listener, err := (&ListenConfig{ |
| 168 |
AcceptFilter: func(pkt []byte) bool { |
| 169 |
return pkt[0] == 0xAA |
| 170 |
}, |
| 171 |
}).Listen(network, addr) |
| 172 |
assert.NoError(t, err) |
| 173 |
|
| 174 |
var wgAcceptLoop sync.WaitGroup |
| 175 |
wgAcceptLoop.Add(1) |
| 176 |
defer func() { |
| 177 |
assert.NoError(t, listener.Close()) |
| 178 |
wgAcceptLoop.Wait() |
| 179 |
}() |
| 180 |
aAddr, ok := listener.Addr().(*net.UDPAddr) |
| 181 |
assert.True(t, ok) |
| 182 |
conn, err := net.DialUDP(network, nil, aAddr) |
| 183 |
assert.NoError(t, err) |
| 184 |
|
| 185 |
_, err = conn.Write(testCase.packet) |
| 186 |
assert.NoError(t, err) |
| 187 |
|
| 188 |
defer func() { |
| 189 |
assert.NoError(t, conn.Close()) |
| 190 |
}() |
| 191 |
|
| 192 |
chAccepted := make(chan struct{}) |
| 193 |
go func() { |
| 194 |
defer wgAcceptLoop.Done() |
| 195 |
|
| 196 |
conn, _, aArr := listener.Accept() |
| 197 |
if aArr != nil { |
| 198 |
assert.ErrorIs(t, aArr, ErrClosedListener) |
| 199 |
|
| 200 |
return |
| 201 |
} |
| 202 |
close(chAccepted) |
| 203 |
assert.NoError(t, conn.Close()) |
| 204 |
}() |
| 205 |
|
| 206 |
var accepted bool |
| 207 |
select { |
| 208 |
case <-chAccepted: |
| 209 |
accepted = true |
| 210 |
case <-time.After(10 * time.Millisecond): |
| 211 |
} |
| 212 |
|
| 213 |
assert.Equal(t, testCase.accept, accepted) |
| 214 |
}) |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func TestListenerConcurrent(t *testing.T) { //nolint:gocyclo |
| 219 |
// Limit runtime in case of deadlocks |
| 220 |
lim := test.TimeOut(time.Second * 20) |
| 221 |
defer lim.Stop() |
| 222 |
|
| 223 |
// Check for leaking routines |
| 224 |
report := test.CheckRoutines(t) |
| 225 |
defer report() |
| 226 |
|
| 227 |
const backlog = 2 |
| 228 |
|
| 229 |
network, addr := getConfig() |
| 230 |
listener, err := (&ListenConfig{ |
| 231 |
Backlog: backlog, |
| 232 |
}).Listen(network, addr) |
| 233 |
assert.NoError(t, err) |
| 234 |
|
| 235 |
for i := 0; i < backlog+1; i++ { |
| 236 |
addr, ok := listener.Addr().(*net.UDPAddr) |
| 237 |
assert.True(t, ok) |
| 238 |
conn, dErr := net.DialUDP(network, nil, addr) |
| 239 |
if dErr != nil { |
| 240 |
assert.Fail(t, "Failed to dial UDP: %v", dErr) |
| 241 |
|
| 242 |
continue |
| 243 |
} |
| 244 |
_, wErr := conn.Write([]byte{byte(i)}) |
| 245 |
assert.NoError(t, wErr) |
| 246 |
assert.NoError(t, conn.Close()) |
| 247 |
} |
| 248 |
|
| 249 |
time.Sleep(100 * time.Millisecond) // Wait all packets being processed by readLoop |
| 250 |
|
| 251 |
for i := 0; i < backlog; i++ { |
| 252 |
conn, _, lErr := listener.Accept() |
| 253 |
if lErr != nil { |
| 254 |
assert.Fail(t, "Failed to accept connection: %v", lErr) |
| 255 |
|
| 256 |
continue |
| 257 |
} |
| 258 |
b := make([]byte, 1) |
| 259 |
n, _, lErr := conn.ReadFrom(b) |
| 260 |
assert.NoError(t, lErr) |
| 261 |
assert.Equal(t, b[:n], []byte{byte(i)}) |
| 262 |
assert.NoError(t, conn.Close()) |
| 263 |
} |
| 264 |
|
| 265 |
var wg sync.WaitGroup |
| 266 |
wg.Add(1) |
| 267 |
go func() { |
| 268 |
defer wg.Done() |
| 269 |
conn, _, lErr := listener.Accept() |
| 270 |
assert.ErrorIs(t, lErr, ErrClosedListener) |
| 271 |
if lErr == nil { |
| 272 |
assert.NoError(t, conn.Close()) |
| 273 |
} |
| 274 |
}() |
| 275 |
|
| 276 |
time.Sleep(100 * time.Millisecond) // Last Accept should be discarded |
| 277 |
assert.NoError(t, listener.Close()) |
| 278 |
|
| 279 |
wg.Wait() |
| 280 |
} |
| 281 |
|
| 282 |
func pipe() (dtlsnet.PacketListener, net.PacketConn, *net.UDPConn, error) { |
| 283 |
// Start listening |
| 284 |
network, addr := getConfig() |
| 285 |
listener, err := Listen(network, addr) |
| 286 |
if err != nil { |
| 287 |
return nil, nil, nil, fmt.Errorf("failed to listen: %w", err) |
| 288 |
} |
| 289 |
|
| 290 |
// Open a connection |
| 291 |
var dConn *net.UDPConn |
| 292 |
dAddr, ok := listener.Addr().(*net.UDPAddr) |
| 293 |
if !ok { |
| 294 |
return nil, nil, nil, errUDPCastFailed |
| 295 |
} |
| 296 |
dConn, err = net.DialUDP(network, nil, dAddr) |
| 297 |
if err != nil { |
| 298 |
return nil, nil, nil, fmt.Errorf("failed to dial: %w", err) |
| 299 |
} |
| 300 |
|
| 301 |
// Write to the connection to initiate it |
| 302 |
handshake := "hello" |
| 303 |
_, err = dConn.Write([]byte(handshake)) |
| 304 |
if err != nil { |
| 305 |
return nil, nil, nil, fmt.Errorf("failed to write to dialed Conn: %w", err) |
| 306 |
} |
| 307 |
|
| 308 |
// Accept the connection |
| 309 |
var lConn net.PacketConn |
| 310 |
lConn, _, err = listener.Accept() |
| 311 |
if err != nil { |
| 312 |
return nil, nil, nil, fmt.Errorf("failed to accept Conn: %w", err) |
| 313 |
} |
| 314 |
|
| 315 |
var n int |
| 316 |
buf := make([]byte, len(handshake)) |
| 317 |
if n, _, err = lConn.ReadFrom(buf); err != nil { |
| 318 |
return nil, nil, nil, fmt.Errorf("failed to read handshake: %w", err) |
| 319 |
} |
| 320 |
|
| 321 |
result := string(buf[:n]) |
| 322 |
if handshake != result { |
| 323 |
return nil, nil, nil, fmt.Errorf("%w: %s != %s", errHandshakeFailed, handshake, result) |
| 324 |
} |
| 325 |
|
| 326 |
return listener, lConn, dConn, nil |
| 327 |
} |
| 328 |
|
| 329 |
func getConfig() (string, *net.UDPAddr) { |
| 330 |
return "udp", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 0} |
| 331 |
} |
| 332 |
|
| 333 |
func TestConnClose(t *testing.T) { //nolint:cyclop |
| 334 |
lim := test.TimeOut(time.Second * 5) |
| 335 |
defer lim.Stop() |
| 336 |
|
| 337 |
t.Run("Close", func(t *testing.T) { |
| 338 |
// Check for leaking routines |
| 339 |
report := test.CheckRoutines(t) |
| 340 |
defer report() |
| 341 |
|
| 342 |
udpListener, ca, cb, errPipe := pipe() |
| 343 |
assert.NoError(t, errPipe) |
| 344 |
assert.NoError(t, ca.Close()) |
| 345 |
assert.NoError(t, cb.Close()) |
| 346 |
assert.NoError(t, udpListener.Close()) |
| 347 |
}) |
| 348 |
t.Run("CloseError1", func(t *testing.T) { |
| 349 |
// Check for leaking routines |
| 350 |
report := test.CheckRoutines(t) |
| 351 |
defer report() |
| 352 |
|
| 353 |
udpListener, ca, cb, errPipe := pipe() |
| 354 |
assert.NoError(t, errPipe) |
| 355 |
|
| 356 |
// Close l.pConn to inject error. |
| 357 |
listener, ok := udpListener.(*listener) |
| 358 |
assert.True(t, ok) |
| 359 |
assert.NoError(t, listener.pConn.Close()) |
| 360 |
assert.NoError(t, cb.Close()) |
| 361 |
assert.NoError(t, ca.Close()) |
| 362 |
assert.Error(t, udpListener.Close()) |
| 363 |
}) |
| 364 |
t.Run("CloseError2", func(t *testing.T) { |
| 365 |
// Check for leaking routines |
| 366 |
report := test.CheckRoutines(t) |
| 367 |
defer report() |
| 368 |
|
| 369 |
l, ca, cb, errPipe := pipe() |
| 370 |
assert.NoError(t, errPipe) |
| 371 |
|
| 372 |
// Close l.pConn to inject error. |
| 373 |
listener, ok := l.(*listener) |
| 374 |
assert.True(t, ok) |
| 375 |
assert.NoError(t, listener.pConn.Close()) |
| 376 |
assert.NoError(t, cb.Close()) |
| 377 |
assert.NoError(t, l.Close()) |
| 378 |
assert.Error(t, ca.Close()) |
| 379 |
}) |
| 380 |
t.Run("CancelRead", func(t *testing.T) { |
| 381 |
// Limit runtime in case of deadlocks |
| 382 |
lim := test.TimeOut(time.Second * 5) |
| 383 |
defer lim.Stop() |
| 384 |
|
| 385 |
// Check for leaking routines |
| 386 |
report := test.CheckRoutines(t) |
| 387 |
defer report() |
| 388 |
|
| 389 |
listener, ca, cb, errPipe := pipe() |
| 390 |
assert.NoError(t, errPipe) |
| 391 |
|
| 392 |
errC := make(chan error, 1) |
| 393 |
go func() { |
| 394 |
buf := make([]byte, 1024) |
| 395 |
// This read will block because we don't write on the other side. |
| 396 |
// Calling Close must unblock the call. |
| 397 |
_, _, err := ca.ReadFrom(buf) |
| 398 |
errC <- err |
| 399 |
}() |
| 400 |
|
| 401 |
assert.NoError(t, ca.Close()) // Trigger Read cancellation. |
| 402 |
|
| 403 |
// Main test condition, Read should return |
| 404 |
// after ca.Close() by closing the buffer. |
| 405 |
assert.ErrorIs(t, <-errC, io.EOF) |
| 406 |
assert.NoError(t, cb.Close()) |
| 407 |
assert.NoError(t, listener.Close()) |
| 408 |
}) |
| 409 |
} |
| 410 |
|
| 411 |
func TestListenerCustomConnIDs(t *testing.T) { //nolint:gocyclo,cyclop,maintidx |
| 412 |
const helloPayload, setPayload = "hello", "set" |
| 413 |
const serverCount, clientCount = 5, 20 |
| 414 |
// Limit runtime in case of deadlocks. |
| 415 |
lim := test.TimeOut(time.Second * 20) |
| 416 |
defer lim.Stop() |
| 417 |
|
| 418 |
// Check for leaking routines. |
| 419 |
report := test.CheckRoutines(t) |
| 420 |
defer report() |
| 421 |
|
| 422 |
type pkt struct { |
| 423 |
ID int |
| 424 |
Payload string |
| 425 |
} |
| 426 |
network, addr := getConfig() |
| 427 |
listener, err := (&ListenConfig{ |
| 428 |
// For all datagrams other than the initial "hello" packet, use the ID |
| 429 |
// to route. |
| 430 |
DatagramRouter: func(buf []byte) (string, bool) { |
| 431 |
var p pkt |
| 432 |
if err := json.Unmarshal(buf, &p); err != nil { |
| 433 |
return "", false |
| 434 |
} |
| 435 |
if p.Payload == helloPayload { |
| 436 |
return "", false |
| 437 |
} |
| 438 |
|
| 439 |
return fmt.Sprint(p.ID), true |
| 440 |
}, |
| 441 |
// Use the outgoing "set" payload to add an identifier for a connection. |
| 442 |
ConnectionIdentifier: func(buf []byte) (string, bool) { |
| 443 |
var p pkt |
| 444 |
if err := json.Unmarshal(buf, &p); err != nil { |
| 445 |
return "", false |
| 446 |
} |
| 447 |
if p.Payload == setPayload { |
| 448 |
return fmt.Sprint(p.ID), true |
| 449 |
} |
| 450 |
|
| 451 |
return "", false |
| 452 |
}, |
| 453 |
}).Listen(network, addr) |
| 454 |
assert.NoError(t, err) |
| 455 |
|
| 456 |
var clientWg sync.WaitGroup |
| 457 |
var phaseOne [5]chan struct{} |
| 458 |
for i := range phaseOne { |
| 459 |
phaseOne[i] = make(chan struct{}) |
| 460 |
} |
| 461 |
var serverWg sync.WaitGroup |
| 462 |
clientMap := map[string]struct{}{} |
| 463 |
var clientMapMu sync.Mutex |
| 464 |
// Start servers. |
| 465 |
for i := 0; i < serverCount; i++ { |
| 466 |
serverWg.Add(1) |
| 467 |
go func() { |
| 468 |
defer serverWg.Done() |
| 469 |
// The first payload from the accepted connection should inform |
| 470 |
// which connection this server is. |
| 471 |
conn, _, err := listener.Accept() |
| 472 |
assert.NoError(t, err) |
| 473 |
|
| 474 |
buf := make([]byte, 100) |
| 475 |
n, raddr, rErr := conn.ReadFrom(buf) |
| 476 |
assert.NoError(t, rErr) |
| 477 |
|
| 478 |
var udpPkt pkt |
| 479 |
assert.NoError(t, json.Unmarshal(buf[:n], &udpPkt)) |
| 480 |
|
| 481 |
// First message should be a hello and custom connection |
| 482 |
// ID function will use remote address as identifier. |
| 483 |
assert.Equal(t, helloPayload, udpPkt.Payload) |
| 484 |
connID := udpPkt.ID |
| 485 |
|
| 486 |
// Send set message to associate ID with this connection. |
| 487 |
buf, err = json.Marshal(&pkt{ |
| 488 |
ID: connID, |
| 489 |
Payload: "set", |
| 490 |
}) |
| 491 |
assert.NoError(t, err) |
| 492 |
|
| 493 |
_, wErr := conn.WriteTo(buf, raddr) |
| 494 |
assert.NoError(t, wErr) |
| 495 |
|
| 496 |
// Signal to the corresponding clients that connection ID has been |
| 497 |
// set. |
| 498 |
close(phaseOne[connID]) |
| 499 |
// Receive packets, ensuring that each one came from a different |
| 500 |
// client remote address and has a unique payload. |
| 501 |
for j := 0; j < clientCount/serverCount; j++ { |
| 502 |
buf := make([]byte, 100) |
| 503 |
n, _, err := conn.ReadFrom(buf) |
| 504 |
assert.NoError(t, err) |
| 505 |
|
| 506 |
var udpPkt pkt |
| 507 |
assert.NoError(t, json.Unmarshal(buf[:n], &udpPkt)) |
| 508 |
assert.Equal(t, connID, udpPkt.ID) |
| 509 |
|
| 510 |
// Ensure we only ever receive one message from a given client. |
| 511 |
clientMapMu.Lock() |
| 512 |
_, exists := clientMap[udpPkt.Payload] |
| 513 |
assert.Falsef(t, exists, "Multiple messages from single client %s", udpPkt.Payload) |
| 514 |
clientMap[udpPkt.Payload] = struct{}{} |
| 515 |
clientMapMu.Unlock() |
| 516 |
} |
| 517 |
assert.NoError(t, conn.Close()) |
| 518 |
}() |
| 519 |
} |
| 520 |
|
| 521 |
// Start a client per server to send initial "hello" message and receive a |
| 522 |
// "set" message. |
| 523 |
for i := 0; i < serverCount; i++ { |
| 524 |
clientWg.Add(1) |
| 525 |
go func(connID int) { |
| 526 |
defer clientWg.Done() |
| 527 |
addr, ok := listener.Addr().(*net.UDPAddr) |
| 528 |
assert.True(t, ok) |
| 529 |
conn, dErr := net.DialUDP(network, nil, addr) |
| 530 |
assert.NoError(t, dErr) |
| 531 |
|
| 532 |
hbuf, err := json.Marshal(&pkt{ |
| 533 |
ID: connID, |
| 534 |
Payload: helloPayload, |
| 535 |
}) |
| 536 |
assert.NoError(t, err) |
| 537 |
|
| 538 |
_, wErr := conn.Write(hbuf) |
| 539 |
assert.NoError(t, wErr) |
| 540 |
|
| 541 |
var udpPacket pkt |
| 542 |
buf := make([]byte, 100) |
| 543 |
n, err := conn.Read(buf) |
| 544 |
assert.NoError(t, err) |
| 545 |
|
| 546 |
assert.NoError(t, json.Unmarshal(buf[:n], &udpPacket)) |
| 547 |
|
| 548 |
// Second message should be a set and custom connection identifier |
| 549 |
// function will update the connection ID from remote address to the |
| 550 |
// supplied ID. |
| 551 |
assert.Equal(t, "set", udpPacket.Payload) |
| 552 |
|
| 553 |
// Ensure the connection ID matches what the "hello" message |
| 554 |
// indicated. |
| 555 |
assert.Equal(t, connID, udpPacket.ID) |
| 556 |
|
| 557 |
// Close connection. We will reconnect from a different remote |
| 558 |
// address using the same connection ID. |
| 559 |
assert.NoError(t, conn.Close()) |
| 560 |
}(i) |
| 561 |
} |
| 562 |
|
| 563 |
// Spawn clients sending to server connections. |
| 564 |
for i := 1; i <= clientCount; i++ { |
| 565 |
clientWg.Add(1) |
| 566 |
go func(connID int, clientID string) { |
| 567 |
defer clientWg.Done() |
| 568 |
// Ensure that we are using a connection ID for packet |
| 569 |
// routing prior to sending any messages. |
| 570 |
<-phaseOne[connID] |
| 571 |
addr, ok := listener.Addr().(*net.UDPAddr) |
| 572 |
assert.True(t, ok) |
| 573 |
conn, dErr := net.DialUDP(network, nil, addr) |
| 574 |
assert.NoError(t, dErr) |
| 575 |
|
| 576 |
// Send a packet with a connection ID and this client's unique ID. |
| 577 |
buf, err := json.Marshal(&pkt{ |
| 578 |
ID: connID, |
| 579 |
Payload: clientID, |
| 580 |
}) |
| 581 |
assert.NoError(t, err) |
| 582 |
|
| 583 |
_, wErr := conn.Write(buf) |
| 584 |
assert.NoError(t, wErr) |
| 585 |
assert.NoError(t, conn.Close()) |
| 586 |
}(i%serverCount, fmt.Sprintf("client-%d", i)) |
| 587 |
} |
| 588 |
|
| 589 |
// Wait for clients to exit. |
| 590 |
clientWg.Wait() |
| 591 |
// Wait for servers to exit. |
| 592 |
serverWg.Wait() |
| 593 |
assert.NoError(t, listener.Close()) |
| 594 |
} |