From d2d22699f1377fa7af30d72d332a1a5c3917ffd2 Mon Sep 17 00:00:00 2001 From: Naitik Shah Date: Mon, 15 Jun 2015 20:27:40 +0000 Subject: [PATCH] use TestMain and remove external binary --- gracehttp/http_test.go | 30 ++----------------- .../testserver.go => testbin_test.go} | 23 +++++++++++--- 2 files changed, 22 insertions(+), 31 deletions(-) rename gracehttp/{testserver/testserver.go => testbin_test.go} (92%) diff --git a/gracehttp/http_test.go b/gracehttp/http_test.go index 6d9c1fe..4c9a70b 100644 --- a/gracehttp/http_test.go +++ b/gracehttp/http_test.go @@ -17,16 +17,10 @@ import ( "time" "github.com/facebookgo/freeport" - "github.com/facebookgo/tool" ) -var ( - // Debug logging. - debugLog = flag.Bool("debug", false, "enable debug logging") - testserverCommand = &tool.CommandBuild{ - ImportPath: "github.com/facebookgo/grace/gracehttp/testserver", - } -) +// Debug logging. +var debugLog = flag.Bool("debug", false, "enable debug logging") func debug(format string, a ...interface{}) { if *debugLog { @@ -34,19 +28,6 @@ func debug(format string, a ...interface{}) { } } -var ( - buildOut string - buildErr error - buildOnce sync.Once -) - -// The response from the test server. -type response struct { - Sleep time.Duration - Pid int - Error string -} - // State for the test run. type harness struct { T *testing.T // The test instance. @@ -78,13 +59,8 @@ func (h *harness) setupAddr() { // Start a fresh server and wait for pid updates on restart. func (h *harness) Start() { - bin, err := testserverCommand.Build() - if err != nil { - h.T.Fatalf("build error: %s", err) - } - h.setupAddr() - cmd := exec.Command(bin, "-http", h.httpAddr, "-https", h.httpsAddr) + cmd := exec.Command(os.Args[0], "-http", h.httpAddr, "-https", h.httpsAddr) stderr, err := cmd.StderrPipe() if err != nil { h.T.Fatal(err) diff --git a/gracehttp/testserver/testserver.go b/gracehttp/testbin_test.go similarity index 92% rename from gracehttp/testserver/testserver.go rename to gracehttp/testbin_test.go index 3cf4d98..b92ec2d 100644 --- a/gracehttp/testserver/testserver.go +++ b/gracehttp/testbin_test.go @@ -1,5 +1,4 @@ -// Command testserver implements a test case. -package main +package gracehttp_test import ( "crypto/tls" @@ -11,11 +10,27 @@ import ( "os" "strings" "sync" + "testing" "time" "github.com/facebookgo/grace/gracehttp" ) +func TestMain(m *testing.M) { + const ( + testbinKey = "GRACEHTTP_TEST_BIN" + testbinValue = "1" + ) + if os.Getenv(testbinKey) == testbinValue { + testbinMain() + return + } + if err := os.Setenv(testbinKey, testbinValue); err != nil { + panic(err) + } + os.Exit(m.Run()) +} + type response struct { Sleep time.Duration Pid int @@ -84,7 +99,7 @@ func httpsServer(addr string) *http.Server { } } -func main() { +func testbinMain() { var httpAddr, httpsAddr string flag.StringVar(&httpAddr, "http", ":48560", "http address to bind to") flag.StringVar(&httpsAddr, "https", ":48561", "https address to bind to") @@ -112,7 +127,7 @@ func main() { go wait(&wg, fmt.Sprintf("https://%s/sleep/?duration=1ms", httpsAddr)) wg.Wait() - err = json.NewEncoder(os.Stderr).Encode(&response{Pid: os.Getpid()}) + err := json.NewEncoder(os.Stderr).Encode(&response{Pid: os.Getpid()}) if err != nil { log.Fatalf("Error writing startup json: %s", err) }