Skip to content

Commit

Permalink
Avoid random test failures by waiting for http server to start
Browse files Browse the repository at this point in the history
  • Loading branch information
samanhappy committed Jan 12, 2024
1 parent 707e216 commit 7b2f63c
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions probe/websocket/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package websocket
import (
"fmt"
"log"
"net"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -50,6 +51,21 @@ func TestWSPing(t *testing.T) {
}
}()

// wait for http server to start
conn, err := net.Conn(nil), error(nil)
for i := 0; i < 30; i++ {
conn, err = net.Dial("tcp", "127.0.0.1:18080")
if err == nil {
conn.Close()
break
}
t.Log("waiting for http server to start...")
time.Sleep(1 * time.Second)
}
if err != nil {
t.Fatalf("http server not started: %v", err)
}

testcases := []TestCase{
{URL: "ws://127.0.0.1:18080/right", Timeout: 500 * time.Millisecond, Headers: token, Want: false},
{URL: "ws://127.0.0.1:18080/right", Timeout: 2000 * time.Millisecond, Headers: token, Want: true},
Expand All @@ -66,9 +82,8 @@ func TestWSPing(t *testing.T) {
Headers: test.Headers,
}
ws.Config(global.ProbeSettings{})
ok, _ := ws.DoProbe()
assert.Equalf(t, test.Want, ok, fmt.Sprintf("case %d", i))

ok, err := ws.DoProbe()
assert.Equalf(t, test.Want, ok, fmt.Sprintf("case %d %s", i, err))
}
}

Expand Down

0 comments on commit 7b2f63c

Please sign in to comment.