package app_test import ( "context" "io" "log/slog" "net/http" "testing" "time" "messenger/internal/app" "messenger/internal/testenv" ) // newIsolatedTester assembles the application over containers of its own, // so a test can stop or pause them without disturbing the shared ones. func newIsolatedTester(t *testing.T, requestTimeout time.Duration) (*tester, *testenv.DB, *testenv.KV) { t.Helper() ctx := context.Background() pg, err := testenv.StartPostgres(ctx) if err != nil { t.Fatal(err) } t.Cleanup(pg.Close) vk, err := testenv.StartValkey(ctx) if err != nil { t.Fatal(err) } t.Cleanup(vk.Close) log := slog.New(slog.NewTextHandler(io.Discard, nil)) a := app.New(app.Deps{ Log: log, DB: pg.Pool, Sessions: vk.Client, SessionTTL: time.Hour, MaxBodyBytes: 64 * 1024, RequestTimeout: requestTimeout, InstanceID: testInstance, }) return &tester{t: t, a: a}, pg, vk } // Without the session store no caller can be authenticated: the API answers // 503, never 401 (the token is not at fault) and never as an anonymous user. // Registration does not need the store and keeps working. func TestValkeyOutageAnswers503(t *testing.T) { ts, _, vk := newIsolatedTester(t, 5*time.Second) alice := ts.user("alice", "Алиса") room := ts.chat(alice.token, "t", alice.ID) if err := vk.Stop(context.Background()); err != nil { t.Fatal(err) } ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+room.ID, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodGet, "/health/live", "", nil, nil), http.StatusOK, "") body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword} ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusCreated, "") } func TestPostgresOutageAnswers503(t *testing.T) { ts, pg, _ := newIsolatedTester(t, 5*time.Second) alice := ts.user("alice", "Алиса") if err := pg.Stop(context.Background()); err != nil { t.Fatal(err) } ts.expect(ts.do(http.MethodGet, "/api/v1/chats", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable") body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword} ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusServiceUnavailable, "unavailable") ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable") } // A database that accepts connections but never answers must not hold the // request: the request deadline turns it into a 503. func TestHungPostgresAnswers503WithinTheRequestTimeout(t *testing.T) { ts, pg, _ := newIsolatedTester(t, 300*time.Millisecond) alice := ts.user("alice", "Алиса") ctx := context.Background() if err := pg.Pause(ctx); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = pg.Unpause(ctx) }) for _, path := range []string{"/api/v1/chats", "/health/ready"} { start := time.Now() ts.expect(ts.do(http.MethodGet, path, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable") if elapsed := time.Since(start); elapsed > 2*time.Second { t.Fatalf("%s took %v with a hung database, want it bounded", path, elapsed) } } }