alist/pkg/task/task_test.go

97 lines
1.9 KiB
Go
Raw Normal View History

2022-06-17 14:09:20 +00:00
package task
import (
2022-06-21 08:14:37 +00:00
"sync/atomic"
2022-06-17 14:09:20 +00:00
"testing"
"time"
2022-07-30 14:04:21 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
2022-06-17 14:09:20 +00:00
)
func TestTask_Manager(t *testing.T) {
2022-07-30 14:04:21 +00:00
tm := NewTaskManager(3, func(id *uint64) {
2022-06-21 08:14:37 +00:00
atomic.AddUint64(id, 1)
2022-06-17 14:09:20 +00:00
})
2022-06-22 11:28:41 +00:00
id := tm.Submit(WithCancelCtx(&Task[uint64]{
2022-06-21 08:14:37 +00:00
Name: "test",
2022-06-22 11:28:41 +00:00
Func: func(task *Task[uint64]) error {
2022-06-21 08:14:37 +00:00
time.Sleep(time.Millisecond * 500)
return nil
},
}))
2022-06-17 14:09:20 +00:00
task, ok := tm.Get(id)
if !ok {
t.Fatal("task not found")
}
time.Sleep(time.Millisecond * 100)
2022-06-23 13:09:54 +00:00
if task.state != RUNNING {
t.Errorf("task status not running: %s", task.state)
2022-06-17 14:09:20 +00:00
}
time.Sleep(time.Second)
2022-06-29 10:36:14 +00:00
if task.state != SUCCEEDED {
2022-06-23 13:09:54 +00:00
t.Errorf("task status not finished: %s", task.state)
2022-06-17 14:09:20 +00:00
}
}
func TestTask_Cancel(t *testing.T) {
2022-07-30 14:04:21 +00:00
tm := NewTaskManager(3, func(id *uint64) {
2022-06-21 08:14:37 +00:00
atomic.AddUint64(id, 1)
2022-06-17 14:09:20 +00:00
})
2022-06-22 11:28:41 +00:00
id := tm.Submit(WithCancelCtx(&Task[uint64]{
2022-06-21 08:14:37 +00:00
Name: "test",
2022-06-22 11:28:41 +00:00
Func: func(task *Task[uint64]) error {
2022-06-21 08:14:37 +00:00
for {
if utils.IsCanceled(task.Ctx) {
return nil
} else {
t.Logf("task is running")
}
}
},
}))
2022-06-17 14:09:20 +00:00
task, ok := tm.Get(id)
if !ok {
t.Fatal("task not found")
}
time.Sleep(time.Microsecond * 50)
task.Cancel()
time.Sleep(time.Millisecond)
2022-06-23 13:09:54 +00:00
if task.state != CANCELED {
t.Errorf("task status not canceled: %s", task.state)
2022-06-17 14:09:20 +00:00
}
}
func TestTask_Retry(t *testing.T) {
2022-07-30 14:04:21 +00:00
tm := NewTaskManager(3, func(id *uint64) {
2022-06-21 08:14:37 +00:00
atomic.AddUint64(id, 1)
2022-06-17 14:09:20 +00:00
})
2022-06-21 08:14:37 +00:00
num := 0
2022-06-22 11:28:41 +00:00
id := tm.Submit(WithCancelCtx(&Task[uint64]{
2022-06-21 08:14:37 +00:00
Name: "test",
2022-06-22 11:28:41 +00:00
Func: func(task *Task[uint64]) error {
2022-06-21 08:14:37 +00:00
num++
if num&1 == 1 {
return errors.New("test error")
}
return nil
},
}))
2022-06-17 14:09:20 +00:00
task, ok := tm.Get(id)
if !ok {
t.Fatal("task not found")
}
time.Sleep(time.Millisecond)
if task.Error == nil {
2022-06-23 13:09:54 +00:00
t.Error(task.state)
2022-06-17 14:09:20 +00:00
t.Fatal("task error is nil, but expected error")
} else {
t.Logf("task error: %s", task.Error)
}
2022-06-18 12:38:14 +00:00
task.retry()
2022-06-17 14:09:20 +00:00
time.Sleep(time.Millisecond)
if task.Error != nil {
t.Errorf("task error: %+v, but expected nil", task.Error)
}
}