fix(gemini-test): use ProgressCallback to make resume tests deterministic
This commit is contained in:
@@ -218,18 +218,29 @@ func TestGeminiResumeSaveMetadata(t *testing.T) {
|
|||||||
u, _ := url.Parse("gemini://127.0.0.1:" + strconv.Itoa(port) + "/")
|
u, _ := url.Parse("gemini://127.0.0.1:" + strconv.Itoa(port) + "/")
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
// 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)
|
||||||
errCh := make(chan error, 1)
|
errCh := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
_, err := proto.Download(ctx, &core.DownloadRequest{
|
_, err := 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 <- err
|
errCh <- err
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 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(150 * time.Millisecond)
|
<-progress
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
err = <-errCh
|
err = <-errCh
|
||||||
@@ -307,17 +318,26 @@ func TestGeminiResumeDownload(t *testing.T) {
|
|||||||
u, _ := url.Parse("gemini://127.0.0.1:" + strconv.Itoa(port) + "/data")
|
u, _ := url.Parse("gemini://127.0.0.1:" + strconv.Itoa(port) + "/data")
|
||||||
|
|
||||||
// First attempt: the server sends 500 bytes and blocks. We cancel
|
// First attempt: the server sends 500 bytes and blocks. We cancel
|
||||||
// the context, then the server closes the connection.
|
// the context, then the server closes the connection. progress
|
||||||
|
// signals after the first writer.Write so waiting on it replaces
|
||||||
|
// the previous fixed sleep with a deterministic barrier.
|
||||||
ctx1, cancel1 := context.WithCancel(context.Background())
|
ctx1, cancel1 := context.WithCancel(context.Background())
|
||||||
|
progress := make(chan int64, 1)
|
||||||
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{}{} // let server close connection
|
cancelReady <- struct{}{} // let server close connection
|
||||||
<-connReady
|
<-connReady
|
||||||
|
|||||||
Reference in New Issue
Block a user