Отвязать жизненный цикл сервера от сборки приложения
This commit is contained in:
@@ -9,20 +9,32 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/health"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
|
||||
// newHandler builds the smallest handler with probes; the lifecycle does not
|
||||
// depend on the business modules or their stores.
|
||||
func newHandler(log *slog.Logger) (*gin.Engine, *health.Handler) {
|
||||
e := httpx.NewEngine(log, 1024)
|
||||
probes := health.New()
|
||||
probes.Register(e)
|
||||
return e, probes
|
||||
}
|
||||
|
||||
func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(log, 1024)
|
||||
h, probes := newHandler(log)
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }()
|
||||
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
|
||||
|
||||
base := "http://" + ln.Addr().String()
|
||||
waitFor(t, base+"/health/ready", http.StatusOK)
|
||||
@@ -43,14 +55,14 @@ func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) {
|
||||
|
||||
func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(log, 1024)
|
||||
h, probes := newHandler(log)
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.Run(ctx, log, ln, a, 5*time.Second) }()
|
||||
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
|
||||
base := "http://" + ln.Addr().String()
|
||||
waitFor(t, base+"/health/ready", http.StatusOK)
|
||||
|
||||
@@ -67,7 +79,7 @@ func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
|
||||
cancel()
|
||||
<-done
|
||||
<-slow
|
||||
if !a.Health.IsReady() {
|
||||
if !probes.IsReady() {
|
||||
return
|
||||
}
|
||||
t.Fatal("readiness must be switched off during shutdown")
|
||||
|
||||
Reference in New Issue
Block a user