2012-06-05 06:21:10 +08:00
|
|
|
package gracehttp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2013-08-21 02:29:40 +08:00
|
|
|
"crypto/tls"
|
2012-06-05 06:21:10 +08:00
|
|
|
"encoding/json"
|
2013-08-20 06:39:03 +08:00
|
|
|
"flag"
|
2012-06-05 06:21:10 +08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2013-08-21 02:29:40 +08:00
|
|
|
"log"
|
2013-08-20 13:51:32 +08:00
|
|
|
"net"
|
2012-06-05 06:21:10 +08:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2013-08-21 02:29:40 +08:00
|
|
|
|
|
|
|
"github.com/daaku/go.freeport"
|
|
|
|
"github.com/daaku/go.tool"
|
2012-06-05 06:21:10 +08:00
|
|
|
)
|
|
|
|
|
2013-08-20 06:39:03 +08:00
|
|
|
// Debug logging.
|
|
|
|
var debugLog = flag.Bool("debug", false, "enable debug logging")
|
|
|
|
|
2013-03-26 04:33:05 +08:00
|
|
|
func debug(format string, a ...interface{}) {
|
2013-08-20 06:39:03 +08:00
|
|
|
if *debugLog {
|
2013-03-26 04:33:05 +08:00
|
|
|
println(fmt.Sprintf(format, a...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-05 06:21:10 +08:00
|
|
|
// The response from the test server.
|
|
|
|
type response struct {
|
|
|
|
Sleep time.Duration
|
|
|
|
Pid int
|
2013-08-21 02:29:40 +08:00
|
|
|
Error string
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// State for the test run.
|
|
|
|
type harness struct {
|
2013-08-20 13:51:32 +08:00
|
|
|
T *testing.T // The test instance.
|
|
|
|
ImportPath string // The import path for the server command.
|
|
|
|
ExeName string // The temp binary from the build.
|
2013-08-21 02:29:40 +08:00
|
|
|
httpAddr string // The address for the http server.
|
|
|
|
httpsAddr string // The address for the https server.
|
2013-08-20 13:51:32 +08:00
|
|
|
Process []*os.Process // The server commands, oldest to newest.
|
|
|
|
ProcessMutex sync.Mutex // The mutex to guard Process manipulation.
|
|
|
|
RequestWaitGroup sync.WaitGroup // The wait group for the HTTP requests.
|
2013-08-21 02:29:40 +08:00
|
|
|
newProcess chan bool // A bool is sent on start/restart.
|
2013-08-20 13:51:32 +08:00
|
|
|
requestCount int
|
|
|
|
requestCountMutex sync.Mutex
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find 3 free ports and setup addresses.
|
|
|
|
func (h *harness) SetupAddr() {
|
2013-08-21 02:29:40 +08:00
|
|
|
port, err := freeport.Get()
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatalf("Failed to find a free port: %s", err)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
2013-08-21 02:29:40 +08:00
|
|
|
h.httpAddr = fmt.Sprintf("127.0.0.1:%d", port)
|
2012-06-05 06:21:10 +08:00
|
|
|
|
2013-08-21 02:29:40 +08:00
|
|
|
port, err = freeport.Get()
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatalf("Failed to find a free port: %s", err)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
2013-08-21 02:29:40 +08:00
|
|
|
h.httpsAddr = fmt.Sprintf("127.0.0.1:%d", port)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Builds the command.
|
|
|
|
func (h *harness) Build() {
|
|
|
|
basename := filepath.Base(h.ImportPath)
|
|
|
|
tempFile, err := ioutil.TempFile("", basename+"-")
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatalf("Error creating temp file: %s", err)
|
|
|
|
}
|
|
|
|
h.ExeName = tempFile.Name()
|
|
|
|
_ = os.Remove(h.ExeName) // the build tool will create this
|
|
|
|
options := tool.Options{
|
|
|
|
ImportPaths: []string{h.ImportPath},
|
|
|
|
Output: h.ExeName,
|
|
|
|
}
|
|
|
|
_, err = options.Command("build")
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a fresh server and wait for pid updates on restart.
|
|
|
|
func (h *harness) Start() {
|
2013-08-21 02:29:40 +08:00
|
|
|
h.SetupAddr()
|
|
|
|
cmd := exec.Command(h.ExeName, "-http", h.httpAddr, "-https", h.httpsAddr)
|
2012-06-05 06:21:10 +08:00
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
go func() {
|
|
|
|
reader := bufio.NewReader(stderr)
|
|
|
|
for {
|
|
|
|
line, isPrefix, err := reader.ReadLine()
|
|
|
|
if err != nil {
|
2013-08-21 02:29:40 +08:00
|
|
|
log.Fatalf("Failed to read line from server process: %s", err)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
if isPrefix {
|
2013-08-21 02:29:40 +08:00
|
|
|
log.Fatalf("Deal with isPrefix for line: %s", line)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
res := &response{}
|
|
|
|
err = json.Unmarshal([]byte(line), res)
|
|
|
|
if err != nil {
|
2013-08-21 02:29:40 +08:00
|
|
|
log.Fatalf("Could not parse json from stderr %s: %s", line, err)
|
|
|
|
}
|
|
|
|
if res.Error != "" {
|
|
|
|
println(fmt.Sprintf("Got error from process: %v", res))
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
process, err := os.FindProcess(res.Pid)
|
|
|
|
if err != nil {
|
2013-08-21 02:29:40 +08:00
|
|
|
log.Fatalf("Could not find process with pid: %d", res.Pid)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
2013-08-20 06:37:41 +08:00
|
|
|
h.ProcessMutex.Lock()
|
2012-06-05 06:21:10 +08:00
|
|
|
h.Process = append(h.Process, process)
|
2013-08-20 06:37:41 +08:00
|
|
|
h.ProcessMutex.Unlock()
|
2012-06-05 06:21:10 +08:00
|
|
|
h.newProcess <- true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatalf("Failed to start command: %s", err)
|
|
|
|
}
|
|
|
|
<-h.newProcess
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart the most recent server.
|
|
|
|
func (h *harness) Restart() {
|
|
|
|
err := h.MostRecentProcess().Signal(syscall.SIGUSR2)
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatalf("Failed to send SIGUSR2 and restart process: %s", err)
|
|
|
|
}
|
|
|
|
<-h.newProcess
|
|
|
|
}
|
|
|
|
|
|
|
|
// Graceful termination of the most recent server.
|
|
|
|
func (h *harness) Stop() {
|
|
|
|
err := h.MostRecentProcess().Signal(syscall.SIGTERM)
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatalf("Failed to send SIGTERM and stop process: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the most recent server process.
|
|
|
|
func (h *harness) MostRecentProcess() *os.Process {
|
2013-08-20 06:37:41 +08:00
|
|
|
h.ProcessMutex.Lock()
|
|
|
|
defer h.ProcessMutex.Unlock()
|
2012-06-05 06:21:10 +08:00
|
|
|
l := len(h.Process)
|
|
|
|
if l == 0 {
|
|
|
|
h.T.Fatalf("Most recent command requested before command was created.")
|
|
|
|
}
|
|
|
|
return h.Process[l-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the built executable.
|
|
|
|
func (h *harness) RemoveExe() {
|
|
|
|
err := os.Remove(h.ExeName)
|
|
|
|
if err != nil {
|
|
|
|
h.T.Fatalf("Failed to RemoveExe: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 02:29:40 +08:00
|
|
|
// Get the global request count and increment it.
|
2013-08-20 13:51:32 +08:00
|
|
|
func (h *harness) RequestCount() int {
|
|
|
|
h.requestCountMutex.Lock()
|
|
|
|
defer h.requestCountMutex.Unlock()
|
|
|
|
c := h.requestCount
|
|
|
|
h.requestCount++
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2012-06-05 06:21:10 +08:00
|
|
|
// Helper for sending a single request.
|
2013-08-21 02:29:40 +08:00
|
|
|
func (h *harness) SendOne(dialgroup *sync.WaitGroup, url string, pid int) {
|
|
|
|
defer h.RequestWaitGroup.Done()
|
2013-08-20 13:51:32 +08:00
|
|
|
count := h.RequestCount()
|
2013-08-21 02:29:40 +08:00
|
|
|
debug("Send %02d pid=%d url=%s", count, pid, url)
|
2012-06-05 06:21:10 +08:00
|
|
|
client := &http.Client{
|
2013-08-20 13:51:32 +08:00
|
|
|
Transport: &http.Transport{
|
|
|
|
DisableKeepAlives: true,
|
|
|
|
Dial: func(network, addr string) (net.Conn, error) {
|
|
|
|
defer dialgroup.Done()
|
|
|
|
return net.Dial(network, addr)
|
|
|
|
},
|
2013-08-21 02:29:40 +08:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
2013-08-20 13:51:32 +08:00
|
|
|
},
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
r, err := client.Get(url)
|
|
|
|
if err != nil {
|
2013-08-21 02:29:40 +08:00
|
|
|
h.T.Fatalf("Failed request %02d to %s pid=%d: %s", count, url, pid, err)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
2013-08-21 02:29:40 +08:00
|
|
|
debug("Body %02d pid=%d url=%s", count, pid, url)
|
2012-06-05 06:21:10 +08:00
|
|
|
defer r.Body.Close()
|
|
|
|
res := &response{}
|
|
|
|
err = json.NewDecoder(r.Body).Decode(res)
|
|
|
|
if err != nil {
|
2013-08-21 02:29:40 +08:00
|
|
|
h.T.Fatalf("Failed to ready decode json response body pid=%d: %s", pid, err)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
if pid != res.Pid {
|
|
|
|
h.T.Fatalf("Didn't get expected pid %d instead got %d", pid, res.Pid)
|
|
|
|
}
|
2013-08-21 02:29:40 +08:00
|
|
|
debug("Done %02d pid=%d url=%s", count, pid, url)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send test HTTP request.
|
|
|
|
func (h *harness) SendRequest() {
|
|
|
|
pid := h.MostRecentProcess().Pid
|
2013-08-21 02:29:40 +08:00
|
|
|
httpFastUrl := fmt.Sprintf("http://%s/sleep/?duration=0", h.httpAddr)
|
|
|
|
httpSlowUrl := fmt.Sprintf("http://%s/sleep/?duration=2s", h.httpAddr)
|
|
|
|
httpsFastUrl := fmt.Sprintf("https://%s/sleep/?duration=0", h.httpsAddr)
|
|
|
|
httpsSlowUrl := fmt.Sprintf("https://%s/sleep/?duration=2s", h.httpsAddr)
|
|
|
|
|
2013-08-20 13:51:32 +08:00
|
|
|
var dialgroup sync.WaitGroup
|
2013-08-21 02:29:40 +08:00
|
|
|
h.RequestWaitGroup.Add(4)
|
|
|
|
dialgroup.Add(4)
|
|
|
|
go h.SendOne(&dialgroup, httpFastUrl, pid)
|
|
|
|
go h.SendOne(&dialgroup, httpSlowUrl, pid)
|
|
|
|
go h.SendOne(&dialgroup, httpsFastUrl, pid)
|
|
|
|
go h.SendOne(&dialgroup, httpsSlowUrl, pid)
|
|
|
|
debug("Added Requests pid=%d", pid)
|
2013-08-20 13:51:32 +08:00
|
|
|
dialgroup.Wait()
|
2013-08-21 02:29:40 +08:00
|
|
|
debug("Dialed Requests pid=%d", pid)
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for everything.
|
|
|
|
func (h *harness) Wait() {
|
|
|
|
h.RequestWaitGroup.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// The main test case.
|
|
|
|
func TestComplex(t *testing.T) {
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Started TestComplex")
|
2012-06-05 06:21:10 +08:00
|
|
|
h := &harness{
|
2012-07-08 05:38:18 +08:00
|
|
|
ImportPath: "github.com/daaku/go.grace/gracehttp/testserver",
|
2012-06-05 06:21:10 +08:00
|
|
|
T: t,
|
2013-08-20 13:51:32 +08:00
|
|
|
newProcess: make(chan bool),
|
2012-06-05 06:21:10 +08:00
|
|
|
}
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Building")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.Build()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Initial Start")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.Start()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Send Request 1")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.SendRequest()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Restart 1")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.Restart()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Send Request 2")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.SendRequest()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Restart 2")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.Restart()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Send Request 3")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.SendRequest()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Waiting")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.Wait()
|
2013-08-20 13:51:32 +08:00
|
|
|
debug("Stopping")
|
|
|
|
h.Stop()
|
2013-03-26 04:33:05 +08:00
|
|
|
debug("Removing Executable")
|
2012-06-05 06:21:10 +08:00
|
|
|
h.RemoveExe()
|
|
|
|
}
|