test(pipeline): verify idempotency under faults and reordered delivery
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -24,76 +23,6 @@ import (
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func TestWorkerPersistenceAndDuplicate(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
|
||||
Reference in New Issue
Block a user