From aedcae840dcee9609e8090e5c05f5470b93d404a Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 23 Jun 2022 17:06:17 +0800 Subject: [PATCH] test(aria2): download and transfer file --- internal/aria2/add.go | 4 +- internal/aria2/aria2_test.go | 78 +++++++++++++++++++++++++++++++++++- internal/aria2/monitor.go | 3 +- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/internal/aria2/add.go b/internal/aria2/add.go index b082ec3d..8cb4f933 100644 --- a/internal/aria2/add.go +++ b/internal/aria2/add.go @@ -25,13 +25,13 @@ func AddURI(ctx context.Context, uri string, dstDirPath string) error { // check path is valid obj, err := operations.Get(ctx, account, dstDirActualPath) if err != nil { - if !errs.IsErrObjectNotFound(err) { + if !errs.IsObjectNotFound(err) { return errors.WithMessage(err, "failed get object") } } else { if !obj.IsDir() { // can't add to a file - return errors.WithStack(errs.ErrNotFolder) + return errors.WithStack(errs.NotFolder) } } // call aria2 rpc diff --git a/internal/aria2/aria2_test.go b/internal/aria2/aria2_test.go index da44d05d..610381e2 100644 --- a/internal/aria2/aria2_test.go +++ b/internal/aria2/aria2_test.go @@ -1,6 +1,33 @@ package aria2 -import "testing" +import ( + "context" + "github.com/alist-org/alist/v3/conf" + _ "github.com/alist-org/alist/v3/drivers" + "github.com/alist-org/alist/v3/internal/model" + "github.com/alist-org/alist/v3/internal/operations" + "github.com/alist-org/alist/v3/internal/store" + "github.com/alist-org/alist/v3/pkg/task" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "path/filepath" + "testing" + "time" +) + +func init() { + conf.Conf = conf.DefaultConfig() + absPath, err := filepath.Abs("../../data/temp") + if err != nil { + panic(err) + } + conf.Conf.TempDir = absPath + db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } + store.Init(db) +} func TestConnect(t *testing.T) { err := InitAria2Client("http://localhost:16800/jsonrpc", "secret", 3) @@ -8,3 +35,52 @@ func TestConnect(t *testing.T) { t.Errorf("failed to init aria2: %+v", err) } } + +func TestDown(t *testing.T) { + TestConnect(t) + err := operations.CreateAccount(context.Background(), model.Account{ + ID: 0, + VirtualPath: "/", + Index: 0, + Driver: "Local", + Status: "", + Addition: `{"root_folder":"../../data"}`, + Remark: "", + }) + if err != nil { + t.Fatalf("failed to create account: %+v", err) + } + err = AddURI(context.Background(), "https://nodejs.org/dist/index.json", "/test") + if err != nil { + t.Errorf("failed to add uri: %+v", err) + } + tasks := downTaskManager.GetAll() + if len(tasks) != 1 { + t.Errorf("failed to get tasks: %+v", tasks) + } + for { + tsk := tasks[0] + t.Logf("task: %+v", tsk) + if tsk.Status == task.FINISHED { + break + } + if tsk.Status == task.ERRORED { + t.Fatalf("failed to download: %+v", tsk) + } + time.Sleep(time.Second) + } + for { + if len(transferTaskManager.GetAll()) == 0 { + continue + } + tsk := transferTaskManager.GetAll()[0] + t.Logf("task: %+v", tsk) + if tsk.Status == task.FINISHED { + break + } + if tsk.Status == task.ERRORED { + t.Fatalf("failed to download: %+v", tsk) + } + time.Sleep(time.Second) + } +} diff --git a/internal/aria2/monitor.go b/internal/aria2/monitor.go index 3941963f..f0d21843 100644 --- a/internal/aria2/monitor.go +++ b/internal/aria2/monitor.go @@ -99,6 +99,7 @@ var transferTaskManager = task.NewTaskManager[uint64](3, func(k *uint64) { func (m *Monitor) Complete() error { // check dstDir again account, dstDirActualPath, err := operations.GetAccountAndActualPath(m.dstDirPath) + println("dstDirActualPath:", dstDirActualPath) if err != nil { return errors.WithMessage(err, "failed get account") } @@ -119,7 +120,7 @@ func (m *Monitor) Complete() error { }() for _, file := range files { transferTaskManager.Submit(task.WithCancelCtx[uint64](&task.Task[uint64]{ - Name: fmt.Sprintf("transfer %s to %s", file.Path, m.dstDirPath), + Name: fmt.Sprintf("transfer %s to [%s](%s)", file.Path, account.GetAccount().VirtualPath, dstDirActualPath), Func: func(tsk *task.Task[uint64]) error { defer wg.Done() size, _ := strconv.ParseInt(file.Length, 10, 64)