use TestMain and remove external binary

This commit is contained in:
Naitik Shah 2015-06-15 20:27:40 +00:00
parent 2e2aaa0ab8
commit d2d22699f1
2 changed files with 22 additions and 31 deletions

View File

@ -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",
}
)
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)

View File

@ -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)
}