fixed read-after-write after failed cache invalidation and API restart

This commit is contained in:
Нурлыгаянова Екатерина
2026-10-29 15:18:44 +03:00
parent 5021df4ea8
commit ee87d17cb9
13 changed files with 510 additions and 78 deletions
+47
View File
@@ -136,6 +136,23 @@ func compose(t *testing.T, env map[string]string, args ...string) {
}
}
// waitHealthy ждёт healthcheck сервиса: после unpause он остаётся unhealthy
// до следующей успешной проверки, и compose up --wait сразу падает.
func waitHealthy(t *testing.T, service string) {
t.Helper()
deadline := time.Now().Add(30 * time.Second)
for time.Now().Before(deadline) {
cmd := exec.Command("docker", "compose", "--env-file", ".env", "ps", "--format", "{{.Health}}", service)
cmd.Dir = root
out, _ := cmd.Output()
if strings.TrimSpace(string(out)) == "healthy" {
return
}
time.Sleep(500 * time.Millisecond)
}
t.Fatalf("%s не стал healthy за 30 с", service)
}
func randHex(n int) string {
b := make([]byte, n)
_, _ = rand.Read(b)
@@ -326,4 +343,34 @@ func TestFailures(t *testing.T) {
t.Error("ожидали две одинаковые полные страницы с сообщением, записанным при упавшем кэше")
}
})
t.Run("инвалидация не удалась, API перезапущен: сообщение с 201 сразу видно в истории", func(t *testing.T) {
// Прогреваем страницу в кэше, затем замораживаем Valkey-кэш: данные
// (и старая версия) остаются, но DEL версии падает по таймауту.
c.call(http.MethodGet, messagesPath+"?limit=100", token, nil, nil)
c.call(http.MethodGet, messagesPath+"?limit=100", token, nil, nil)
compose(t, nil, "pause", "valkey-cache")
paused := true
t.Cleanup(func() {
if paused {
compose(t, nil, "unpause", "valkey-cache")
}
waitHealthy(t, "valkey-cache")
})
send := c.call(http.MethodPost, messagesPath, token, map[string]string{"text": "до рестарта API"}, nil)
if send.status != 201 {
t.Fatalf("ожидали 201, получили %d", send.status)
}
compose(t, nil, "restart", "api")
c.waitReady(t)
compose(t, nil, "unpause", "valkey-cache")
paused = false
for i := 0; i < 3; i++ {
list := c.call(http.MethodGet, messagesPath+"?limit=100", token, nil, nil)
if list.status != 200 || !slices.Contains(list.itemIDs(), send.str("id")) {
t.Fatalf("чтение %d после рестарта: status %d, подтверждённого сообщения нет в истории", i+1, list.status)
}
time.Sleep(300 * time.Millisecond)
}
})
}