mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 09:32:45 +01:00
Reserve both loopback addresses for notification fallback tests (#3824)
Reserve both loopback addresses for notification fallback tests Allocate the healthy loopback listener first, then reserve the stalled address at the same port. Retry only EADDRINUSE during paired reservation and retain both sockets for the test lifetime. This removes the cross-address allocation race without replacing the real dial path or replaying HTTP requests. Contract-Neutral: Linux-only test fixture allocation changes. Production notification delivery, HTTP fallback semantics, permission boundaries and subsystem contracts remain unchanged.
This commit is contained in:
@@ -4,6 +4,7 @@ package notifications
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -24,17 +25,7 @@ import (
|
||||
// privileged network configuration, or a replacement production dial function.
|
||||
func notificationStalledAddress(t *testing.T) (string, *atomic.Int32) {
|
||||
t.Helper()
|
||||
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = syscall.Close(fd) })
|
||||
require.NoError(t, syscall.Bind(fd, &syscall.SockaddrInet4{Addr: [4]byte{127, 0, 0, 2}}))
|
||||
require.NoError(t, syscall.Listen(fd, 1))
|
||||
address, err := syscall.Getsockname(fd)
|
||||
require.NoError(t, err)
|
||||
port := strconv.Itoa(address.(*syscall.SockaddrInet4).Port)
|
||||
|
||||
healthy, err := net.Listen("tcp4", net.JoinHostPort("127.0.0.1", port))
|
||||
require.NoError(t, err)
|
||||
port, healthy := notificationLoopbackListenerPair(t)
|
||||
var accepted atomic.Int32
|
||||
server := &httptest.Server{Listener: healthy, Config: &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
accepted.Add(1)
|
||||
@@ -76,6 +67,45 @@ func notificationStalledAddress(t *testing.T) (string, *atomic.Int32) {
|
||||
return port, &accepted
|
||||
}
|
||||
|
||||
func notificationLoopbackListenerPair(t *testing.T) (string, net.Listener) {
|
||||
t.Helper()
|
||||
var bindErr error
|
||||
for attempt := 0; attempt < 10; attempt++ {
|
||||
// Allocate on the busy loopback address first. A port allocated on
|
||||
// 127.0.0.2 alone may already be occupied on 127.0.0.1.
|
||||
healthy, err := net.Listen("tcp4", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
port := healthy.Addr().(*net.TCPAddr).Port
|
||||
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
_ = healthy.Close()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
closePair := func() {
|
||||
_ = syscall.Close(fd)
|
||||
_ = healthy.Close()
|
||||
}
|
||||
bindErr = syscall.Bind(fd, &syscall.SockaddrInet4{Addr: [4]byte{127, 0, 0, 2}, Port: port})
|
||||
if bindErr != nil {
|
||||
closePair()
|
||||
// Only retry reservation conflicts. Both sockets stay owned once
|
||||
// the pair is reserved, so there is no close-and-rebind window.
|
||||
if errors.Is(bindErr, syscall.EADDRINUSE) {
|
||||
continue
|
||||
}
|
||||
require.NoError(t, bindErr)
|
||||
}
|
||||
if err := syscall.Listen(fd, 1); err != nil {
|
||||
closePair()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
t.Cleanup(closePair)
|
||||
return strconv.Itoa(port), healthy
|
||||
}
|
||||
t.Fatalf("could not reserve a loopback listener pair after 10 attempts: %v", bindErr)
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func TestQueuedWebhookFallbackVerdictSurvivesRestart(t *testing.T) {
|
||||
port, accepted := notificationStalledAddress(t)
|
||||
dir := t.TempDir()
|
||||
|
||||
Reference in New Issue
Block a user