From: Simon Pasquier Date: Thu, 3 May 2018 15:20:17 +0000 (+0200) Subject: prober: fix data races X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c750a1520ce8a39cbdbb0ac47376ef0a8b53b41e;p=blackbox_exporter.git prober: fix data races Signed-off-by: Simon Pasquier --- diff --git a/prober/dns_test.go b/prober/dns_test.go index 2e66a5d..e40f431 100644 --- a/prober/dns_test.go +++ b/prober/dns_test.go @@ -34,18 +34,31 @@ var PROTOCOLS = [...]string{"udp", "tcp"} func startDNSServer(protocol string, handler func(dns.ResponseWriter, *dns.Msg)) (*dns.Server, net.Addr) { h := dns.NewServeMux() h.HandleFunc(".", handler) + server := &dns.Server{Addr: ":0", Net: protocol, Handler: h} - go server.ListenAndServe() - // Wait until PacketConn becomes available, but give up after 1 second. - for i := 0; server.PacketConn == nil && i < 200; i++ { - if protocol == "tcp" && server.Listener != nil { - break + if protocol == "udp" { + a, err := net.ResolveUDPAddr(server.Net, server.Addr) + if err != nil { + panic(err) + } + l, err := net.ListenUDP(server.Net, a) + if err != nil { + panic(err) } - if protocol == "udp" && server.PacketConn != nil { - break + server.PacketConn = l + } else { + a, err := net.ResolveTCPAddr(server.Net, server.Addr) + if err != nil { + panic(err) + } + l, err := net.ListenTCP(server.Net, a) + if err != nil { + panic(err) } - time.Sleep(5 * time.Millisecond) + server.Listener = l } + go server.ActivateAndServe() + if protocol == "tcp" { return server, server.Listener.Addr() }