forked from mtusi2026/mtusi2026lab5
feat(worker): enforce image processing timeout with child process
This commit is contained in:
@@ -229,3 +229,46 @@ func TestWorkerRetryExhausted(t *testing.T) {
|
||||
resp.Body.Close()
|
||||
t.Logf("PASS attachment=%s: %d attempts, failed/retry_exhausted, content/link rejected", att.ID, maxAttempts)
|
||||
}
|
||||
|
||||
func TestWorkerProcessingTimeout(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
stopService(t, "worker")
|
||||
if workerQueue(t) != "0:0" {
|
||||
t.Fatal("run on an idle stand with an empty attachment queue")
|
||||
}
|
||||
|
||||
// Decode and resize of 36 million pixels must be interrupted by the short deadline.
|
||||
resp := uploadAttachment(t, user, chat.ID, "large.png", "image/png", makePNG(t, 6000, 6000))
|
||||
requireStatus(t, resp, http.StatusAccepted)
|
||||
var att dto.AttachmentResponse
|
||||
mustDecode(t, resp, &att)
|
||||
|
||||
testWorker := "worker-timeout-" + uuid.NewString()
|
||||
t.Cleanup(func() {
|
||||
workerCommand(t, "rm", "-f", testWorker)
|
||||
})
|
||||
workerCommand(t, "compose", "run", "-d", "--no-deps", "--name", testWorker,
|
||||
"-e", "JOB_TIMEOUT_SECONDS=1", "-e", "JOB_MAX_ATTEMPTS=1", "worker")
|
||||
parentPID := workerCommand(t, "inspect", "--format", "{{.State.Pid}}", testWorker)
|
||||
workerWait(t, "image child process", 5*time.Second, func() bool {
|
||||
return strings.Contains(workerCommand(t, "top", testWorker, "-eo", "pid,args"), "-process-image")
|
||||
})
|
||||
failed := waitAttachment(t, user, att.ID, "failed", 5*time.Second)
|
||||
if failed.ErrorCode == nil || *failed.ErrorCode != "retry_exhausted" {
|
||||
t.Fatal("expected retry_exhausted after processing deadline")
|
||||
}
|
||||
if strings.Contains(workerCommand(t, "top", testWorker, "-eo", "pid,args"), "-process-image") {
|
||||
t.Fatal("image process survived its deadline")
|
||||
}
|
||||
if workerSQL(t, "SELECT count(*) FROM attachment_variants WHERE attachment_id='"+att.ID.String()+"'") != "0" {
|
||||
t.Fatal("timed-out processing published variants")
|
||||
}
|
||||
|
||||
// The same parent process must remain usable after terminating the image child.
|
||||
uploadPNG(t, user, chat.ID)
|
||||
after := workerCommand(t, "inspect", "--format", "{{.State.Pid}}", testWorker)
|
||||
if parentPID != after || parentPID == "0" {
|
||||
t.Fatal("parent worker restarted after the image timeout")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user