| index a141c15ce..81c92bcf5 100644 |
| --- a/internal/console/apimedia_test.go |
| +++ b/internal/console/apimedia_test.go |
| @@ -935,3 +935,96 @@ func TestNewServerRefusesBadMediaLimits(t *testing.T) { |
| t.Fatalf("default limits: %v", err) |
| } |
| } |
| + |
| +// pausingGrants holds the next HEAD of one key until released, so a test |
| +// can run dispatch while a repeat POST waits on its HEAD. |
| +type pausingGrants struct { |
| + *countingGrants |
| + key string |
| + armed atomic.Bool |
| + reached chan struct{} |
| + release chan struct{} |
| +} |
| + |
| +func (g *pausingGrants) Head(ctx context.Context, bucket, key string) (media.ObjectHead, error) { |
| + if key == g.key && g.armed.CompareAndSwap(true, false) { |
| + close(g.reached) |
| + <-g.release |
| + } |
| + return g.countingGrants.Head(ctx, bucket, key) |
| +} |
| + |
| +// TestRepeatPathAnswersAStoreRefusalDispatchMadeMeanwhile (Astra fix round |
| +// 2): a repeat POST reads the store record, finds its queued row and |
| +// pauses on the HEAD. Meanwhile the store changes and dispatch fails the |
| +// row on it. The repeat answers the door's refusal, not 200 with the row. |
| +func TestRepeatPathAnswersAStoreRefusalDispatchMadeMeanwhile(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + change func(f *mediaDoorFixture) |
| + status int |
| + code string |
| + msg string |
| + }{ |
| + {"store_writes_denied", func(f *mediaDoorFixture) { |
| + if _, err := objstore.SetWritesDenied(context.Background(), f.rel, f.sqid, "titogo", "", |
| + &objstore.WritesDenied{Reason: "storage grace ended", At: f.clock}); err != nil { |
| + f.t.Fatal(err) |
| + } |
| + }, 402, "store_writes_denied", mediaText("media.store_writes_denied")}, |
| + // A bucket made again carries no tag: dispatch verifies it afresh, |
| + // mints a new incarnation and fails the old row store_changed. |
| + {"store_changed", func(f *mediaDoorFixture) { f.buckets.Tags[f.bucket] = map[string]string{} }, |
| + 409, "store_changed", mediaText("media.job.store_changed")}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + f := newMediaDoor(t, "code-media-pause-"+tc.name) |
| + g := &pausingGrants{countingGrants: f.grants, key: mediaTestKey, reached: make(chan struct{}), release: make(chan struct{})} |
| + f.srv.cfg.MediaGrants = g |
| + sub := f.submit(mediaTestKey) |
| + if sub.status != http.StatusAccepted { |
| + t.Fatalf("submit %d %s", sub.status, sub.raw) |
| + } |
| + g.armed.Store(true) |
| + done := make(chan mediaReply, 1) |
| + go func() { |
| + status, header, raw := rawReq(t, http.MethodPost, f.url("titogo"), f.svc, `{"key":"`+mediaTestKey+`"}`) |
| + done <- decodeMediaReply(t, status, header, raw) |
| + }() |
| + select { |
| + case <-g.reached: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the repeat POST never reached its HEAD") |
| + } |
| + tc.change(f) |
| + if err := f.srv.media.Pass(context.Background(), f.clock); err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, err := f.st.db.Query(`SELECT state, error FROM media_jobs WHERE job_id = ?`, sub.job.ID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var state, code string |
| + for rows.Next() { |
| + if err := rows.Scan(&state, &code); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + rows.Close() |
| + if state != "failed" || code != tc.code { |
| + t.Fatalf("dispatch left the row %s %s, want failed %s", state, code, tc.code) |
| + } |
| + close(g.release) |
| + var r mediaReply |
| + select { |
| + case r = <-done: |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the repeat POST did not answer") |
| + } |
| + wantMediaRefusal(t, r, tc.status, tc.code, tc.msg) |
| + if n := len(f.engine.Executions()); n != 0 { |
| + t.Fatalf("%d executions", n) |
| + } |
| + }) |
| + } |
| +} |