mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-19 14:53:44 +00:00
fix: batch create tasks with duplicate names (#635)
This commit is contained in:
6 files changed
+52
-17
No files matched your search
@@ -2,12 +2,12 @@ package test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/armon/go-socks5"
|
"github.com/armon/go-socks5"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|||||||
@@ -94,6 +94,9 @@ func (o *Options) InitSelectFiles(fileSize int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *Options) Clone() *Options {
|
func (o *Options) Clone() *Options {
|
||||||
|
if o == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return &Options{
|
return &Options{
|
||||||
Name: o.Name,
|
Name: o.Name,
|
||||||
Path: o.Path,
|
Path: o.Path,
|
||||||
|
|||||||
@@ -332,7 +332,7 @@ func (d *Downloader) CreateDirect(req *base.Request, opts *base.Options) (taskId
|
|||||||
func (d *Downloader) CreateDirectBatch(reqs []*base.Request, opts *base.Options) (taskId []string, err error) {
|
func (d *Downloader) CreateDirectBatch(reqs []*base.Request, opts *base.Options) (taskId []string, err error) {
|
||||||
taskIds := make([]string, 0)
|
taskIds := make([]string, 0)
|
||||||
for _, req := range reqs {
|
for _, req := range reqs {
|
||||||
taskId, err := d.CreateDirect(req, opts)
|
taskId, err := d.CreateDirect(req, opts.Clone())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -492,8 +492,10 @@ func (d *Downloader) DeleteByStatues(statues []base.Status, force bool) (err err
|
|||||||
}
|
}
|
||||||
|
|
||||||
deleteIds := make([]string, 0)
|
deleteIds := make([]string, 0)
|
||||||
|
deleteTasksPtr := make([]*Task, 0)
|
||||||
for _, task := range deleteTasks {
|
for _, task := range deleteTasks {
|
||||||
deleteIds = append(deleteIds, task.ID)
|
deleteIds = append(deleteIds, task.ID)
|
||||||
|
deleteTasksPtr = append(deleteTasksPtr, task)
|
||||||
}
|
}
|
||||||
func() {
|
func() {
|
||||||
d.lock.Lock()
|
d.lock.Lock()
|
||||||
@@ -515,7 +517,7 @@ func (d *Downloader) DeleteByStatues(statues []base.Status, force bool) (err err
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, task := range deleteTasks {
|
for _, task := range deleteTasksPtr {
|
||||||
err = d.doDelete(task, force)
|
err = d.doDelete(task, force)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/GopeedLab/gopeed/pkg/protocol/http"
|
"github.com/GopeedLab/gopeed/pkg/protocol/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -92,15 +94,33 @@ func TestDownloader_CreateDirectBatch(t *testing.T) {
|
|||||||
if err := downloader.Setup(); err != nil {
|
if err := downloader.Setup(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer downloader.Clear()
|
defer func() {
|
||||||
|
downloader.DeleteByStatues(nil, true)
|
||||||
|
downloader.Clear()
|
||||||
|
}()
|
||||||
|
|
||||||
reqs := make([]*base.Request, 0)
|
reqs := make([]*base.Request, 0)
|
||||||
for i := 0; i < 6; i++ {
|
fileNames := make([]string, 0)
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
req := &base.Request{
|
req := &base.Request{
|
||||||
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||||
}
|
}
|
||||||
reqs = append(reqs, req)
|
reqs = append(reqs, req)
|
||||||
|
if i == 0 {
|
||||||
|
fileNames = append(fileNames, test.DownloadName)
|
||||||
|
} else {
|
||||||
|
arr := strings.Split(test.DownloadName, ".")
|
||||||
|
fileNames = append(fileNames, arr[0]+" ("+strconv.Itoa(i)+")."+arr[1])
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(len(reqs))
|
||||||
|
downloader.Listener(func(event *Event) {
|
||||||
|
if event.Key == EventKeyDone {
|
||||||
|
wg.Done()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
_, err := downloader.CreateDirectBatch(reqs, &base.Options{
|
_, err := downloader.CreateDirectBatch(reqs, &base.Options{
|
||||||
Path: test.Dir,
|
Path: test.Dir,
|
||||||
@@ -113,10 +133,28 @@ func TestDownloader_CreateDirectBatch(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
tasks := downloader.GetTasks()
|
tasks := downloader.GetTasks()
|
||||||
if len(tasks) != len(reqs) {
|
if len(tasks) != len(reqs) {
|
||||||
t.Errorf("CreateDirectBatch() got = %v, want %v", len(tasks), len(reqs))
|
t.Errorf("CreateDirectBatch() task got = %v, want %v", len(tasks), len(reqs))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nameMatchMap := make(map[string]bool)
|
||||||
|
for _, name := range fileNames {
|
||||||
|
if _, err := os.Stat(test.Dir + "/" + name); os.IsNotExist(err) {
|
||||||
|
t.Errorf("CreateDirectBatch() file not exist: %v", name)
|
||||||
|
}
|
||||||
|
for _, task := range tasks {
|
||||||
|
if name == task.Meta.Opts.Name {
|
||||||
|
nameMatchMap[task.Meta.Opts.Name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(nameMatchMap) != len(reqs) {
|
||||||
|
t.Errorf("CreateDirectBatch() names got = %v, want %v", len(nameMatchMap), len(reqs))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDownloader_CreateWithProxy(t *testing.T) {
|
func TestDownloader_CreateWithProxy(t *testing.T) {
|
||||||
|
|||||||
+1
-1
@@ -154,7 +154,7 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
defer func() {
|
defer func() {
|
||||||
if v := recover(); v != nil {
|
if v := recover(); v != nil {
|
||||||
err := errors.WithStack(fmt.Errorf("%v", v))
|
err := errors.WithStack(fmt.Errorf("%v", v))
|
||||||
Downloader.Logger.Error().Stack().Err(err).Msg("http server panic")
|
Downloader.Logger.Error().Stack().Err(err).Msgf("http server panic: %s %s", r.Method, r.RequestURI)
|
||||||
WriteJson(w, model.NewErrorResult(err.Error(), model.CodeError))
|
WriteJson(w, model.NewErrorResult(err.Error(), model.CodeError))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
+2
-10
@@ -140,7 +140,7 @@ func TestCreateDirectTask(t *testing.T) {
|
|||||||
func TestCreateDirectTaskBatch(t *testing.T) {
|
func TestCreateDirectTaskBatch(t *testing.T) {
|
||||||
doTest(func() {
|
doTest(func() {
|
||||||
reqs := make([]*base.Request, 0)
|
reqs := make([]*base.Request, 0)
|
||||||
for i := 0; i < 6; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
reqs = append(reqs, createReq.Req)
|
reqs = append(reqs, createReq.Req)
|
||||||
}
|
}
|
||||||
taskIds := httpRequestCheckOk[[]string](http.MethodPost, "/api/v1/tasks/batch", &model.CreateTaskBatch{
|
taskIds := httpRequestCheckOk[[]string](http.MethodPost, "/api/v1/tasks/batch", &model.CreateTaskBatch{
|
||||||
@@ -583,15 +583,7 @@ func doTest(handler func()) {
|
|||||||
}()
|
}()
|
||||||
defer func() {
|
defer func() {
|
||||||
Downloader.PauseAll()
|
Downloader.PauseAll()
|
||||||
|
Downloader.DeleteByStatues(nil, true)
|
||||||
tasks := Downloader.GetTasks()
|
|
||||||
taskIds := make([]string, len(tasks))
|
|
||||||
for i, task := range tasks {
|
|
||||||
taskIds[i] = task.ID
|
|
||||||
}
|
|
||||||
for _, id := range taskIds {
|
|
||||||
Downloader.Delete(id, true)
|
|
||||||
}
|
|
||||||
os.RemoveAll(cfg.StorageDir)
|
os.RemoveAll(cfg.StorageDir)
|
||||||
}()
|
}()
|
||||||
taskReq.URL = "http://" + fileListener.Addr().String() + "/" + test.BuildName
|
taskReq.URL = "http://" + fileListener.Addr().String() + "/" + test.BuildName
|
||||||
|
|||||||
Reference in new issue
Block a user