Исправить замечания ревью: защита rollout от остановленных реплик, чтение серий, время экспериментов по часам Docker, выход сервера при ошибке старта
This commit is contained in:
+13
-6
@@ -80,15 +80,22 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
InstanceID: alias,
|
||||
IdempotencyTTL: cfg.IdempotencyTTL,
|
||||
})
|
||||
var background sync.WaitGroup
|
||||
defer background.Wait() // before the deferred pool.Close
|
||||
background.Go(func() { chat.RunKeyCleanup(ctx, log, a.Chats, keyCleanupEvery) })
|
||||
var debugLn net.Listener
|
||||
if cfg.DebugAddr != "" {
|
||||
publishDebugVars(alias, pool)
|
||||
debugLn, err := net.Listen("tcp", cfg.DebugAddr)
|
||||
if err != nil {
|
||||
if debugLn, err = net.Listen("tcp", cfg.DebugAddr); err != nil {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.DebugAddr, err)
|
||||
}
|
||||
publishDebugVars(alias, pool)
|
||||
}
|
||||
|
||||
// The background work ends with the server: on a signal, and also when
|
||||
// the server fails, so the deferred Wait cannot block the exit.
|
||||
var background sync.WaitGroup
|
||||
defer background.Wait() // before the deferred pool.Close
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel() // before Wait
|
||||
background.Go(func() { chat.RunKeyCleanup(ctx, log, a.Chats, keyCleanupEvery) })
|
||||
if debugLn != nil {
|
||||
background.Go(func() {
|
||||
if err := server.RunDebug(ctx, log, debugLn, expvar.Handler()); err != nil {
|
||||
log.Error("debug listener stopped", "error", err.Error())
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// A start-up failure after the background work began must still end the
|
||||
// process with an error, not leave it waiting for a signal.
|
||||
func TestRunFailsFastWhenTheDebugListenerCannotStart(t *testing.T) {
|
||||
busy, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer busy.Close()
|
||||
cfg := config.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: 0,
|
||||
ShutdownTimeout: time.Second,
|
||||
MaxBodyBytes: 1024,
|
||||
DatabaseURL: db.DSN,
|
||||
SessionStoreURL: kv.URL,
|
||||
SessionTTL: time.Hour,
|
||||
IdempotencyTTL: time.Hour,
|
||||
DBMaxConns: 2,
|
||||
DebugAddr: busy.Addr().String(),
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- run(context.Background(), slog.New(slog.NewTextHandler(io.Discard, nil)), cfg, "alias")
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err == nil {
|
||||
t.Fatal("run succeeded on a busy debug address")
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("run hung instead of returning the listen error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user