Skip to content

Commit

Permalink
dnss_test: Enforce test servers ordering to prevent races
Browse files Browse the repository at this point in the history
The DNS-to-HTTP server autodetection requires the HTTP server to be up;
however, the tests don't enforce this. Under some circumstances, the
autodetection can time out, causing the tests to fail

To fix that, this patch enforces the ordering of startup of the test
servers, so that we can be sure the HTTP server started and is serving
_before_ the DNS-to-HTTP server comes up and tries to autodetect.
  • Loading branch information
albertito committed Jul 21, 2018
1 parent 811cfee commit ad1e9fd
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions dnss_test.go
Expand Up @@ -45,6 +45,26 @@ func Setup(tb testing.TB, mode string) string {
HTTPSToDNSAddr := testutil.GetFreePort()
DNSServerAddr := testutil.GetFreePort()

// HTTPS to DNS server.
htod := httpserver.Server{
Addr: HTTPSToDNSAddr,
Upstream: DNSServerAddr,
}
httpserver.InsecureForTesting = true
go htod.ListenAndServe()

// Test DNS server.
go testutil.ServeTestDNSServer(DNSServerAddr, handleTestDNS)

// Wait for the above to start; the DNS to HTTPS server below needs them
// up for protocol autodetection.
if err := testutil.WaitForHTTPServer(HTTPSToDNSAddr); err != nil {
tb.Fatalf("Error waiting for HTTPS to DNS server to start: %v", err)
}
if err := testutil.WaitForDNSServer(DNSServerAddr); err != nil {
tb.Fatalf("Error waiting for testing DNS server to start: %v", err)
}

// DNS to HTTPS server.
HTTPSToDNSURL, err := url.Parse("http://" + HTTPSToDNSAddr + "/resolve")
if err != nil {
Expand All @@ -66,27 +86,8 @@ func Setup(tb testing.TB, mode string) string {
dtoh := dnsserver.New(DNSToHTTPSAddr, r, "")
go dtoh.ListenAndServe()

// HTTPS to DNS server.
htod := httpserver.Server{
Addr: HTTPSToDNSAddr,
Upstream: DNSServerAddr,
}
httpserver.InsecureForTesting = true
go htod.ListenAndServe()

// Test DNS server.
go testutil.ServeTestDNSServer(DNSServerAddr, handleTestDNS)

// Wait for the servers to start up.
err1 := testutil.WaitForDNSServer(DNSToHTTPSAddr)
err2 := testutil.WaitForHTTPServer(HTTPSToDNSAddr)
err3 := testutil.WaitForDNSServer(DNSServerAddr)
if err1 != nil || err2 != nil || err3 != nil {
tb.Logf("Error waiting for the test servers to start:\n")
tb.Logf(" DNS to HTTPS: %v\n", err1)
tb.Logf(" HTTPS to DNS: %v\n", err2)
tb.Logf(" DNS server: %v\n", err3)
tb.Fatalf("Check the INFO logs for more details\n")
if err := testutil.WaitForDNSServer(DNSToHTTPSAddr); err != nil {
tb.Fatalf("Error waiting for DNS to HTTPS server to start: %v", err)
}

return DNSToHTTPSAddr
Expand Down

0 comments on commit ad1e9fd

Please sign in to comment.