alist/internal/aria2/aria2_test.go

88 lines
1.9 KiB
Go
Raw Normal View History

2022-06-22 11:36:49 +00:00
package aria2
import (
"context"
2022-07-18 03:37:53 +00:00
"path/filepath"
"testing"
"time"
_ "github.com/alist-org/alist/v3/drivers"
2022-06-25 12:38:02 +00:00
conf2 "github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/task"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func init() {
2022-06-25 12:38:02 +00:00
conf2.Conf = conf2.DefaultConfig()
absPath, err := filepath.Abs("../../data/temp")
if err != nil {
panic(err)
}
2022-06-25 12:38:02 +00:00
conf2.Conf.TempDir = absPath
dB, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.Init(dB)
}
2022-06-22 11:36:49 +00:00
func TestConnect(t *testing.T) {
2022-07-18 03:37:53 +00:00
_, err := InitAria2Client("http://localhost:16800/jsonrpc", "secret", 3)
2022-06-22 11:36:49 +00:00
if err != nil {
t.Errorf("failed to init aria2: %+v", err)
}
}
func TestDown(t *testing.T) {
TestConnect(t)
err := op.CreateStorage(context.Background(), model.Storage{
ID: 0,
MountPath: "/",
Index: 0,
2022-08-30 13:52:06 +00:00
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)
}
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()
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 {
break
}
2022-06-23 13:09:54 +00:00
if tsk.GetState() == task.ERRORED {
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 {
continue
}
2022-06-23 13:24:23 +00:00
tsk := TransferTaskManager.GetAll()[0]
t.Logf("task: %+v", tsk)
2022-06-29 10:36:14 +00:00
if tsk.GetState() == task.SUCCEEDED {
break
}
2022-06-23 13:09:54 +00:00
if tsk.GetState() == task.ERRORED {
t.Fatalf("failed to download: %+v", tsk)
}
time.Sleep(time.Second)
}
}