forked from ajvb/kala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.go
202 lines (170 loc) · 3.94 KB
/
test_utils.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package job
import (
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/primedata-ai/kala/utils/iso8601"
)
type MockDBGetAll struct {
MockDB
response []*Job
}
func (d *MockDBGetAll) GetAll() ([]*Job, error) {
return d.response, nil
}
type MockDB struct{}
func (m *MockDB) GetAll() ([]*Job, error) {
return nil, nil
}
func (m *MockDB) Get(id string) (*Job, error) {
return nil, nil
}
func (m *MockDB) Delete(id string) error {
return nil
}
func (m *MockDB) Save(job *Job) error {
return nil
}
func (m *MockDB) Close() error {
return nil
}
func NewMockCache() *LockFreeJobCache {
return NewLockFreeJobCache(&MockDB{})
}
func GetMockJob() *Job {
return &Job{
Name: "mock_job",
Command: "bash -c 'date'",
Owner: "example@example.com",
Retries: 2,
}
}
func GetMockFailingJob() *Job {
return &Job{
Name: "mock_failing_job",
Command: "asdf",
Owner: "example@example.com",
Retries: 2,
}
}
func GetMockRemoteJob(props RemoteProperties) *Job {
return &Job{
Name: "mock_remote_job",
Command: "",
JobType: RemoteJob,
RemoteProperties: props,
}
}
func GetMockJobWithSchedule(repeat int, scheduleTime time.Time, delay string) *Job {
genericMockJob := GetMockJob()
parsedTime := scheduleTime.Format(time.RFC3339)
scheduleStr := fmt.Sprintf("R%d/%s/%s", repeat, parsedTime, delay)
genericMockJob.Schedule = scheduleStr
return genericMockJob
}
func GetMockRecurringJobWithSchedule(scheduleTime time.Time, delay string) *Job {
genericMockJob := GetMockJob()
parsedTime := scheduleTime.Format(time.RFC3339)
scheduleStr := fmt.Sprintf("R/%s/%s", parsedTime, delay)
genericMockJob.Schedule = scheduleStr
genericMockJob.timesToRepeat = -1
parsedDuration, _ := iso8601.FromString(delay)
genericMockJob.delayDuration = parsedDuration
return genericMockJob
}
func GetMockJobStats(oldDate time.Time, count int) []*JobStat {
stats := make([]*JobStat, 0)
for i := 1; i <= count; i++ {
el := &JobStat{
JobId: "stats-id-" + string(i),
NumberOfRetries: 0,
ExecutionDuration: 10000,
Success: true,
RanAt: oldDate,
}
stats = append(stats, el)
}
return stats
}
func GetMockJobWithGenericSchedule(now time.Time) *Job {
fiveMinutesFromNow := now.Add(time.Minute * 5)
return GetMockJobWithSchedule(2, fiveMinutesFromNow, "P1DT10M10S")
}
func parseTime(t *testing.T, value string) time.Time {
now, err := time.Parse("2006-Jan-02 15:04", value)
if err != nil {
t.Fatal(err)
}
return now
}
func parseTimeInLocation(t *testing.T, value string, location string) time.Time {
loc, err := time.LoadLocation(location)
if err != nil {
t.Fatal(err)
}
now, err := time.ParseInLocation("2006-Jan-02 15:04", value, loc)
if err != nil {
t.Fatal(err)
}
return now
}
// Used to hand off execution briefly so that jobs can run and so on.
func briefPause() {
time.Sleep(time.Millisecond * 100)
}
func awaitJobRan(t *testing.T, j *Job, timeout time.Duration) {
t.Helper()
select {
case <-j.ranChan:
case <-time.After(timeout):
t.Fatal("Job failed to run")
}
}
var _ JobDB = (*MemoryDB)(nil)
type MemoryDB struct {
m map[string]*Job
lock sync.RWMutex
}
func NewMemoryDB() *MemoryDB {
return &MemoryDB{
m: map[string]*Job{},
}
}
func (m *MemoryDB) GetAll() (ret []*Job, _ error) {
m.lock.RLock()
defer m.lock.RUnlock()
for _, v := range m.m {
ret = append(ret, v)
}
return
}
func (m *MemoryDB) Get(id string) (*Job, error) {
m.lock.RLock()
defer m.lock.RUnlock()
j, exist := m.m[id]
if !exist {
return nil, ErrJobNotFound(id)
}
return j, nil
}
func (m *MemoryDB) Delete(id string) error {
m.lock.Lock()
defer m.lock.Unlock()
if _, exists := m.m[id]; !exists {
return errors.New("Doesn't exist") // Used for testing
}
delete(m.m, id)
// log.Printf("After delete: %+v", m)
return nil
}
func (m *MemoryDB) Save(j *Job) error {
m.lock.Lock()
defer m.lock.Unlock()
m.m[j.Id] = j
return nil
}
func (m *MemoryDB) Close() error {
return nil
}