From b2fbaea18c565cc16f3017e16d396c12f6b7c06c Mon Sep 17 00:00:00 2001 From: Ryan Uber Date: Mon, 12 Jan 2015 16:05:41 -0800 Subject: [PATCH] agent: make dns randomization test more reliable --- command/agent/dns_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/command/agent/dns_test.go b/command/agent/dns_test.go index dbd7fad745..76af3685cc 100644 --- a/command/agent/dns_test.go +++ b/command/agent/dns_test.go @@ -1001,10 +1001,11 @@ func TestDNS_ServiceLookup_Randomize(t *testing.T) { } } - // Ensure the response is randomized each time + // Ensure the response is randomized each time. + loops := 5 uniques := map[string]struct{}{} addr, _ := srv.agent.config.ClientListener("", srv.agent.config.Ports.DNS) - for i := 0; i < 5; i++ { + for i := 0; i < loops; i++ { m := new(dns.Msg) m.SetQuestion("web.service.consul.", dns.TypeANY) @@ -1032,12 +1033,17 @@ func TestDNS_ServiceLookup_Randomize(t *testing.T) { } nameS := strings.Join(names, "|") - // Check if unique - if _, ok := uniques[nameS]; ok { - t.Fatalf("non-unique response: %v", nameS) - } + // Tally the results uniques[nameS] = struct{}{} } + + // Give some wiggle room. Since the responses are randomized and there + // is a finite number of combinations, requiring 0 duplicates every + // test run eventually gives us failures. + if len(uniques) < (loops - 1) { + t.Fatalf("unique response ratio too low: %d/%d\n%v", + len(uniques), loops, uniques) + } } func TestDNS_ServiceLookup_CNAME(t *testing.T) {