-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathfunction_job_test.go
101 lines (81 loc) · 2.35 KB
/
function_job_test.go
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
97
98
99
100
101
package job_test
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/reugn/go-quartz/internal/assert"
"github.com/reugn/go-quartz/job"
"github.com/reugn/go-quartz/quartz"
)
func TestFunctionJob(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var n atomic.Int32
funcJob1 := job.NewFunctionJob(func(_ context.Context) (string, error) {
n.Add(2)
return "fired1", nil
})
funcJob2 := job.NewFunctionJob(func(_ context.Context) (*int, error) {
n.Add(2)
result := 42
return &result, nil
})
sched := quartz.NewStdScheduler()
sched.Start(ctx)
assert.IsNil(t, sched.ScheduleJob(quartz.NewJobDetail(funcJob1,
quartz.NewJobKey("funcJob1")),
quartz.NewRunOnceTrigger(time.Millisecond*300)))
assert.IsNil(t, sched.ScheduleJob(quartz.NewJobDetail(funcJob2,
quartz.NewJobKey("funcJob2")),
quartz.NewRunOnceTrigger(time.Millisecond*800)))
time.Sleep(time.Second)
assert.IsNil(t, sched.Clear())
sched.Stop()
assert.Equal(t, funcJob1.JobStatus(), job.StatusOK)
assert.Equal(t, funcJob1.Result(), "fired1")
assert.Equal(t, funcJob2.JobStatus(), job.StatusOK)
assert.NotEqual(t, funcJob2.Result(), nil)
assert.Equal(t, *funcJob2.Result(), 42)
assert.Equal(t, n.Load(), 4)
}
func TestNewFunctionJob_WithDesc(t *testing.T) {
jobDesc := "test job"
funcJob1 := job.NewFunctionJobWithDesc(func(_ context.Context) (string, error) {
return "fired1", nil
}, jobDesc)
funcJob2 := job.NewFunctionJobWithDesc(func(_ context.Context) (string, error) {
return "fired2", nil
}, jobDesc)
assert.Equal(t, funcJob1.Description(), jobDesc)
assert.Equal(t, funcJob2.Description(), jobDesc)
}
func TestFunctionJob_RespectsContext(t *testing.T) {
var n int
funcJob2 := job.NewFunctionJob(func(ctx context.Context) (bool, error) {
timer := time.NewTimer(time.Hour)
defer timer.Stop()
select {
case <-ctx.Done():
n--
return false, ctx.Err()
case <-timer.C:
n++
return true, nil
}
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sig := make(chan struct{})
go func() { defer close(sig); _ = funcJob2.Execute(ctx) }()
if n != 0 {
t.Fatal("job should not have run yet")
}
cancel()
<-sig
if n != -1 {
t.Fatal("job side effect should have reflected cancelation:", n)
}
assert.ErrorIs(t, funcJob2.Error(), context.Canceled)
assert.Equal(t, funcJob2.Result(), false)
}