forked from rakanalh/scheduler
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstore_test.go
More file actions
181 lines (156 loc) · 4.32 KB
/
store_test.go
File metadata and controls
181 lines (156 loc) · 4.32 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package scheduler
import (
"testing"
"github.com/sdvallejo/scheduler/storage"
"github.com/sdvallejo/scheduler/task"
)
func TestStore(t *testing.T) {
mock := task.CallbackMock{}
funcRegistry := task.NewFuncRegistry()
store := getStoreBridge(funcRegistry, nil)
task := newTask(funcRegistry, mock.CallNoArgs)
task.IsRecurring = true
err := store.Add(task)
if err != nil {
t.Error("Failed to store task")
}
}
func TestStoreTaskWithMultipleParams(t *testing.T) {
mock := task.CallbackMock{}
funcRegistry := task.NewFuncRegistry()
store := getStoreBridge(funcRegistry, nil)
task := newTask(funcRegistry, mock.CallWithArgs, "Hello", "World")
err := store.Add(task)
if err != nil {
t.Error("Failed to store task with multiple params")
}
}
func TestStoreThatFails(t *testing.T) {
mock := task.CallbackMock{}
funcRegistry := task.NewFuncRegistry()
store := getStoreBridge(funcRegistry, nil)
task := newTask(funcRegistry, mock.CallWithChan, make(chan bool))
err := store.Add(task)
if err == nil {
t.Error("Wrong storage of a task with a channel arg took place")
}
}
func TestRemoveTask(t *testing.T) {
mock := task.CallbackMock{}
funcRegistry := task.NewFuncRegistry()
store := getStoreBridge(funcRegistry, nil)
task := newTask(funcRegistry, mock.CallWithArgs, "Hello", "World")
_ = store.Add(task)
err := store.Remove(task)
if err != nil {
t.Error("Failed to remove task")
}
}
func TestRemoveThatFails(t *testing.T) {
mock := task.CallbackMock{}
funcRegistry := task.NewFuncRegistry()
store := getStoreBridge(funcRegistry, nil)
task := newTask(funcRegistry, mock.CallWithChan, make(chan bool))
err := store.Remove(task)
if err == nil {
t.Error("Wrong call to remove a task with a channel arg took place")
}
}
func TestFetch(t *testing.T) {
mock := task.CallbackMock{}
funcRegistry := task.NewFuncRegistry()
store := getStoreBridge(funcRegistry, nil)
task := newTask(funcRegistry, mock.CallNoArgs)
err := store.Add(task)
if err != nil {
t.Error("Failed to store task")
}
tasks, err := store.Fetch()
if err != nil {
t.Error("Could not read tasks from store")
}
if len(tasks) != 1 {
t.Error("Found wrong task count")
}
}
func TestFetchWithParams(t *testing.T) {
mock := task.CallbackMock{}
funcRegistry := task.NewFuncRegistry()
store := getStoreBridge(funcRegistry, nil)
task := newTask(funcRegistry, mock.CallWithArgs, "Test", true)
err := store.Add(task)
if err != nil {
t.Error("Failed to store task")
}
tasks, err := store.Fetch()
if err != nil {
t.Error("Could not read tasks from store")
}
if len(tasks) != 1 {
t.Error("Found wrong task count")
}
}
func TestFetchWrongRunTimes(t *testing.T) {
funcRegistry := task.NewFuncRegistry()
storeMock := newStoreMockWithMode(fail)
store := getStoreBridge(funcRegistry, storeMock)
_, err := store.Fetch()
if err == nil {
t.Error("Should fail when fetching")
}
storeMock.Mode = failOnLastRun
_, err = store.Fetch()
if err == nil {
t.Error("Should fail when parsing lastRun")
}
storeMock.Mode = failOnNextRun
_, err = store.Fetch()
if err == nil {
t.Error("Should fail when parsing nextRun")
}
storeMock.Mode = failOnDuration
_, err = store.Fetch()
if err == nil {
t.Error("Should fail when parsing duration")
}
storeMock.Mode = failOnIsRecurring
_, err = store.Fetch()
if err == nil {
t.Error("Should fail when parsing isRecurring")
}
funcRegistry.Add(mockFunction)
storeMock.Mode = failOnFuncMeta
_, err = store.Fetch()
if err == nil {
t.Error("Should fail when trying to find function")
}
storeMock.Mode = failOnEmptyParams
params, err := store.Fetch()
if err != nil && len(params) != 0 {
t.Error("Should fail when trying to parse empty string params")
}
storeMock.Mode = failOnEmptyListParams
_, err = store.Fetch()
if err == nil {
t.Error("Should fail when trying to parse empty string params")
}
}
func newTask(funcRegistry *task.FuncRegistry, function task.Function, params ...task.Param) *task.Task {
funcMeta, err := funcRegistry.Add(function)
if err != nil {
return nil
}
task := task.New(funcMeta, params)
_, _ = funcRegistry.Add(task.Func)
return task
}
func getStoreBridge(funcRegistry *task.FuncRegistry, store storage.TaskStore) storeBridge {
if store == nil {
store = storage.NewMemoryStorage()
}
storeBridge := storeBridge{
store: store,
funcRegistry: funcRegistry,
}
return storeBridge
}