|
6 | 6 | "strings" |
7 | 7 | "testing" |
8 | 8 | "time" |
| 9 | + |
| 10 | + "github.com/TMHSDigital/subenum/internal/dnstest" |
9 | 11 | ) |
10 | 12 |
|
11 | 13 | func TestParseResolverList(t *testing.T) { |
@@ -321,3 +323,84 @@ func TestPoolPickPrefersAvoidedHealthyOverBenched(t *testing.T) { |
321 | 323 | t.Fatalf("all benched: pick = %s, want the soonest-unbenched member", got.Addr) |
322 | 324 | } |
323 | 325 | } |
| 326 | + |
| 327 | +// TestPoolBenchesDeadMemberFast covers #463: a member that fails a few |
| 328 | +// lookups in a row is benched without waiting for a window of 20, each |
| 329 | +// repeat bench lasts twice the one before up to a cap, and a member that |
| 330 | +// answers again starts over at the shortest bench. |
| 331 | +func TestPoolBenchesDeadMemberFast(t *testing.T) { |
| 332 | + p := NewPool([]string{"192.0.2.1:53", "192.0.2.2:53"}, time.Second) |
| 333 | + now := time.Unix(0, 0) |
| 334 | + p.now = func() time.Time { return now } |
| 335 | + dead := p.members[0] |
| 336 | + benchedFor := func() time.Duration { |
| 337 | + dead.mu.Lock() |
| 338 | + defer dead.mu.Unlock() |
| 339 | + return dead.until.Sub(now) |
| 340 | + } |
| 341 | + failStreak := func() { |
| 342 | + for range poolBenchStreak { |
| 343 | + p.report(dead, OutcomeTimeout) |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + failStreak() |
| 348 | + if st := p.Stats()[0]; st.Benched != 1 || benchedFor() != poolBenchFor { |
| 349 | + t.Fatalf("after %d timeouts: benched %d for %v, want once for %v", poolBenchStreak, st.Benched, benchedFor(), poolBenchFor) |
| 350 | + } |
| 351 | + // Lookups still in flight when it was benched do not bench it again. |
| 352 | + p.report(dead, OutcomeTimeout) |
| 353 | + if st := p.Stats()[0]; st.Benched != 1 { |
| 354 | + t.Fatalf("a late failure re-benched it: %d benches", st.Benched) |
| 355 | + } |
| 356 | + |
| 357 | + want := poolBenchFor |
| 358 | + for range 5 { |
| 359 | + now = now.Add(benchedFor() + time.Second) |
| 360 | + failStreak() |
| 361 | + want = min(2*want, poolBenchMax) |
| 362 | + if got := benchedFor(); got != want { |
| 363 | + t.Fatalf("repeat bench lasts %v, want %v", got, want) |
| 364 | + } |
| 365 | + } |
| 366 | + if st := p.Stats()[0]; st.Benched != 6 { |
| 367 | + t.Errorf("benched %d times, want 6", st.Benched) |
| 368 | + } |
| 369 | + |
| 370 | + // An answer resets the backoff. |
| 371 | + now = now.Add(benchedFor() + time.Second) |
| 372 | + p.report(dead, OutcomeNXDomain) |
| 373 | + failStreak() |
| 374 | + if got := benchedFor(); got != poolBenchFor { |
| 375 | + t.Errorf("bench after an answer lasts %v, want %v again", got, poolBenchFor) |
| 376 | + } |
| 377 | + |
| 378 | + // Failures that are not in a row still bench through the window. |
| 379 | + other := p.members[1] |
| 380 | + for i := range poolHealthMinLookups { |
| 381 | + o := OutcomeTimeout |
| 382 | + if i%3 == 2 { |
| 383 | + o = OutcomeFound |
| 384 | + } |
| 385 | + p.report(other, o) |
| 386 | + } |
| 387 | + if st := p.Stats()[1]; st.Benched != 1 { |
| 388 | + t.Errorf("a member failing two lookups in three: benched %d times, want 1 by the window", st.Benched) |
| 389 | + } |
| 390 | +} |
| 391 | + |
| 392 | +// TestPoolFailedCanaryCounts covers #463: a member that never answers its |
| 393 | +// canary checks is benched by them, since each one fails like a lookup. |
| 394 | +func TestPoolFailedCanaryCounts(t *testing.T) { |
| 395 | + srv := dnstest.Start(t, func(dnstest.Query) dnstest.Reply { return dnstest.Dropped }) |
| 396 | + p := NewPool([]string{srv.Addr}, 50*time.Millisecond) |
| 397 | + p.AddCanary("www.example.com") |
| 398 | + l := Lookup{Timeout: 50 * time.Millisecond, Types: []string{"A"}} |
| 399 | + m := p.members[0] |
| 400 | + for range poolBenchStreak { |
| 401 | + p.checkCanary(context.Background(), m, l) |
| 402 | + } |
| 403 | + if st := p.Stats()[0]; st.Benched != 1 || st.Failed != poolBenchStreak { |
| 404 | + t.Errorf("after %d failed canaries: benched %d, failed %d; want 1 and %d", poolBenchStreak, st.Benched, st.Failed, poolBenchStreak) |
| 405 | + } |
| 406 | +} |
0 commit comments