carlos / platform Public

test: measure slow witness delay on the tenant clock

Co-Authored-By: GPT-6 Astra <noreply@openai.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 481942f009db8a8c1884acdcef5ee9a13cf8935a parent 44e6be0
3 files changed, +60 −5
  • internal/host/region_ha_g2_loss_helpers_test.go +37 −0
  • internal/host/region_ha_g2_loss_root_linux_test.go +21 −5
  • internal/host/region_ha_g2_root_fixture_test.go +2 −0
diff --git a/internal/host/region_ha_g2_loss_helpers_test.go b/internal/host/region_ha_g2_loss_helpers_test.go
index b76c456ad..ea2a55141 100644
--- a/internal/host/region_ha_g2_loss_helpers_test.go
+++ b/internal/host/region_ha_g2_loss_helpers_test.go
@@ -137,6 +137,39 @@ func TestG2LossRequestCensus(t *testing.T) {
}
}
+func TestG2LossSlowResponseTiming(t *testing.T) {
+ for _, tt := range []struct {
+ name string
+ elapsed time.Duration
+ wait string
+ count int
+ status int
+ wantErr bool
+ }{
+ {"run-3", 20009475044 * time.Nanosecond, "20s", 1, 200, false},
+ {"run-4", 19978219634 * time.Nanosecond, "20s", 1, 200, false},
+ {"early-client", 19 * time.Second, "20s", 1, 200, true},
+ {"delayed-client", 25*time.Second + time.Nanosecond, "20s", 1, 200, true},
+ {"short-fixture-wait", 20 * time.Second, "19.99s", 1, 200, true},
+ {"missing-fixture-wait", 20 * time.Second, "", 1, 200, true},
+ {"invalid-fixture-wait", 20 * time.Second, "invalid", 1, 200, true},
+ {"replay", 20 * time.Second, "20s", 2, 200, true},
+ {"missing-commit", 20 * time.Second, "20s", 0, 200, true},
+ {"refusal", 20 * time.Second, "20s", 1, 503, true},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ response := g2Response{
+ Status: tt.status, Elapsed: tt.elapsed,
+ Headers: http.Header{"X-G2-Slow-Wait": {tt.wait}},
+ Body: []byte(fmt.Sprintf(`{"ref":"holder","pid":7,"cgroup":"carlos-instance","deliveries":{"slow-post":%d,"cancel-post":1}}`, tt.count)),
+ }
+ if err := g2LossSlowResponse(response, "holder"); (err != nil) != tt.wantErr {
+ t.Fatalf("error=%v, wantErr=%v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
func TestG2LossTenantCommitsBeforeSlowHeaders(t *testing.T) {
dir := t.TempDir()
socket, dbpath := filepath.Join(dir, "slow.sock"), filepath.Join(dir, "slow.db")
@@ -171,6 +204,10 @@ func TestG2LossTenantCommitsBeforeSlowHeaders(t *testing.T) {
if response.StatusCode != 200 {
err = fmt.Errorf("status %d", response.StatusCode)
}
+ wait, parseErr := time.ParseDuration(response.Header.Get("X-G2-Slow-Wait"))
+ if parseErr != nil || wait < 20*time.Second {
+ err = fmt.Errorf("fixture wait=%s parse=%v", wait, parseErr)
+ }
}
completed <- err
}()
diff --git a/internal/host/region_ha_g2_loss_root_linux_test.go b/internal/host/region_ha_g2_loss_root_linux_test.go
index 3407c8c82..d55f9c17e 100644
--- a/internal/host/region_ha_g2_loss_root_linux_test.go
+++ b/internal/host/region_ha_g2_loss_root_linux_test.go
@@ -157,6 +157,26 @@ func g2LossCheck(t *testing.T, client *http.Client, name string) {
g2Must(t, g2LossRefusal(response))
}
+func g2LossSlowResponse(response g2Response, ref string) error {
+ delivery, err := g2ServedDelivery(response, ref)
+ if err != nil {
+ return err
+ }
+ wait, err := time.ParseDuration(response.Headers.Get("X-G2-Slow-Wait"))
+ if err != nil || wait < 20*time.Second {
+ return fmt.Errorf("G2_SLOW_POST: fixture wait=%s parse=%v", wait, err)
+ }
+ // The tenant and client run on separate machines. Prove the full wait on
+ // the tenant's own clock; allow 100 ms at the client's lower bound only.
+ if response.Elapsed < 20*time.Second-100*time.Millisecond || response.Elapsed > 25*time.Second {
+ return fmt.Errorf("G2_SLOW_POST: client elapsed=%s fixture wait=%s", response.Elapsed, wait)
+ }
+ if delivery.Deliveries["slow-post"] != 1 {
+ return fmt.Errorf("G2_SLOW_POST: POST deliveries=%v", delivery.Deliveries)
+ }
+ return nil
+}
+
func g2LossSlow(t *testing.T, storage *g2Storage, client, direct *http.Client, ref string) {
t.Helper()
const host = g2AbsentOldHost
@@ -224,11 +244,7 @@ func g2LossSlow(t *testing.T, storage *g2Storage, client, direct *http.Client, r
for i, answer := range []<-chan g2Response{slow, second, probe, next, last} {
response := <-answer
g2LogResponse(t, fmt.Sprintf("slow-%d", i), response)
- delivery, err := g2ServedDelivery(response, ref)
- g2Must(t, err)
- if response.Elapsed < 20*time.Second || response.Elapsed > 25*time.Second || delivery.Deliveries["slow-post"] != 1 {
- t.Fatalf("G2_SLOW_POST: delay or replay violation: %+v %+v", response, delivery)
- }
+ g2Must(t, g2LossSlowResponse(response, ref))
}
final := g2LossRequest(t.Context(), direct, host, "/", "GET", "")
delivery, err := g2ServedDelivery(final, ref)
diff --git a/internal/host/region_ha_g2_root_fixture_test.go b/internal/host/region_ha_g2_root_fixture_test.go
index ef1e17f6d..55d2ef8bf 100644
--- a/internal/host/region_ha_g2_root_fixture_test.go
+++ b/internal/host/region_ha_g2_root_fixture_test.go
@@ -1019,7 +1019,9 @@ func g2Tenant() int {
}
// Fixture-only delay: commit first, then withhold final headers.
if r.URL.Path == "/slow" {
+ started := time.Now()
time.Sleep(20 * time.Second)
+ w.Header().Set("X-G2-Slow-Wait", time.Since(started).String())
}
rows, err := db.Query("SELECT id,deliveries FROM deliveries ORDER BY id")
if err != nil {