fix: undo locks its history entries and marks them only once (WR-11)
Two concurrent undos of one entry both passed the already-undone check and both restored. Undo and UndoBatch now read the entries FOR UPDATE, and the final mark is conditional on undone_at IS NULL, refusing with already_undone when another undo got there first.
This commit is contained in:
@@ -817,3 +817,60 @@ func TestAddressChangeFromEvent(t *testing.T) {
|
||||
t.Fatalf("redirects after the admin save = %+v, want both old addresses on /pages/delta", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConcurrentUndo checks that undo is idempotent under concurrent
|
||||
// requests (WR-11): while one transaction holds the undo of a deleted
|
||||
// redirect uncommitted, a second undo of the same entry waits, then is
|
||||
// refused as already undone; the redirect is re-created once.
|
||||
func TestConcurrentUndo(t *testing.T) {
|
||||
env := bootEnv(t)
|
||||
svc := env.seoPlugin(t).Service()
|
||||
r := models.NewRedirect()
|
||||
r.Source, r.Target = "/undo-twice", "/x"
|
||||
if err := env.db.Transaction(func(tx *gorm.DB) error {
|
||||
if _, err := svc.SaveRedirect(t.Context(), tx, &r); err != nil {
|
||||
return err
|
||||
}
|
||||
return svc.DeleteRedirect(t.Context(), tx, r)
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var revID uint
|
||||
if err := env.db.Raw(`SELECT id FROM golem15_seo_revisions WHERE subject_kind = 'redirect' ORDER BY id DESC LIMIT 1`).Scan(&revID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
first := env.db.Begin()
|
||||
if _, err := svc.Undo(t.Context(), first, revID); err != nil {
|
||||
first.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- env.db.Transaction(func(tx *gorm.DB) error {
|
||||
_, err := svc.Undo(context.Background(), tx, revID)
|
||||
return err
|
||||
})
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
first.Rollback()
|
||||
t.Fatalf("second undo finished while the first was uncommitted: %v", err)
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
}
|
||||
if err := first.Commit().Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var refusal *classes.UndoRefusal
|
||||
if err := <-done; !errors.As(err, &refusal) || refusal.Code != classes.UndoDone {
|
||||
t.Fatalf("second undo: err = %v, want already_undone", err)
|
||||
}
|
||||
var n int64
|
||||
if err := env.db.Raw(`SELECT COUNT(*) FROM golem15_seo_redirects WHERE source = '/undo-twice'`).Scan(&n).Error; err != nil || n != 1 {
|
||||
t.Fatalf("redirects of /undo-twice = %d (err %v), want 1", n, err)
|
||||
}
|
||||
var undos int64
|
||||
if err := env.db.Raw(`SELECT COUNT(*) FROM golem15_seo_revisions WHERE via = 'undo'`).Scan(&undos).Error; err != nil || undos != 1 {
|
||||
t.Fatalf("undo history rows = %d (err %v), want 1", undos, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user