Skip to content

Commit

Permalink
Fix shutdownDelay for integration tests #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Nov 9, 2020
1 parent 18a3a3c commit 0935f1e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
9 changes: 9 additions & 0 deletions internal/test/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test

import "context"

const Key = "test_bug2kguvvhfvuij7gcsg"

func NewContext(background context.Context) context.Context {
return context.WithValue(background, Key, true)
}
8 changes: 8 additions & 0 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/avenga/couper/config/request"
"github.com/avenga/couper/config/runtime"
"github.com/avenga/couper/handler"
"github.com/avenga/couper/internal/test"
"github.com/avenga/couper/logging"
)

Expand Down Expand Up @@ -143,6 +144,13 @@ func (s *HTTPServer) listenForCtx() {
}
s.log.WithFields(logFields).Warn("shutting down")
close(s.shutdownCh)

// testHook - skip shutdownDelay
if _, ok := s.commandCtx.Value(test.Key).(bool); ok {
_ = s.srv.Shutdown(context.TODO())
return
}

time.Sleep(s.config.Timings.ShutdownDelay)
ctx, cancel := context.WithTimeout(context.Background(), s.config.Timings.ShutdownTimeout)
defer cancel()
Expand Down
37 changes: 29 additions & 8 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server_test
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
Expand All @@ -13,6 +14,7 @@ import (
"testing"
"time"

"github.com/sirupsen/logrus"
logrustest "github.com/sirupsen/logrus/hooks/test"

"github.com/avenga/couper/command"
Expand Down Expand Up @@ -85,11 +87,17 @@ func newCouper(file string, helper *test.Helper) (func(), *logrustest.Hook) {

log, hook := logrustest.NewNullLogger()

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(test.NewContext(context.Background()))
go func() {
helper.Must(command.NewRun(ctx).Execute([]string{file}, gatewayConf, log.WithContext(context.Background())))
helper.Must(command.NewRun(ctx).Execute([]string{file}, gatewayConf, log.WithContext(ctx)))
}()
time.Sleep(time.Second / 2)

entry := hook.LastEntry()
if entry.Level < logrus.InfoLevel {
helper.Must(fmt.Errorf(entry.String()))
}

hook.Reset() // no startup logs
return cancel, hook
}
Expand All @@ -116,19 +124,20 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
requests []requestCase
}

dialer := &net.Dialer{}
client := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
_, port, _ := net.SplitHostPort(addr)
if port != "" {
return net.Dial("tcp4", "127.0.0.1:"+port)
return dialer.DialContext(ctx, "tcp4", "127.0.0.1:"+port)
}
return net.Dial("tcp4", "127.0.0.1")
return dialer.DialContext(ctx, "tcp4", "127.0.0.1")
},
},
}

for _, testcase := range []testCase{
for i, testcase := range []testCase{
{"spa/01_couper.hcl", []requestCase{
{
testRequest{http.MethodGet, "http://anyserver:8080/"},
Expand All @@ -145,8 +154,20 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
expectation{http.StatusOK, []byte(`<html><body><title>1.0</title></body></html>`), nil, "file"},
},
}},
{"api/01_couper.hcl", []requestCase{
{
testRequest{http.MethodGet, "http://anyserver:8080/"},
expectation{http.StatusOK, []byte(`<html>1002</html>`), nil, ""},
},
{
testRequest{http.MethodGet, "http://anyserver:8080/v1/anything"},
expectation{http.StatusOK, []byte(`<html><body><title>1.0</title></body></html>`), http.Header{"Content-Type": {"application/json"}}, "api"},
},
}},
} {
cancelFn, logHook := newCouper(path.Join("testdata/integration", testcase.fileName), test.New(t))
confPath := path.Join("testdata/integration", testcase.fileName)
t.Logf("#%.2d: Create Couper: %q", i+1, confPath)
shutdown, logHook := newCouper(confPath, test.New(t))

for _, rc := range testcase.requests {
t.Run(testcase.fileName+" "+rc.req.method+"|"+rc.req.url, func(subT *testing.T) {
Expand Down Expand Up @@ -179,6 +200,7 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
}

entry := logHook.LastEntry()

if entry == nil || entry.Data["type"] != "couper_access" {
t.Error("Expected a log entry, got nothing")
return
Expand All @@ -188,7 +210,6 @@ func TestHTTPServer_ServeHTTP(t *testing.T) {
}
})
}

cancelFn()
shutdown()
}
}
12 changes: 12 additions & 0 deletions server/testdata/integration/api/01_couper.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server "api" {
api {
base_path = "/v1"

endpoint "/" {
backend {
path = "/anything"
origin = "http://anyserver/"
}
}
}
}

0 comments on commit 0935f1e

Please sign in to comment.