diff --git a/internal/protocol/gopher/gopher_test.go b/internal/protocol/gopher/gopher_test.go index 4e7dc3c..004b0b9 100644 --- a/internal/protocol/gopher/gopher_test.go +++ b/internal/protocol/gopher/gopher_test.go @@ -145,19 +145,31 @@ func TestGopherResumeSaveMetadata(t *testing.T) { proto := NewProtocol() u, _ := url.Parse("gopher://127.0.0.1:" + strconv.Itoa(port) + "/page") + // progress is signalled every time the download loop calls + // req.ProgressCallback — which happens *after* a successful + // writer.Write. Reading from it is therefore a deterministic + // barrier waiting for the first byte to land on disk. + progress := make(chan int64, 1) + ctx, cancel := context.WithCancel(context.Background()) errCh := make(chan error, 1) go func() { _, dlErr := proto.Download(ctx, &core.DownloadRequest{ URL: u, Output: outPath, + ProgressCallback: func(current, total int64, speed float64) { + select { + case progress <- current: + default: + } + }, }) errCh <- dlErr }() - // Cancel mid-download. The slow server keeps the connection open - // long enough for the cancellation to fire between reads. - time.Sleep(50 * time.Millisecond) + // Wait until the first byte has been written to disk, then cancel. + // No fixed sleep — this is robust against slow CI schedulers. + <-progress cancel() dlErr := <-errCh @@ -243,16 +255,25 @@ func TestGopherResumeDownload(t *testing.T) { u, _ := url.Parse("gopher://127.0.0.1:" + strconv.Itoa(port) + "/foo") // First attempt: send only the first line, then cancel. + // progress signals after the first writer.Write, so waiting on + // it replaces the previous fixed sleep with a deterministic barrier. + progress := make(chan int64, 1) ctx1, cancel1 := context.WithCancel(context.Background()) errCh := make(chan error, 1) go func() { _, dlErr := proto.Download(ctx1, &core.DownloadRequest{ URL: u, Output: outPath, + ProgressCallback: func(current, total int64, speed float64) { + select { + case progress <- current: + default: + } + }, }) errCh <- dlErr }() - time.Sleep(100 * time.Millisecond) + <-progress cancel1() cancelReady <- struct{}{} <-connReady