-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexport_test.go
More file actions
96 lines (78 loc) · 1.92 KB
/
Copy pathexport_test.go
File metadata and controls
96 lines (78 loc) · 1.92 KB
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
90
91
92
93
94
95
96
package task
import (
"context"
"fmt"
"log"
"strconv"
"testing"
"time"
"github.com/assembly-hub/basics/redis"
)
func hello1(i ...interface{}) (interface{}, error) {
fmt.Printf("hello,欢迎来到编程狮%v\n", i)
// time.Sleep(time.Second * 1)
// panic(123)
return i[0], nil
}
func hello2(i ...interface{}) (interface{}, error) {
fmt.Printf("hello,欢迎来到编程狮%v\n", i)
// time.Sleep(time.Second * 1)
// r := i[0]
// panic(111)
return i[0], nil
}
func TestExecute(t *testing.T) {
taskObj := NewTaskExecutor("test111")
taskObj.AddFixed(hello1, 0)
taskObj.AddFlexible(func(i int) (interface{}, error) {
fmt.Println("test: ", i)
return nil, nil
}, 1)
r, taskErr, err := taskObj.ExecuteWithErr(context.Background())
if err != nil {
panic(err)
}
fmt.Println(taskErr)
for i, v := range r {
if v == nil {
fmt.Println(strconv.Itoa(i) + ": nil")
} else {
fmt.Println(strconv.Itoa(i) + ": " + strconv.Itoa(v.(int)))
}
}
}
func TestManege(t *testing.T) {
defer func() {
if p := recover(); p != nil {
log.Println(p)
}
}()
opts := redis.DefaultOptions()
opts.Addr = "127.0.0.1:6379"
opts.DB = 0
r := redis.NewRedis(&opts)
// step 1, init task manager
task := SingleTask(100, "test", 1500, r)
// step 1.1 非必须,默认时间为:60秒
task.SetTaskEffectiveTime(10)
// step 2, register task
task.RegisterFlexible("test_111", func(i int, s string, arr []int, mp map[string]interface{}) {
fmt.Println("-------task_111 params: ", i, s, arr, mp)
})
task.RegisterFlexible("test_222", func() {
fmt.Println("-------task_222 ")
})
// step 3, run task manager
task.Run()
// step 4 use task
// add Interval Task
task.AddInterval("test_111", []interface{}{1, "2", []int{1, 2, 3}, map[string]interface{}{
"test": "test", "key": []string{"test"},
}}, 5)
task.AddInterval("test_222", nil, 5)
task.AddDelay("test_222", nil, 2)
for {
time.Sleep(5 * time.Second)
break
}
}