Skip to content

Commit

Permalink
Make tests fast by pinging port (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw authored Jul 2, 2023
1 parent ae5e338 commit dbeae29
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Most recent version is listed first.
# v0.0.57
- Add own ACME client implementation: https://github.com/komuw/ong/pull/294
- Work around bug in checklocks static analyzer: https://github.com/komuw/ong/pull/298
- Make tests fast by pinging port: https://github.com/komuw/ong/pull/299

# v0.0.56
- Set appropriate log level for http.Server.ErrorLog: https://github.com/komuw/ong/pull/288
Expand Down
9 changes: 4 additions & 5 deletions server/pprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"sync"
"testing"
"time"

"github.com/komuw/ong/log"

Expand All @@ -25,10 +24,10 @@ func TestPprofServer(t *testing.T) {
startPprofServer(l)

// await for the server to start.
time.Sleep(1 * time.Second)
port := 65060
ping(t, uint16(port))

uri := "/debug/pprof/heap"
port := 65060
res, err := http.Get(fmt.Sprintf("http://localhost:%d%s", port, uri))
attest.Ok(t, err)
defer res.Body.Close()
Expand All @@ -42,11 +41,11 @@ func TestPprofServer(t *testing.T) {
startPprofServer(l)

// await for the server to start.
time.Sleep(1 * time.Second)
port := 65060
ping(t, uint16(port))

runhandler := func() {
uri := "/debug/pprof/heap"
port := 65060
res, err := http.Get(fmt.Sprintf("http://localhost:%d%s", port, uri))
attest.Ok(t, err)
defer res.Body.Close()
Expand Down
35 changes: 31 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"math"
"math/rand"
"net"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -35,6 +36,32 @@ func getPort() uint16 {
return p
}

// ping waits for port to be open.
func ping(t attest.TB, port uint16) {
t.Helper()

var err error
count := 0
maxCount := 12
defer func() {
attest.Ok(t, err)
}()

for {
count = count + 1
time.Sleep(1 * time.Second)
_, err = net.DialTimeout("tcp", fmt.Sprintf("localhost:%d", port), 1*time.Second)
if err == nil {
break
}

if count > maxCount {
err = fmt.Errorf("ping max count(%d) reached: %w", maxCount, err)
break
}
}
}

// todo: enable this.
//
// This is disabled because `server.Run()` starts a http server(aka goroutine)
Expand Down Expand Up @@ -194,7 +221,7 @@ func TestServer(t *testing.T) {
}()

// await for the server to start.
time.Sleep(11 * time.Second)
ping(t, port)

{
// https server.
Expand Down Expand Up @@ -297,7 +324,7 @@ func TestServer(t *testing.T) {
}()

// await for the server to start.
time.Sleep(11 * time.Second)
ping(t, port)

t.Run("smallSize", func(t *testing.T) {
postMsg := strings.Repeat("a", int(defaultMaxBodyBytes/100))
Expand Down Expand Up @@ -364,7 +391,7 @@ func TestServer(t *testing.T) {
}()

// await for the server to start.
time.Sleep(11 * time.Second)
ping(t, port)

runhandler := func() {
res, err := client.Get(fmt.Sprintf("https://127.0.0.1:%d%s", port, uri)) // note: the https scheme.
Expand Down Expand Up @@ -416,7 +443,7 @@ func BenchmarkServer(b *testing.B) {
}()

// await for the server to start.
time.Sleep(11 * time.Second)
ping(b, port)

b.ResetTimer()
b.ReportAllocs()
Expand Down

0 comments on commit dbeae29

Please sign in to comment.