113 lines
4.4 KiB
Go
113 lines
4.4 KiB
Go
package libraryfreeze
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const r30digest = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
|
|
// 第一次进窗在"未写任何内容"的情况下失败(例如预检/预备份前就断了),窗口已按失败收尾。
|
|
// 同一个作业随后 resume:必须能开**新一次尝试**,而不是被 "freeze request already exists" 挡住。
|
|
func TestR30ResumeAfterClosedWindowWithoutContentRisk(t *testing.T) {
|
|
root, c, e := instanceFixture(t)
|
|
if _, err := e.RunWindow(context.Background(), "job-r30-b", func(context.Context) error { return nil },
|
|
func(ctx context.Context, w *Window) (string, error) {
|
|
return "", errors.New("preflight failed before any target write")
|
|
}, func(error) bool { return true }); err == nil {
|
|
t.Fatal("first window should fail")
|
|
}
|
|
first, err := e.load("job-r30-b")
|
|
if err != nil {
|
|
t.Fatalf("first journal: %v", err)
|
|
}
|
|
if first.State != "failed" || first.ContentRisk {
|
|
t.Fatalf("fixture shape wrong: state=%s contentRisk=%v", first.State, first.ContentRisk)
|
|
}
|
|
|
|
callsBefore := len(c.calls)
|
|
result, err := e.RunWindow(context.Background(), "job-r30-b", func(context.Context) error { return nil },
|
|
func(ctx context.Context, w *Window) (string, error) {
|
|
if err := w.MarkContentRisk(ctx); err != nil {
|
|
return "", err
|
|
}
|
|
return r30digest, nil
|
|
}, func(error) bool { return true })
|
|
if err != nil {
|
|
t.Fatalf("resume after closed window must be allowed, got: %v", err)
|
|
}
|
|
if result.State != "succeeded" || result.SnapshotID != r30digest {
|
|
t.Fatalf("result=%+v", result)
|
|
}
|
|
if len(c.calls) == callsBefore {
|
|
t.Fatal("second attempt did not actually run the window")
|
|
}
|
|
journal, err := e.load("job-r30-b")
|
|
if err != nil {
|
|
t.Fatalf("journal after resume: %v", err)
|
|
}
|
|
if journal.Job != "job-r30-b" || journal.Attempt != 2 {
|
|
t.Fatalf("new attempt must bind same job with attempt=2: job=%q attempt=%d", journal.Job, journal.Attempt)
|
|
}
|
|
if journal.SupersededFrom != "archive/job-r30-b-attempt1.json" {
|
|
t.Fatalf("previous closed request must be referenced, not silently dropped: %q", journal.SupersededFrom)
|
|
}
|
|
raw, err := os.ReadFile(filepath.Join(root, journal.SupersededFrom))
|
|
if err != nil {
|
|
t.Fatalf("archived previous journal missing: %v", err)
|
|
}
|
|
if !strings.Contains(string(raw), `"state":"failed"`) {
|
|
t.Fatalf("archived journal must keep the closed state: %s", raw)
|
|
}
|
|
}
|
|
|
|
// 已经在窗口内写过目标内容(ContentRisk)却失败的作业,不许开新窗口 —— 必须走恢复。
|
|
func TestR30RefusesNewAttemptWhenContentRiskRecorded(t *testing.T) {
|
|
_, _, e := instanceFixture(t)
|
|
if _, err := e.RunWindow(context.Background(), "job-r30-c", func(context.Context) error { return nil },
|
|
func(ctx context.Context, w *Window) (string, error) {
|
|
if err := w.MarkContentRisk(ctx); err != nil {
|
|
return "", err
|
|
}
|
|
return "", errors.New("write failed mid way")
|
|
}, func(error) bool { return true }); err == nil {
|
|
t.Fatal("first window should fail")
|
|
}
|
|
_, err := e.RunWindow(context.Background(), "job-r30-c", func(context.Context) error { return nil },
|
|
func(context.Context, *Window) (string, error) { return r30digest, nil }, func(error) bool { return true })
|
|
if err == nil || !strings.Contains(err.Error(), "already exists") {
|
|
t.Fatalf("content-risk failure must still require recovery, got: %v", err)
|
|
}
|
|
}
|
|
|
|
// 已成功收尾的窗口:再跑一次只回放结果,不重复副作用。
|
|
func TestR30SucceededWindowIsNotReplayed(t *testing.T) {
|
|
_, c, e := instanceFixture(t)
|
|
if _, err := e.RunWindow(context.Background(), "job-r30-d", func(context.Context) error { return nil },
|
|
func(ctx context.Context, w *Window) (string, error) {
|
|
if err := w.MarkContentRisk(ctx); err != nil {
|
|
return "", err
|
|
}
|
|
return r30digest, nil
|
|
}, func(error) bool { return true }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
callsBefore := len(c.calls)
|
|
replay, err := e.RunWindow(context.Background(), "job-r30-d", func(context.Context) error { return nil },
|
|
func(context.Context, *Window) (string, error) { return strings.Repeat("c", 64), nil },
|
|
func(error) bool { return true })
|
|
if err != nil {
|
|
t.Fatalf("replay: %v", err)
|
|
}
|
|
if replay.State != "succeeded" || replay.SnapshotID != r30digest {
|
|
t.Fatalf("replay must return original result, got %+v", replay)
|
|
}
|
|
if len(c.calls) != callsBefore {
|
|
t.Fatalf("replay must not touch services again: %v", c.calls)
|
|
}
|
|
}
|