mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 14:53:44 +00:00
debug pause
This commit is contained in:
1 parent
fedf4a60e8
commit
3215955056
2 files changed
+52
-8
No files matched your search
@@ -76,8 +76,11 @@ func TestFetcher_DownloadContinue(t *testing.T) {
|
|||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
// 暂停继续
|
// 暂停继续
|
||||||
downloadContinue(listener, 1, t)
|
downloadContinue(listener, 1, t)
|
||||||
|
fmt.Println("d1")
|
||||||
downloadContinue(listener, 5, t)
|
downloadContinue(listener, 5, t)
|
||||||
|
fmt.Println("d5")
|
||||||
downloadContinue(listener, 8, t)
|
downloadContinue(listener, 8, t)
|
||||||
|
fmt.Println("d8")
|
||||||
downloadContinue(listener, 16, t)
|
downloadContinue(listener, 16, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +99,13 @@ func TestFetcher_DownloadRetry(t *testing.T) {
|
|||||||
downloadNormal(listener, 1, 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 {
|
func startTestFileServer() net.Listener {
|
||||||
return startTestServer(func() http.Handler {
|
return startTestServer(func() http.Handler {
|
||||||
return http.FileServer(http.Dir(testDir))
|
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 {
|
func startTestServer(serverHandle func() http.Handler) net.Listener {
|
||||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
if err != nil {
|
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 {
|
func fileMd5(filePath string) string {
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -78,11 +78,7 @@ func (p *Process) Pause() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
p.status = common.DownloadStatusPause
|
p.status = common.DownloadStatusPause
|
||||||
if len(p.clients) > 0 {
|
p.stop()
|
||||||
for _, client := range p.clients {
|
|
||||||
client.Body.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 释放锁,并等待下载结束
|
// 释放锁,并等待下载结束
|
||||||
p.pauseCond.Wait()
|
p.pauseCond.Wait()
|
||||||
return nil
|
return nil
|
||||||
@@ -143,11 +139,15 @@ func (p *Process) fetch() error {
|
|||||||
var retErr error
|
var retErr error
|
||||||
for i := 0; i < p.opts.Connections; i++ {
|
for i := 0; i < p.opts.Connections; i++ {
|
||||||
err := <-errCh
|
err := <-errCh
|
||||||
if retErr != nil {
|
if err != nil && retErr == nil {
|
||||||
retErr = err
|
retErr = err
|
||||||
// 有一个连接失败就立即终止下载
|
// 有一个连接失败就立即终止下载
|
||||||
if retErr != common.PauseErr {
|
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
|
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) {
|
func (p *Process) fetchChunk(index int, name string, chunk *model.Chunk) (err error) {
|
||||||
httpReq, err := BuildRequest(p.res.Req)
|
httpReq, err := BuildRequest(p.res.Req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,11 +197,11 @@ func (p *Process) fetchChunk(index int, name string, chunk *model.Chunk) (err er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
p.clients[index] = resp
|
||||||
if resp.StatusCode != common.HttpCodeOK && resp.StatusCode != common.HttpCodePartialContent {
|
if resp.StatusCode != common.HttpCodeOK && resp.StatusCode != common.HttpCodePartialContent {
|
||||||
err = NewRequestError(resp.StatusCode, resp.Status)
|
err = NewRequestError(resp.StatusCode, resp.Status)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.clients[index] = resp
|
|
||||||
return nil
|
return nil
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user