fix(gopher-test): use ProgressCallback to make resume tests deterministic
This commit is contained in:
@@ -145,19 +145,31 @@ func TestGopherResumeSaveMetadata(t *testing.T) {
|
|||||||
proto := NewProtocol()
|
proto := NewProtocol()
|
||||||
u, _ := url.Parse("gopher://127.0.0.1:" + strconv.Itoa(port) + "/page")
|
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())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
_, dlErr := proto.Download(ctx, &core.DownloadRequest{
|
_, dlErr := proto.Download(ctx, &core.DownloadRequest{
|
||||||
URL: u,
|
URL: u,
|
||||||
Output: outPath,
|
Output: outPath,
|
||||||
|
ProgressCallback: func(current, total int64, speed float64) {
|
||||||
|
select {
|
||||||
|
case progress <- current:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
errCh <- dlErr
|
errCh <- dlErr
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Cancel mid-download. The slow server keeps the connection open
|
// Wait until the first byte has been written to disk, then cancel.
|
||||||
// long enough for the cancellation to fire between reads.
|
// No fixed sleep — this is robust against slow CI schedulers.
|
||||||
time.Sleep(50 * time.Millisecond)
|
<-progress
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
dlErr := <-errCh
|
dlErr := <-errCh
|
||||||
@@ -243,16 +255,25 @@ func TestGopherResumeDownload(t *testing.T) {
|
|||||||
u, _ := url.Parse("gopher://127.0.0.1:" + strconv.Itoa(port) + "/foo")
|
u, _ := url.Parse("gopher://127.0.0.1:" + strconv.Itoa(port) + "/foo")
|
||||||
|
|
||||||
// First attempt: send only the first line, then cancel.
|
// 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())
|
ctx1, cancel1 := context.WithCancel(context.Background())
|
||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
_, dlErr := proto.Download(ctx1, &core.DownloadRequest{
|
_, dlErr := proto.Download(ctx1, &core.DownloadRequest{
|
||||||
URL: u,
|
URL: u,
|
||||||
Output: outPath,
|
Output: outPath,
|
||||||
|
ProgressCallback: func(current, total int64, speed float64) {
|
||||||
|
select {
|
||||||
|
case progress <- current:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
},
|
||||||
})
|
})
|
||||||
errCh <- dlErr
|
errCh <- dlErr
|
||||||
}()
|
}()
|
||||||
time.Sleep(100 * time.Millisecond)
|
<-progress
|
||||||
cancel1()
|
cancel1()
|
||||||
cancelReady <- struct{}{}
|
cancelReady <- struct{}{}
|
||||||
<-connReady
|
<-connReady
|
||||||
|
|||||||
Reference in New Issue
Block a user