Improved test consistency
All checks were successful
/ test (push) Successful in 18s

This commit is contained in:
Eduard Urbach 2025-06-06 17:47:15 +02:00
parent 3adfce18cb
commit 0442fc1e2e
Signed by: eduard
GPG key ID: 49226B848C78F6C8
2 changed files with 17 additions and 0 deletions

View file

@ -19,6 +19,7 @@ import (
// Server is the interface for an HTTP server.
type Server interface {
Get(path string, handler Handler)
Ready() chan struct{}
Request(method string, path string, headers []Header, body io.Reader) Response
Router() *router.Router[Handler]
Run(address string) error
@ -31,6 +32,7 @@ type server struct {
contextPool sync.Pool
router *router.Router[Handler]
errorHandler func(Context, error)
ready chan struct{}
}
// NewServer creates a new HTTP server.
@ -54,6 +56,7 @@ func NewServer() Server {
errorHandler: func(ctx Context, err error) {
log.Println(ctx.Request().Path(), err)
},
ready: make(chan struct{}),
}
s.contextPool.New = func() any { return s.newContext() }
@ -65,6 +68,11 @@ func (s *server) Get(path string, handler Handler) {
s.Router().Add("GET", path, handler)
}
// Ready returns a channel that will be closed once the listener is ready for connection handling.
func (s *server) Ready() chan struct{} {
return s.ready
}
// Request performs a synthetic request and returns the response.
// This function keeps the response in memory so it's slightly slower than a real request.
// However it is very useful inside tests where you don't want to spin up a real web server.
@ -99,6 +107,7 @@ func (s *server) Run(address string) error {
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
close(s.ready)
<-stop
return nil
}

View file

@ -35,6 +35,7 @@ func TestRun(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
_, err := http.Get("http://127.0.0.1:8080/")
assert.Nil(t, err)
}()
@ -48,6 +49,7 @@ func TestBadRequest(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)
defer conn.Close()
@ -73,6 +75,7 @@ func TestBadRequestHeader(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)
defer conn.Close()
@ -95,6 +98,7 @@ func TestBadRequestMethod(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)
defer conn.Close()
@ -116,6 +120,7 @@ func TestBadRequestPath(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)
defer conn.Close()
@ -141,6 +146,7 @@ func TestBadRequestProtocol(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)
defer conn.Close()
@ -163,6 +169,7 @@ func TestConnectionClose(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)
defer conn.Close()
@ -183,6 +190,7 @@ func TestEarlyClose(t *testing.T) {
go func() {
defer syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
<-s.Ready()
conn, err := net.Dial("tcp", ":8080")
assert.Nil(t, err)