-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron_test.go
More file actions
62 lines (49 loc) · 1.4 KB
/
cron_test.go
File metadata and controls
62 lines (49 loc) · 1.4 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
package dgcron
import (
"fmt"
"testing"
"time"
dgctx "github.com/darwinOrg/go-common/context"
dglock "github.com/darwinOrg/go-dlock"
redisdk "github.com/darwinOrg/go-redis"
"github.com/rolandhe/saber/gocc"
)
func TestAddJob(t *testing.T) {
cron := NewAndStart(nil)
defer cron.Stop()
cron.AddJob("TestJob", "*/1 * * * * ?", func(ctx *dgctx.DgContext) {
fmt.Println(time.Now().UnixMilli())
})
time.Sleep(5 * time.Second)
}
func TestAddJobWithLock(t *testing.T) {
redisdk.InitClient("localhost:6379")
cron := NewAndStart(dglock.NewRedisLocker(true))
defer cron.Stop()
cron.AddJobWithLock("TestJob", "*/1 * * * * ?", 5000, func(ctx *dgctx.DgContext) {
fmt.Println(time.Now().UnixMilli())
})
time.Sleep(10 * time.Second)
}
func TestAddFixDurationJob(t *testing.T) {
AddFixDurationJob("TestJob", time.Second, func(ctx *dgctx.DgContext) {
fmt.Println(time.Now().UnixMilli())
})
time.Sleep(5 * time.Second)
}
func TestAddFixDelayJob(t *testing.T) {
AddFixDelayJob("TestJob", time.Second, func(ctx *dgctx.DgContext) {
fmt.Println(time.Now().UnixMilli())
})
time.Sleep(5 * time.Second)
}
func TestRunSemaphoreJob(t *testing.T) {
semaphore := gocc.NewDefaultSemaphore(3)
for i := 0; i < 10; i++ {
ctx := dgctx.SimpleDgContext()
_ = RunSemaphoreJob(ctx, "TestJob", semaphore, time.Second, func(ctx *dgctx.DgContext) {
fmt.Println(time.Now().UnixMilli())
})
}
time.Sleep(10 * time.Second)
}