package test import ( "net" "sync" "testing" "time" "github.com/miekg/dns" ) func TCPServer(t *testing.T, laddr string) (*dns.Server, string, error) { l, err := net.Listen("tcp", laddr) if err != nil { return nil, "", err } server := &dns.Server{Listener: l, ReadTimeout: time.Hour, WriteTimeout: time.Hour} waitLock := sync.Mutex{} waitLock.Lock() server.NotifyStartedFunc = func() { t.Logf("started TCP server on %s", l.Addr()); waitLock.Unlock() } go func() { server.ActivateAndServe() l.Close() }() waitLock.Lock() return server, l.Addr().String(), nil } func UDPServer(t *testing.T, laddr string) (*dns.Server, string, error) { pc, err := net.ListenPacket("udp", laddr) if err != nil { return nil, "", err } server := &dns.Server{PacketConn: pc, ReadTimeout: time.Hour, WriteTimeout: time.Hour} waitLock := sync.Mutex{} waitLock.Lock() server.NotifyStartedFunc = func() { t.Logf("started UDP server on %s", pc.LocalAddr()); waitLock.Unlock() } go func() { server.ActivateAndServe() pc.Close() }() waitLock.Lock() return server, pc.LocalAddr().String(), nil }