From 058616de3841114249fa4c2bd1ce66c182375908 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Sun, 1 Nov 2026 04:02:22 +0300 Subject: [PATCH] test(transfer): cancellation inside a file and during Prepare (3.4) A cancel 150 ms into a slow 3 MiB overwrite returns within 200 ms with Report.Cancelled (not success), keeps the old target, copies nothing after it and leaves no temp (N-P1-7); a cancelled Prepare stops walking and writes nothing. Co-Authored-By: Claude Opus 5.5 --- internal/transfer/cancel_test.go | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 internal/transfer/cancel_test.go diff --git a/internal/transfer/cancel_test.go b/internal/transfer/cancel_test.go new file mode 100644 index 0000000..0fcc97b --- /dev/null +++ b/internal/transfer/cancel_test.go @@ -0,0 +1,96 @@ +package transfer + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "testing" + "time" + + "vcom/internal/testutil/fstree" + "vcom/internal/vfs" + "vcom/internal/vfs/vfstest" +) + +// slow sleeps on every op of the given kind (a slow link). +func slow(op vfstest.Op, d time.Duration) func(vfstest.Op, string) error { + return func(o vfstest.Op, _ string) error { + if o == op { + time.Sleep(d) + } + return nil + } +} + +func TestCancelMidFileReportsCancelled(t *testing.T) { + src, dst := layout(t, fstree.Spec{fstree.File("big.bin", "").WithSize(3 << 20), fstree.File("next", "n")}, + fstree.Spec{fstree.File("big.bin", "OLD")}) + to := &vfstest.FaultFS{FS: local, Hook: slow(vfstest.OpWrite, 5*time.Millisecond)} // ~0.5 s for 3 MiB + plan, err := Prepare(bg, req(Copy, local, join(src, "big.bin", "next"), to, dst, overwrite), nil) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(bg) + var cancelledAt time.Time + time.AfterFunc(150*time.Millisecond, func() { cancelledAt = time.Now(); cancel() }) + var last Progress + rep := Execute(ctx, plan, Hooks{Progress: func(p Progress) { last = p }}) + took := time.Since(cancelledAt) + if took > 200*time.Millisecond { + t.Fatalf("returned %v after cancel (N-P1-7)", took) + } + if !rep.Cancelled || rep.Done != 0 || rep.Err != nil { + t.Fatalf("report %+v: must say cancelled, not success", rep) + } + if read(t, filepath.Join(dst, "big.bin")) != "OLD" { + t.Fatal("old target damaged by a cancelled overwrite") + } + if _, err := os.Lstat(filepath.Join(dst, "next")); !os.IsNotExist(err) { + t.Fatal("items after the cancel were copied") + } + if last.BytesDone >= plan.TotalBytes { + t.Fatalf("progress claims completion: %+v", last) + } + noLeftovers(t, dst) +} + +func TestCancelDuringPrepare(t *testing.T) { + var spec fstree.Spec + for i := range 100 { + spec = append(spec, fstree.File(fmt.Sprintf("d/s%03d/f", i), "")) + } + src, dst := layout(t, spec, nil) + from := &vfstest.FaultFS{FS: local, Hook: slow(vfstest.OpReadDir, 20*time.Millisecond)} // ~2 s walk + ctx, cancel := context.WithCancel(bg) + time.AfterFunc(50*time.Millisecond, cancel) + t0 := time.Now() + plan, err := Prepare(ctx, req(Copy, from, join(src, "d"), local, dst), nil) + if !errors.Is(err, vfs.ErrCancelled) || plan != nil { + t.Fatalf("plan=%v err=%v, want cancelled", plan, err) + } + if el := time.Since(t0); el > 200*time.Millisecond { + t.Fatalf("Prepare took %v after a cancel at 50 ms", el) + } + if ents, _ := os.ReadDir(dst); len(ents) != 0 { + t.Fatal("Prepare wrote to the target") + } +} + +func TestCancelBeforeExecute(t *testing.T) { + src, dst := layout(t, fstree.Spec{fstree.File("a", "a")}, nil) + plan, err := Prepare(bg, req(Copy, local, join(src, "a"), local, dst), nil) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(bg) + cancel() + rep := Execute(ctx, plan, Hooks{}) + if !rep.Cancelled || rep.Done != 0 { + t.Fatalf("%+v", rep) + } + if ents, _ := os.ReadDir(dst); len(ents) != 0 { + t.Fatalf("wrote after cancel: %v", ents) + } +}