forked from codnect/chrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_test.go
114 lines (84 loc) · 3.06 KB
/
task_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
102
103
104
105
106
107
108
109
110
111
112
113
114
package chrono
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)
func TestNewSchedulerTask(t *testing.T) {
_, err := CreateSchedulerTask(nil)
assert.Error(t, err)
}
func TestNewSchedulerTask_WithLocation(t *testing.T) {
_, err := CreateSchedulerTask(func(ctx context.Context) {
}, WithLocation("Europe/Istanbul"))
assert.Nil(t, err)
}
func TestNewSchedulerTask_WithInvalidLocation(t *testing.T) {
_, err := CreateSchedulerTask(func(ctx context.Context) {
}, WithLocation("Europe"))
assert.Error(t, err)
}
func TestNewScheduledRunnableTask(t *testing.T) {
task, _ := CreateScheduledRunnableTask(0, func(ctx context.Context) {
}, time.Now(), -1, true)
assert.Equal(t, task.period, 0*time.Second)
_, err := CreateScheduledRunnableTask(0, nil, time.Now(), -1, true)
assert.Error(t, err)
}
func TestNewTriggerTask(t *testing.T) {
trigger, err := CreateCronTrigger("* * * * * *", time.Local)
assert.Nil(t, err)
_, err = CreateTriggerTask(nil, NewDefaultTaskExecutor(), trigger)
assert.Error(t, err)
_, err = CreateTriggerTask(func(ctx context.Context) {
}, nil, trigger)
assert.Error(t, err)
_, err = CreateTriggerTask(func(ctx context.Context) {
}, NewDefaultTaskExecutor(), nil)
}
type zeroTrigger struct {
}
func (trigger *zeroTrigger) NextExecutionTime(ctx TriggerContext) time.Time {
return time.Time{}
}
func TestTriggerTask_Schedule(t *testing.T) {
task, _ := CreateTriggerTask(func(ctx context.Context) {}, NewDefaultTaskExecutor(), &zeroTrigger{})
_, err := task.Schedule()
assert.NotNil(t, err)
}
type scheduledExecutorMock struct {
mock.Mock
}
func (executor scheduledExecutorMock) Schedule(task Task, delay time.Duration) (ScheduledTask, error) {
result := executor.Called(task, delay)
return result.Get(0).(ScheduledTask), result.Error(1)
}
func (executor scheduledExecutorMock) ScheduleWithFixedDelay(task Task, initialDelay time.Duration, delay time.Duration) (ScheduledTask, error) {
result := executor.Called(task, initialDelay, delay)
return result.Get(0).(ScheduledTask), result.Error(1)
}
func (executor scheduledExecutorMock) ScheduleAtFixedRate(task Task, initialDelay time.Duration, period time.Duration) (ScheduledTask, error) {
result := executor.Called(task, initialDelay, period)
return result.Get(0).(ScheduledTask), result.Error(1)
}
func (executor scheduledExecutorMock) IsShutdown() bool {
result := executor.Called()
return result.Bool(0)
}
func (executor scheduledExecutorMock) Shutdown() chan bool {
result := executor.Called()
return result.Get(0).(chan bool)
}
func TestTriggerTask_ScheduleWithError(t *testing.T) {
scheduledExecutorMock := &scheduledExecutorMock{}
scheduledExecutorMock.On("Schedule", mock.AnythingOfType("Task"), mock.AnythingOfType("time.Duration")).
Return((*ScheduledRunnableTask)(nil), errors.New("test error"))
trigger, err := CreateCronTrigger("* * * * * *", time.Local)
assert.Nil(t, err)
task, _ := CreateTriggerTask(func(ctx context.Context) {}, scheduledExecutorMock, trigger)
_, err = task.Schedule()
assert.NotNil(t, err)
}