test(pipeline): verify idempotency under faults and reordered delivery

This commit is contained in:
kk10
2026-11-16 22:51:35 +03:00
parent f8ca7a7218
commit 179c795758
6 changed files with 383 additions and 201 deletions
+78 -1
View File
@@ -1,12 +1,19 @@
//go:build failure || worker
//go:build failure || worker || pipeline
package studenttests_test
import (
"context"
"io"
"net/http"
"os/exec"
"strings"
"testing"
"time"
"github.com/google/uuid"
"mtusi2026-messenger/internal/dto"
)
func compose(t *testing.T, args ...string) {
@@ -45,3 +52,73 @@ func waitReady(t *testing.T) {
}
t.Fatal("service did not become ready in 60s")
}
func workerCommand(t *testing.T, args ...string) string {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Dir = ".."
out, err := cmd.Output()
if err != nil {
t.Fatalf("Docker command failed: %v", err)
}
return strings.TrimSpace(string(out))
}
func workerSQL(t *testing.T, query string) string {
t.Helper()
return workerCommand(t, "compose", "exec", "-T", "postgres", "psql",
"-U", "messenger", "-d", "messenger", "-Atqc", query)
}
func workerWait(t *testing.T, description string, timeout time.Duration, condition func() bool) {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if condition() {
return
}
time.Sleep(300 * time.Millisecond)
}
t.Fatalf("timeout waiting for %s", description)
}
func workerQueue(t *testing.T) string {
t.Helper()
out := workerCommand(t, "compose", "exec", "-T", "rabbitmq", "rabbitmqctl",
"list_queues", "name", "messages_ready", "messages_unacknowledged", "--silent")
for _, line := range strings.Split(out, "\n") {
fields := strings.Fields(line)
if len(fields) == 3 && fields[0] == "attachments.process.v1" {
return fields[1] + ":" + fields[2]
}
}
t.Fatal("attachment queue missing")
return ""
}
func workerUpload(t *testing.T, user testUser, chatID uuid.UUID) (dto.AttachmentResponse, []byte) {
t.Helper()
source := makePNG(t, 320, 200)
resp := uploadAttachment(t, user, chatID, "worker.png", "image/png", source)
requireStatus(t, resp, http.StatusAccepted)
var att dto.AttachmentResponse
mustDecode(t, resp, &att)
if att.Status != "queued" {
t.Fatalf("expected queued, got %s", att.Status)
}
return att, source
}
func workerContent(t *testing.T, user testUser, id uuid.UUID, variant string) []byte {
t.Helper()
resp := doRequest(t, http.MethodGet, "/api/v1/attachments/"+id.String()+"/content?variant="+variant, user.Token, nil)
requireStatus(t, resp, http.StatusOK)
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
return data
}