mirror of
https://github.com/coredns/coredns.git
synced 2026-07-15 20:20:10 -04:00
test: poll instead of fixed sleeps across several tests (#8275)
Several tests slept a fixed duration then took a single snapshot of an async result, racing whatever they waited on: - forward health tests waited 20ms for the health-check goroutine to bump an atomic counter, - the auto plugin tests (dns + metrics) waited 50-110ms for a file-watch reload to be picked up, - the file ZoneReload test waited 30ms (self-described as could still be racy) for a reload, - the overloaded health test slept 1s for its background goroutine to fire its first request. Replace each with a bounded poll of the actual condition (the atomic counter, a dns.Exchange response, a metrics scrape, z.ApexIfDefined, or a channel signalled by the test's own handler) so they pass as soon as the awaited state is reached and no longer flake when it is slower than the fixed wait. Test-only. Signed-off-by: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
This commit is contained in:
@@ -41,11 +41,15 @@ func TestAuto(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
time.Sleep(50 * time.Millisecond) // wait for it to be picked up
|
||||
|
||||
resp, err = dns.Exchange(m, udp)
|
||||
if err != nil {
|
||||
t.Fatal("Expected to receive reply, but didn't")
|
||||
for start := time.Now(); time.Since(start) < 5*time.Second; {
|
||||
resp, err = dns.Exchange(m, udp)
|
||||
if err != nil {
|
||||
t.Fatal("Expected to receive reply, but didn't")
|
||||
}
|
||||
if len(resp.Answer) == 1 {
|
||||
break
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if len(resp.Answer) != 1 {
|
||||
t.Fatalf("Expected 1 RR in the answer section, got %d", len(resp.Answer))
|
||||
@@ -54,10 +58,15 @@ func TestAuto(t *testing.T) {
|
||||
// Remove db.example.org again.
|
||||
os.Remove(filepath.Join(tmpdir, "db.example.org"))
|
||||
|
||||
time.Sleep(50 * time.Millisecond) // wait for it to be picked up
|
||||
resp, err = dns.Exchange(m, udp)
|
||||
if err != nil {
|
||||
t.Fatal("Expected to receive reply, but didn't")
|
||||
for start := time.Now(); time.Since(start) < 5*time.Second; {
|
||||
resp, err = dns.Exchange(m, udp)
|
||||
if err != nil {
|
||||
t.Fatal("Expected to receive reply, but didn't")
|
||||
}
|
||||
if resp.Rcode == dns.RcodeRefused {
|
||||
break
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if resp.Rcode != dns.RcodeRefused {
|
||||
t.Fatalf("Expected reply to be REFUSED, got %d", resp.Rcode)
|
||||
|
||||
Reference in New Issue
Block a user