* HH-442: isolate runtime processes and harden shutdown * HH-442: harden worker shutdown races * HH-442: gate dependency shutdown on active handlers --------- Co-authored-by: Rogee <rogee@ipao.vip>
51 lines
1.7 KiB
Go
51 lines
1.7 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/gochat/gochat/internal/config"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestReadinessChecksRequiredDependenciesAndDrainState(t *testing.T) {
|
|
migrations := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(migrations, "000001_initial.up.sql"), []byte("SELECT 1;"), 0o600))
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.Exec("CREATE TABLE schema_migrations (version INTEGER NOT NULL, dirty BOOLEAN NOT NULL)").Error)
|
|
require.NoError(t, db.Exec("INSERT INTO schema_migrations(version, dirty) VALUES (1, false)").Error)
|
|
mini := miniredis.RunT(t)
|
|
rdb := redis.NewClient(&redis.Options{Addr: mini.Addr()})
|
|
t.Cleanup(func() { _ = rdb.Close() })
|
|
search := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }))
|
|
defer search.Close()
|
|
ready := &atomic.Bool{}
|
|
ready.Store(true)
|
|
cfg := &config.Config{
|
|
Database: config.DatabaseConfig{MigrationsPath: migrations},
|
|
Search: config.SearchConfig{Engine: "meilisearch", Host: search.URL, TimeoutSeconds: 1},
|
|
}
|
|
|
|
checks := newReadinessChecks(cfg, db, rdb, ready)
|
|
require.Len(t, checks, 4)
|
|
byName := map[string]func(context.Context) error{}
|
|
for _, check := range checks {
|
|
byName[check.Name] = check.Check
|
|
require.NoError(t, check.Check(context.Background()), check.Name)
|
|
}
|
|
ready.Store(false)
|
|
require.ErrorContains(t, byName["draining"](context.Background()), "shutdown")
|
|
mini.Close()
|
|
require.Error(t, byName["redis"](context.Background()))
|
|
}
|