use TestMain and remove external binary
This commit is contained in:
parent
2e2aaa0ab8
commit
d2d22699f1
@ -17,16 +17,10 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/facebookgo/freeport"
|
"github.com/facebookgo/freeport"
|
||||||
"github.com/facebookgo/tool"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// Debug logging.
|
||||||
// Debug logging.
|
var debugLog = flag.Bool("debug", false, "enable debug logging")
|
||||||
debugLog = flag.Bool("debug", false, "enable debug logging")
|
|
||||||
testserverCommand = &tool.CommandBuild{
|
|
||||||
ImportPath: "github.com/facebookgo/grace/gracehttp/testserver",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func debug(format string, a ...interface{}) {
|
func debug(format string, a ...interface{}) {
|
||||||
if *debugLog {
|
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.
|
// State for the test run.
|
||||||
type harness struct {
|
type harness struct {
|
||||||
T *testing.T // The test instance.
|
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.
|
// Start a fresh server and wait for pid updates on restart.
|
||||||
func (h *harness) Start() {
|
func (h *harness) Start() {
|
||||||
bin, err := testserverCommand.Build()
|
|
||||||
if err != nil {
|
|
||||||
h.T.Fatalf("build error: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
h.setupAddr()
|
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()
|
stderr, err := cmd.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.T.Fatal(err)
|
h.T.Fatal(err)
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
// Command testserver implements a test case.
|
package gracehttp_test
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
@ -11,11 +10,27 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/facebookgo/grace/gracehttp"
|
"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 {
|
type response struct {
|
||||||
Sleep time.Duration
|
Sleep time.Duration
|
||||||
Pid int
|
Pid int
|
||||||
@ -84,7 +99,7 @@ func httpsServer(addr string) *http.Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func testbinMain() {
|
||||||
var httpAddr, httpsAddr string
|
var httpAddr, httpsAddr string
|
||||||
flag.StringVar(&httpAddr, "http", ":48560", "http address to bind to")
|
flag.StringVar(&httpAddr, "http", ":48560", "http address to bind to")
|
||||||
flag.StringVar(&httpsAddr, "https", ":48561", "https 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))
|
go wait(&wg, fmt.Sprintf("https://%s/sleep/?duration=1ms", httpsAddr))
|
||||||
wg.Wait()
|
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 {
|
if err != nil {
|
||||||
log.Fatalf("Error writing startup json: %s", err)
|
log.Fatalf("Error writing startup json: %s", err)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user