我得到了一个用 Golang 编写的脚本,我不太明白。我想知道他为什么go server.Start()在剧本里面写?为什么不简单地写server.Start?
package main
import (
"github.com/miekg/dns"
"testing"
"time"
)
const TEST_ADDR = "127.0.0.1:9953"
func TestDNSResponse(t *testing.T) {
server := NewDNSServer(&Config{
dnsAddr: TEST_ADDR,
})
go server.Start()
// Allow some time for server to start
time.Sleep(150 * time.Millisecond)
m := new(dns.Msg)
m.Id = dns.Id()
m.Question = []dns.Question{
dns.Question{"docker.", dns.TypeA, dns.ClassINET},
}
c := new(dns.Client)
_, _, err := c.Exchange(m, TEST_ADDR)
if err != nil {
t.Error("Error response from the server", err)
}
server.Stop()
c = new(dns.Client)
_, _, err = c.Exchange(m, TEST_ADDR)
if err == nil {
t.Error("Server still running but should be shut down.")
}
}
慕森卡
相关分类