Compare commits

..
Author SHA1 Message Date
sneak b46d41433f rproxy: breaker test no longer needs each failed ask to reach waf (closes #398)
check / check (push) Failing after 5m53s
TestFiveFailedAsksKeepTheNextRequestFromAskingWAF waited for a question
from each of the five failing asks. Under load an ask could time out
before the stand-in for waf saw it, and the test failed with "no
question". It now waits only for each visitor's answer.

Before the sixth request, it checks that rproxy has not written that the
circuit breaker is open, so none of the first five was kept from asking
waf. It checks that the sixth request did not ask by its request ID,
which the app records, so late questions from the failed asks cannot be
mistaken for it.

Model: opus-5-5
2026-11-16 07:27:32 +00:00
3 changed files with 38 additions and 38 deletions

No files matched your search

+1 -1
View File
@@ -12,7 +12,6 @@ require (
github.com/oschwald/maxminddb-golang/v2 v2.7.0 github.com/oschwald/maxminddb-golang/v2 v2.7.0
github.com/prometheus/client_golang v1.24.1 github.com/prometheus/client_golang v1.24.1
github.com/spf13/viper v1.21.0 github.com/spf13/viper v1.21.0
golang.org/x/sys v0.48.0
) )
require ( require (
@@ -48,6 +47,7 @@ require (
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/net v0.60.0 // indirect golang.org/x/net v0.60.0 // indirect
golang.org/x/sync v0.23.0 // indirect golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/text v0.42.0 // indirect golang.org/x/text v0.42.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect google.golang.org/protobuf v1.36.11 // indirect
rsc.io/binaryregexp v0.2.0 // indirect rsc.io/binaryregexp v0.2.0 // indirect
+8 -32
View File
@@ -5,15 +5,12 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/netip" "net/netip"
"runtime"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
"golang.org/x/sys/unix"
"sneak.berlin/go/smallwebwaf/internal/coreruleset" "sneak.berlin/go/smallwebwaf/internal/coreruleset"
) )
@@ -305,24 +302,8 @@ func worstCaseBodies() []visitorRequest {
} }
} }
// threadCPUTime returns the processor time the calling thread has used. // inspectionTime returns how long crs takes to inspect tc, and fails t unless
func threadCPUTime(t *testing.T) time.Duration { // it reads all of tc's body and Coraza parses it.
t.Helper()
var now unix.Timespec
err := unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &now)
if err != nil {
t.Fatalf("read the thread's processor time: %v", err)
}
return time.Duration(now.Nano())
}
// inspectionTime returns the processor time crs takes to inspect tc, and
// fails t unless it reads all of tc's body and Coraza parses it. Inspect does
// its work on the calling goroutine, which stays on one thread meanwhile, so
// that thread's processor time is the inspection's.
func inspectionTime( func inspectionTime(
t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest, t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest,
) time.Duration { ) time.Duration {
@@ -337,14 +318,11 @@ func inspectionTime(
req.Header.Set("Content-Type", tc.contentType) req.Header.Set("Content-Type", tc.contentType)
} }
runtime.LockOSThread() began := time.Now()
defer runtime.UnlockOSThread()
began := threadCPUTime(t)
result, read, _ := crs.Inspect( result, read, _ := crs.Inspect(
req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body), req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body),
) )
took := threadCPUTime(t) - began took := time.Since(began)
if string(read) != tc.body { if string(read) != tc.body {
t.Errorf("read %d bytes of %d", len(read), len(tc.body)) t.Errorf("read %d bytes of %d", len(read), len(tc.body))
@@ -358,12 +336,10 @@ func inspectionTime(
return took return took
} }
// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime measures processor // TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so
// time, not time on the clock, so that the time other work on the host holds // that the tests running in parallel do not slow it. Other work on the host
// the processor does not count. It runs one at a time, so that the tests // can still slow one try, so a request fails only when three tries in a row
// running in parallel do not slow it. Other work can still slow one try a // take too long.
// little, by sharing the processor's caches, so a request fails only when
// three tries in a row take too long.
// //
//nolint:paralleltest // one at a time, as above //nolint:paralleltest // one at a time, as above
func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) { func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
+29 -5
View File
@@ -1424,7 +1424,14 @@ func TestFiveFailedAsksKeepTheNextRequestFromAskingWAF(t *testing.T) {
failure(w, r) failure(w, r)
}, },
) )
app := startApp(t, greet) nextID := make(chan string, 1)
app := startApp(t, func(w http.ResponseWriter, r *http.Request) {
if answering.Load() {
nextID <- r.Header.Get(requestIDHeader)
}
greet(w, r)
})
addr, out := startRProxy( addr, out := startRProxy(
t, t,
standIn.URL, standIn.URL,
@@ -1434,19 +1441,36 @@ func TestFiveFailedAsksKeepTheNextRequestFromAskingWAF(t *testing.T) {
}, },
) )
// An ask can time out before it reaches the stand-in, so these
// may leave fewer than five questions, or leave some late.
for range 5 { for range 5 {
wantGreeting(t, get(t, addr, "/")) wantGreeting(t, get(t, addr, "/"))
standIn.question(t) }
// rproxy writes this line before it answers a request it kept
// from asking waf, so no wait is needed.
if out.has("the circuit breaker is open") {
t.Error("the circuit breaker opened before five asks failed")
} }
answering.Store(true) answering.Store(true)
wantGreeting(t, get(t, addr, "/")) wantGreeting(t, get(t, addr, "/"))
out.waitFor(t, "the circuit breaker is open") out.waitFor(t, "the circuit breaker is open")
// Had the next request asked waf, its question would have reached
// the stand-in before the request reached the app.
var id string
select { select {
case <-standIn.questions: case id = <-nextID:
t.Error("waf was asked, want it not to be") default:
case <-time.After(10 * pollInterval): t.Fatal("the app did not get the next request")
}
for len(standIn.questions) > 0 {
asked := <-standIn.questions
if asked.header.Get(requestIDHeader) == id {
t.Error("waf was asked, want it not to be")
}
} }
}) })
} }