Module addr

Module addr 

Source
Expand description

Test utilities for allocating unique network addresses.

This module provides thread-safe port allocation for tests using a guard pattern to prevent port reuse race conditions. The design eliminates intra-process races by:

  1. Binding to get a port while holding a TCP listener
  2. Registering the port atomically (while still holding the listener and registry lock)
  3. Only then releasing the listener (port now protected by registry entry)

This ensures no race window between port allocation and registration.

Structs§

PortGuard
A guard that reserves a port in the registry, preventing port reuse until dropped. The guard does NOT hold the actual listener - it just marks the port as reserved so that concurrent calls to next_addr() won’t return the same port.

Functions§

next_addr
next_addr_any
next_addr_for_ip
Allocates a unique port and returns a guard that keeps it reserved.
next_addr_v6