git » dnss » commit ad1e9fd

dnss_test: Enforce test servers ordering to prevent races

author Alberto Bertogli
2018-07-21 12:52:02 UTC
committer Alberto Bertogli
2018-07-21 12:52:02 UTC
parent 811cfee2e7043e4a342c869147b64449aa1aa661

dnss_test: Enforce test servers ordering to prevent races

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.

dnss_test.go +22 -21

diff --git a/dnss_test.go b/dnss_test.go
index 38fcc51..f70e059 100644
--- a/dnss_test.go
+++ b/dnss_test.go
@@ -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 {
@@ -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