prober: fix data races
authorSimon Pasquier <spasquie@redhat.com>
Thu, 3 May 2018 15:20:17 +0000 (17:20 +0200)
committerBrian Brazil <brian.brazil@robustperception.io>
Tue, 22 May 2018 20:09:55 +0000 (21:09 +0100)
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
prober/dns_test.go

index 2e66a5d3b4fc53fb985d2068e74fec734c5b7aad..e40f431c5f117a12dde9681a0f09093cd24cd579 100644 (file)
@@ -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()
        }