Ограничить ожидание зависимостей и отвечать 503 при их недоступности

This commit is contained in:
2026-10-06 15:13:04 +03:00
parent 9890482747
commit f6acdf91e0
10 changed files with 313 additions and 21 deletions
+36
View File
@@ -10,6 +10,7 @@ import (
"runtime"
"sync"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
@@ -94,6 +95,35 @@ func (d *DB) Reset(t testing.TB) {
}
}
// Stop stops the container, as when the database goes down.
func (d *DB) Stop(ctx context.Context) error {
timeout := 5 * time.Second
return d.container.Stop(ctx, &timeout)
}
// Pause freezes the server: connections stay open but nothing answers, as
// with a hung database.
func (d *DB) Pause(ctx context.Context) error {
return withDocker(ctx, func(cli *testcontainers.DockerClient) error {
return cli.ContainerPause(ctx, d.container.GetContainerID())
})
}
func (d *DB) Unpause(ctx context.Context) error {
return withDocker(ctx, func(cli *testcontainers.DockerClient) error {
return cli.ContainerUnpause(ctx, d.container.GetContainerID())
})
}
func withDocker(ctx context.Context, fn func(cli *testcontainers.DockerClient) error) error {
cli, err := testcontainers.NewDockerClientWithOpts(ctx)
if err != nil {
return err
}
defer cli.Close()
return fn(cli)
}
func (d *DB) Close() {
if d.Pool != nil {
d.Pool.Close()
@@ -136,6 +166,12 @@ func (k *KV) Reset(t testing.TB) {
}
}
// Stop stops the container, as when the session store goes down.
func (k *KV) Stop(ctx context.Context) error {
timeout := 5 * time.Second
return k.container.Stop(ctx, &timeout)
}
func (k *KV) Close() {
if k.Client != nil {
_ = k.Client.Close()