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 <noreply@anthropic.com>
This commit is contained in:
parent
4bb60d074f
commit
058616de38
1 changed files with 96 additions and 0 deletions
96
internal/transfer/cancel_test.go
Normal file
96
internal/transfer/cancel_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue