File size: 1,688 Bytes
7107f0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tache_test

import (
	"log/slog"
	"os"
	"sync/atomic"
	"testing"
	"time"

	"github.com/alist-org/alist/v3/pkg/tache"
)

type TestTask struct {
	tache.Base
	Data string
	do   func(*TestTask) error
}

func (t *TestTask) Run() error {
	return t.do(t)
}

func TestManager_Add(t *testing.T) {
	tm := tache.NewManager[*TestTask]()
	task := &TestTask{}
	tm.Add(task)
	t.Logf("%+v", task)
}

func TestWithRetry(t *testing.T) {
	tm := tache.NewManager[*TestTask](tache.WithMaxRetry(3), tache.WithWorks(1))
	var num atomic.Int64
	for i := int64(0); i < 10; i++ {
		task := &TestTask{
			do: func(task *TestTask) error {
				num.Add(1)
				if num.Load() < i*3 {
					return tache.NewErr("test")
				}
				return nil
			},
		}
		tm.Add(task)
	}
	tm.Wait()
	tasks := tm.GetAll()
	for _, task := range tasks {
		t.Logf("%+v", task)
	}
}

func TestWithPersistPath(t *testing.T) {
	tm := tache.NewManager[*TestTask](tache.WithPersistPath("./test.json"))
	task := &TestTask{
		do: func(task *TestTask) error {
			return nil
		},
		Data: "haha",
	}
	tm.Add(task)
	tm.Wait()
	t.Logf("%+v", task)
	time.Sleep(4 * time.Second)
}

func TestMultiTasks(t *testing.T) {
	tm := tache.NewManager[*TestTask](tache.WithWorks(3), tache.WithLogger(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
		AddSource:   true,
		Level:       slog.LevelDebug,
		ReplaceAttr: nil,
	}))))
	var num atomic.Int64
	for i := 0; i < 100; i++ {
		tm.Add(&TestTask{
			do: func(task *TestTask) error {
				num.Add(1)
				return nil
			},
		})
	}
	tm.Wait()
	//time.Sleep(3 * time.Second)
	if num.Load() != 100 {
		t.Errorf("num error, num: %d", num.Load())
	} else {
		t.Logf("num success, num: %d", num.Load())
	}
}