forked from xzc-coder/executor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
66 lines (61 loc) · 1.8 KB
/
examples_test.go
File metadata and controls
66 lines (61 loc) · 1.8 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
package executor
import (
"fmt"
"github.com/stretchr/testify/require"
"testing"
"time"
)
// TestPool 测试协程池案例
func TestPool(t *testing.T) {
//创建
pool := NewExecutor(WithCorePoolSize(1))
//添加任务,无返回值
pool.Execute(func() {
//模拟任务运行
time.Sleep(100 * time.Millisecond)
})
//添加任务,有返回值,获取future
f, err := pool.Submit(func() any {
//模拟任务运行
time.Sleep(100 * time.Millisecond)
return "test"
})
if err == nil {
//阻塞至完成并获取返回值
result := f.Get()
require.Equal(t, "test", result)
}
//结束任务,渐进式关闭
pool.Shutdown()
}
// TestScheduledPool 测试延迟协程池案例
func TestScheduledPool(t *testing.T) {
//创建
schedule := NewSchedule(WithCorePoolSize(1))
//添加延迟任务,无返回值,100毫秒后运行一次
schedule.Schedule(func() {
time.Sleep(100 * time.Millisecond)
}, 100*time.Millisecond)
//添加任务,有返回值,100毫秒后运行一次,获取future
f, err := schedule.Schedules(func() any {
//模拟任务运行
time.Sleep(100 * time.Millisecond)
return "test"
}, 100*time.Millisecond)
if err == nil {
//阻塞至完成并获取返回值
result := f.Get()
require.Equal(t, "test", result)
}
//添加固定速率任务,延迟100毫秒执行,然后每隔100毫秒执行一次(不管上次任务是否完成)
schedule.ScheduleAtFixedRate(func() {
fmt.Println(time.Now())
}, 100*time.Millisecond, 100*time.Millisecond)
//添加固定速率任务,延迟100毫秒执行,等任务执行完后,每隔100毫秒执行一次
schedule.ScheduleWithFixedDelay(func() {
fmt.Println(time.Now())
}, 100*time.Millisecond, 100*time.Millisecond)
time.Sleep(1000 * time.Millisecond)
//结束任务,渐进式关闭
schedule.Shutdown()
}