diff --git a/copy.go b/copy.go index 6617bda..b4b8db1 100644 --- a/copy.go +++ b/copy.go @@ -83,12 +83,12 @@ func (c *Copyer) copy(ctx context.Context, prepared <-chan *writeJob) <-chan *ba func (c *Copyer) write(ctx context.Context, job *writeJob, ch chan<- *baseJob, cntr *counter, noSpaceDevices mapset.Set[string]) { job.setStatus(jobStatusCopying) - defer job.setStatus(jobStatusFinishing) var wg sync.WaitGroup defer func() { wg.Wait() job.done() + job.setStatus(jobStatusFinishing) ch <- job.baseJob }() diff --git a/copy_test.go b/copy_test.go index c70b69f..63c5a83 100644 --- a/copy_test.go +++ b/copy_test.go @@ -1,11 +1,15 @@ package acp import ( + "bytes" "context" + "io" "os" "path/filepath" "testing" "time" + + mapset "github.com/deckarep/golang-set/v2" ) func TestCopyEmptyFile(t *testing.T) { @@ -73,3 +77,20 @@ func TestCopyEmptyFile(t *testing.T) { }) } } + +func TestWritePublishesFinishingJob(t *testing.T) { + // Build a target-free write Job so only the worker-to-cleanup handoff is exercised. + copyer := &Copyer{option: newOption(), eventCh: make(chan Event, 8)} + job := newWriteJob(&baseJob{ + copyer: copyer, + src: &source{}, + stat: &stat{}, + }, io.NopCloser(bytes.NewReader(nil)), 0, false) + completed := make(chan *baseJob, 1) + + // The copy worker must finish all mutations before publishing ownership to cleanup. + copyer.write(context.Background(), job, completed, new(counter), mapset.NewSet[string]()) + if status := (<-completed).status; status != jobStatusFinishing { + t.Fatalf("published status = %q, want %q", status, jobStatusFinishing) + } +}