2022-06-22 11:36:49 +00:00
|
|
|
package aria2
|
|
|
|
|
2022-06-23 09:06:17 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
_ "github.com/alist-org/alist/v3/drivers"
|
2022-06-25 12:38:02 +00:00
|
|
|
conf2 "github.com/alist-org/alist/v3/internal/conf"
|
2022-06-23 09:06:17 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
|
|
"github.com/alist-org/alist/v3/internal/operations"
|
|
|
|
"github.com/alist-org/alist/v3/pkg/task"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-06-25 12:38:02 +00:00
|
|
|
conf2.Conf = conf2.DefaultConfig()
|
2022-06-23 09:06:17 +00:00
|
|
|
absPath, err := filepath.Abs("../../data/temp")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-06-25 12:38:02 +00:00
|
|
|
conf2.Conf.TempDir = absPath
|
2022-06-23 09:06:17 +00:00
|
|
|
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to connect database")
|
|
|
|
}
|
2022-06-25 13:36:35 +00:00
|
|
|
db.Init(db)
|
2022-06-23 09:06:17 +00:00
|
|
|
}
|
2022-06-22 11:36:49 +00:00
|
|
|
|
|
|
|
func TestConnect(t *testing.T) {
|
|
|
|
err := InitAria2Client("http://localhost:16800/jsonrpc", "secret", 3)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to init aria2: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-06-23 09:06:17 +00:00
|
|
|
|
|
|
|
func TestDown(t *testing.T) {
|
|
|
|
TestConnect(t)
|
2022-07-10 06:45:39 +00:00
|
|
|
err := operations.CreateStorage(context.Background(), model.Storage{
|
2022-06-23 09:06:17 +00:00
|
|
|
ID: 0,
|
|
|
|
VirtualPath: "/",
|
|
|
|
Index: 0,
|
|
|
|
Driver: "Local",
|
|
|
|
Status: "",
|
|
|
|
Addition: `{"root_folder":"../../data"}`,
|
|
|
|
Remark: "",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-07-10 06:45:39 +00:00
|
|
|
t.Fatalf("failed to create storage: %+v", err)
|
2022-06-23 09:06:17 +00:00
|
|
|
}
|
|
|
|
err = AddURI(context.Background(), "https://nodejs.org/dist/index.json", "/test")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to add uri: %+v", err)
|
|
|
|
}
|
2022-06-23 13:24:23 +00:00
|
|
|
tasks := DownTaskManager.GetAll()
|
2022-06-23 09:06:17 +00:00
|
|
|
if len(tasks) != 1 {
|
|
|
|
t.Errorf("failed to get tasks: %+v", tasks)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
tsk := tasks[0]
|
|
|
|
t.Logf("task: %+v", tsk)
|
2022-06-29 10:36:14 +00:00
|
|
|
if tsk.GetState() == task.SUCCEEDED {
|
2022-06-23 09:06:17 +00:00
|
|
|
break
|
|
|
|
}
|
2022-06-23 13:09:54 +00:00
|
|
|
if tsk.GetState() == task.ERRORED {
|
2022-06-23 09:06:17 +00:00
|
|
|
t.Fatalf("failed to download: %+v", tsk)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
for {
|
2022-06-23 13:24:23 +00:00
|
|
|
if len(TransferTaskManager.GetAll()) == 0 {
|
2022-06-23 09:06:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-06-23 13:24:23 +00:00
|
|
|
tsk := TransferTaskManager.GetAll()[0]
|
2022-06-23 09:06:17 +00:00
|
|
|
t.Logf("task: %+v", tsk)
|
2022-06-29 10:36:14 +00:00
|
|
|
if tsk.GetState() == task.SUCCEEDED {
|
2022-06-23 09:06:17 +00:00
|
|
|
break
|
|
|
|
}
|
2022-06-23 13:09:54 +00:00
|
|
|
if tsk.GetState() == task.ERRORED {
|
2022-06-23 09:06:17 +00:00
|
|
|
t.Fatalf("failed to download: %+v", tsk)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|