Skip to content

Commit

Permalink
Move AssertHealthy to testsupport
Browse files Browse the repository at this point in the history
  • Loading branch information
tygern committed Mar 27, 2024
1 parent 23d7a1d commit 8a81ce7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
7 changes: 3 additions & 4 deletions internal/app/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package app_test

import (
"github.com/initialcapacity/go-streaming/internal/app"
"github.com/initialcapacity/go-streaming/pkg/testsupport"
"github.com/initialcapacity/go-streaming/pkg/websupport"
"github.com/stretchr/testify/assert"
"testing"
)

func TestHealth(t *testing.T) {
server := websupport.NewServer(app.Handlers(false))
_, _ = server.Start("localhost", 0)
err := server.WaitUntilHealthy("/health")
assert.NoError(t, err)
port, _ := server.Start("localhost", 0)
testsupport.AssertHealthy(t, port, "/health")
}
30 changes: 26 additions & 4 deletions pkg/testsupport/test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package testsupport
import (
"fmt"
"github.com/initialcapacity/go-streaming/pkg/websupport"
"net/http"
"testing"
"time"
)

func StartTestServer(t *testing.T, handlers websupport.Handlers) (string, *websupport.Server) {
server := websupport.NewServer(handlers)

port, _ := server.Start("localhost", 0)
err := server.WaitUntilHealthy("/health")
if err != nil {
t.Errorf("unable to start server: %s", err)
}
AssertHealthy(t, port, "/health")

return fmt.Sprintf("http://localhost:%d", port), server
}
Expand All @@ -24,3 +23,26 @@ func StopTestServer(t *testing.T, server *websupport.Server) {
t.Errorf("unable to stop server: %s", err)
}
}

func AssertHealthy(t *testing.T, port int, path string) {
statusCode := make(chan int)

go func() {
for {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d%s", port, path))
if err == nil {
statusCode <- resp.StatusCode
return
}
}
}()

select {
case code := <-statusCode:
if code != http.StatusOK {
t.Errorf("server responded with a non 200 code: %d", code)
}
case <-time.After(100 * time.Millisecond):
t.Error("server did not respond in 100 milliseconds")
}
}
27 changes: 0 additions & 27 deletions pkg/websupport/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package websupport

import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"time"
)

type Server struct {
Expand Down Expand Up @@ -44,31 +42,6 @@ func (s *Server) Start(host string, port int) (listenerPort int, done chan error
return listenerPort, done
}

func (s *Server) WaitUntilHealthy(path string) error {
statusCode := make(chan int)

go func() {
for {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d%s", s.port, path))
if err == nil {
statusCode <- resp.StatusCode
return
}
}
}()

select {
case code := <-statusCode:
if code == http.StatusOK {
return nil
} else {
return fmt.Errorf("server responded with a non 200 code: %d", code)
}
case <-time.After(100 * time.Millisecond):
return errors.New("server did not respond in 100 milliseconds")
}
}

func (s *Server) Stop() error {
return s.server.Shutdown(context.Background())
}
4 changes: 2 additions & 2 deletions pkg/websupport/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package websupport_test

import (
"fmt"
"github.com/initialcapacity/go-streaming/pkg/testsupport"
"github.com/initialcapacity/go-streaming/pkg/websupport"
"github.com/stretchr/testify/assert"
"io"
Expand All @@ -19,8 +20,7 @@ func TestCreate(t *testing.T) {
})

port, _ := server.Start("localhost", 0)
err := server.WaitUntilHealthy("/")
assert.NoError(t, err)
testsupport.AssertHealthy(t, port, "/")
defer func(server *websupport.Server) {
_ = server.Stop()
}(server)
Expand Down

0 comments on commit 8a81ce7

Please sign in to comment.