mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-26 02:25:41 +00:00
bd321af29f
* new PR tasks * add homebox to know words * formatting * bump deps * generate db models * ts errors * drop id * fix accessor * drop unused time field * change CI * add expected error * add type check * resolve serveral type errors * hoise in CI
101 lines
1.9 KiB
Go
101 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func testServer(t *testing.T, r http.Handler) *Server {
|
|
svr := NewServer(WithHost("127.0.0.1"), WithPort("19245"))
|
|
|
|
if r != nil {
|
|
svr.mux.Mount("/", r)
|
|
}
|
|
go func() {
|
|
err := svr.Start()
|
|
assert.NoError(t, err)
|
|
}()
|
|
|
|
ping := func() error {
|
|
_, err := http.Get("http://127.0.0.1:19245")
|
|
return err
|
|
}
|
|
|
|
for {
|
|
if err := ping(); err == nil {
|
|
break
|
|
}
|
|
time.Sleep(time.Millisecond * 100)
|
|
}
|
|
|
|
return svr
|
|
}
|
|
|
|
func Test_ServerShutdown_Error(t *testing.T) {
|
|
svr := NewServer(WithHost("127.0.0.1"), WithPort("19245"))
|
|
|
|
err := svr.Shutdown("test")
|
|
assert.ErrorIs(t, err, ErrServerNotStarted)
|
|
}
|
|
|
|
func Test_ServerStarts_Error(t *testing.T) {
|
|
svr := testServer(t, nil)
|
|
|
|
err := svr.Start()
|
|
assert.ErrorIs(t, err, ErrServerAlreadyStarted)
|
|
|
|
err = svr.Shutdown("test")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func Test_ServerStarts(t *testing.T) {
|
|
svr := testServer(t, nil)
|
|
err := svr.Shutdown("test")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func Test_GracefulServerShutdownWithWorkers(t *testing.T) {
|
|
isFinished := false
|
|
|
|
svr := testServer(t, nil)
|
|
|
|
svr.Background(func() {
|
|
time.Sleep(time.Second * 4)
|
|
isFinished = true
|
|
})
|
|
|
|
err := svr.Shutdown("test")
|
|
|
|
assert.NoError(t, err)
|
|
assert.True(t, isFinished)
|
|
}
|
|
|
|
func Test_GracefulServerShutdownWithRequests(t *testing.T) {
|
|
var isFinished atomic.Bool
|
|
|
|
router := http.NewServeMux()
|
|
|
|
// add long running handler func
|
|
router.HandleFunc("/test", func(rw http.ResponseWriter, r *http.Request) {
|
|
time.Sleep(time.Second * 3)
|
|
isFinished.Store(true)
|
|
})
|
|
|
|
svr := testServer(t, router)
|
|
|
|
// Make request to "/test"
|
|
go func() {
|
|
_, _ = http.Get("http://127.0.0.1:19245/test") // This is probably bad?
|
|
}()
|
|
|
|
time.Sleep(time.Second) // Hack to wait for the request to be made
|
|
|
|
err := svr.Shutdown("test")
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, isFinished.Load())
|
|
}
|