debug pause

This commit is contained in:
liwei committed 2020-08-20 23:43:27 +08:00
1 parent fedf4a60e8
commit 3215955056
2 files changed
+52 -8

No files matched your search

+33
View File
@@ -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 {
+19 -8
View File
@@ -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 {