From 3215955056fc2887b12415ada749ac009ee4de1c Mon Sep 17 00:00:00 2001 From: liwei Date: Thu, 20 Aug 2020 23:43:27 +0800 Subject: [PATCH] debug pause --- download/http/fetcher_test.go | 33 +++++++++++++++++++++++++++++++++ download/http/process.go | 27 +++++++++++++++++++-------- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/download/http/fetcher_test.go b/download/http/fetcher_test.go index c95402f3..941fa90d 100644 --- a/download/http/fetcher_test.go +++ b/download/http/fetcher_test.go @@ -76,8 +76,11 @@ func TestFetcher_DownloadContinue(t *testing.T) { defer listener.Close() // 暂停继续 downloadContinue(listener, 1, t) + fmt.Println("d1") downloadContinue(listener, 5, t) + fmt.Println("d5") downloadContinue(listener, 8, t) + fmt.Println("d8") downloadContinue(listener, 16, t) } @@ -96,6 +99,13 @@ func TestFetcher_DownloadRetry(t *testing.T) { downloadNormal(listener, 1, t) } +func TestFetcher_DownloadError(t *testing.T) { + listener := startTestErrorServer() + defer listener.Close() + // chunked编码下载 + downloadError(listener, 1, t) +} + func startTestFileServer() net.Listener { return startTestServer(func() http.Handler { return http.FileServer(http.Dir(testDir)) @@ -138,6 +148,21 @@ func startTestRetryServer() net.Listener { }) } +func startTestErrorServer() net.Listener { + counter := 0 + return startTestServer(func() http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("/"+buildName, func(writer http.ResponseWriter, request *http.Request) { + counter++ + if counter != 1 { + writer.WriteHeader(500) + return + } + }) + return mux + }) +} + func startTestServer(serverHandle func() http.Handler) net.Listener { listener, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { @@ -255,6 +280,14 @@ func downloadContinue(listener net.Listener, connections int, t *testing.T) { } } +func downloadError(listener net.Listener, connections int, t *testing.T) { + process := downloadReady(listener, connections, t) + err := process.Start() + if err == nil { + t.Errorf("Download error = %v, want %v", err, nil) + } +} + func fileMd5(filePath string) string { file, err := os.Open(filePath) if err != nil { diff --git a/download/http/process.go b/download/http/process.go index 414e41a0..b6120c2b 100644 --- a/download/http/process.go +++ b/download/http/process.go @@ -78,11 +78,7 @@ func (p *Process) Pause() error { return nil } p.status = common.DownloadStatusPause - if len(p.clients) > 0 { - for _, client := range p.clients { - client.Body.Close() - } - } + p.stop() // 释放锁,并等待下载结束 p.pauseCond.Wait() return nil @@ -143,11 +139,15 @@ func (p *Process) fetch() error { var retErr error for i := 0; i < p.opts.Connections; i++ { err := <-errCh - if retErr != nil { + if err != nil && retErr == nil { retErr = err // 有一个连接失败就立即终止下载 if retErr != common.PauseErr { - p.Pause() + func() { + p.pauseCond.L.Lock() + defer p.pauseCond.L.Unlock() + p.stop() + }() } } } @@ -155,6 +155,17 @@ func (p *Process) fetch() error { return retErr } +func (p *Process) stop() { + if len(p.clients) > 0 { + for _, client := range p.clients { + fmt.Println("client close") + if client != nil { + client.Body.Close() + } + } + } +} + func (p *Process) fetchChunk(index int, name string, chunk *model.Chunk) (err error) { httpReq, err := BuildRequest(p.res.Req) if err != nil { @@ -186,11 +197,11 @@ func (p *Process) fetchChunk(index int, name string, chunk *model.Chunk) (err er if err != nil { return err } + p.clients[index] = resp if resp.StatusCode != common.HttpCodeOK && resp.StatusCode != common.HttpCodePartialContent { err = NewRequestError(resp.StatusCode, resp.Status) return err } - p.clients[index] = resp return nil }() if err != nil {