Skip to content

Commit

Permalink
Fix flaky test (#152)
Browse files Browse the repository at this point in the history
* random port range limited to avoid collisions

* golang 1.11 added to the build matrix

* minor sleep after setup added to the integration test
  • Loading branch information
kpacha committed Sep 27, 2018
1 parent 6e4ac44 commit 76d4acf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: go

go:
- "1.11"
- "1.10"
- 1.9
- 1.8
Expand Down
17 changes: 14 additions & 3 deletions router/http/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestRunServer_TLS(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

port := 9999 + rand.Intn(1000)
port := newPort()

done := make(chan error)
go func() {
Expand All @@ -51,6 +51,8 @@ func TestRunServer_TLS(t *testing.T) {
return
}

<-time.After(100 * time.Millisecond)

resp, err := client.Get(fmt.Sprintf("https://localhost:%d", port))
if err != nil {
t.Error(err)
Expand All @@ -71,7 +73,7 @@ func TestRunServer_plain(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

port := 9999 + rand.Intn(1000)
port := newPort()

done := make(chan error)
go func() {
Expand All @@ -82,6 +84,8 @@ func TestRunServer_plain(t *testing.T) {
)
}()

<-time.After(100 * time.Millisecond)

resp, err := http.Get(fmt.Sprintf("http://localhost:%d", port))
if err != nil {
t.Error(err)
Expand All @@ -104,7 +108,7 @@ func TestRunServer_disabledTLS(t *testing.T) {

done := make(chan error)

port := 9999 + rand.Intn(1000)
port := newPort()

go func() {
done <- RunServer(
Expand All @@ -118,6 +122,8 @@ func TestRunServer_disabledTLS(t *testing.T) {
)
}()

<-time.After(100 * time.Millisecond)

resp, err := http.Get(fmt.Sprintf("http://localhost:%d", port))
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -272,3 +278,8 @@ func httpsClient(cert string) (*http.Client, error) {
}
return &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConf}}, nil
}

// newPort returns random port numbers to avoid port collisions during the tests
func newPort() int {
return 16666 + rand.Intn(40000)
}

0 comments on commit 76d4acf

Please sign in to comment.