-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncmanager_test.go
More file actions
182 lines (136 loc) · 3.32 KB
/
syncmanager_test.go
File metadata and controls
182 lines (136 loc) · 3.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
182
package queue
import (
"testing"
"time"
)
func TestStartStopSyncManager(t *testing.T) {
// Simply test starting and stopping manager:
sm := NewSyncManager(drivers[0])
timeout := make(chan bool, 1)
success := make(chan bool)
go func() {
// Don't want to wait for SM to stop indefinitely
time.Sleep(2 * time.Second)
timeout <- true
}()
go func() {
sm.Run()
success <- true
}()
go func() {
sm.Stop()
}()
select {
case <-success:
return
case <-timeout:
t.Error("Timeout before sync manager stopped")
}
}
func TestRegisterAction(t *testing.T) {
taskName := "TestRegisterAction"
sm := NewSyncManager(drivers[0])
go func() {
sm.Run()
}()
defer sm.Stop()
ea := NewExampleTaskAction(nil)
err := sm.RegisterTaskHandler(&ea, taskName)
if err != nil {
t.Error(err)
return
}
storedAction := sm.getRegisteredAction(taskName)
if storedAction == nil {
t.Errorf("Register action returned no error, yet action was not registered")
}
}
func TestRunScheduledAction(t *testing.T) {
// Test that scheduled action runs
sm := NewSyncManager(drivers[0])
go func() {
sm.Run()
}()
sm.Stop()
results := make(chan bool, 10)
ea := NewExampleScheduledAction(results, 0)
sm.Schedule(&ea, time.Millisecond*250)
started := time.Now()
// Gather four results:
count := 0
for count < 4 {
<-results
count++
}
diff := time.Since(started)
if diff.Seconds() < 0.75 || diff.Seconds() > 1.25 {
t.Errorf("Scheduled time should take around 1 second but took %0.2f", diff.Seconds())
}
}
func TestRunScheduledPanicAction(t *testing.T) {
// Test that scheduled action runs
sm := NewSyncManager(drivers[0])
go func() {
sm.Run()
}()
sm.Stop()
results := make(chan bool, 10)
ea := NewExampleScheduledAction(results, 1)
sm.Schedule(&ea, time.Millisecond*250)
started := time.Now()
// Gather four results:
count := 0
for count < 4 {
<-results
count++
}
diff := time.Since(started)
if diff.Seconds() < 1.00 || diff.Seconds() > 1.50 {
t.Errorf("Scheduled time should take around 1.25 seconds but took %0.2f", diff.Seconds())
}
}
func TestRunTaskAction(t *testing.T) {
// Tests running a task on some queue items. We run this for each driver, since they may implement queue differently
for _, driver := range drivers {
taskName := "TestRunTaskAction"
sm := NewSyncManager(driver)
sm.driver.clear()
tm := NewTaskManager(driver)
go func() {
sm.Run()
}()
defer sm.Stop()
result := make(chan bool)
ea := NewExampleTaskAction(result)
err := sm.RegisterTaskHandler(&ea, taskName)
if err != nil {
t.Error(err)
return
}
// Create our sample task
taskKey := hashKey(taskName + "1")
data := map[string]interface{}{}
err = tm.AddTask(taskName, taskKey, time.Now(), "test_created_by", data)
if err != nil {
t.Error(err)
}
timeout := make(chan bool, 1)
go func() {
// Don't want to wait for SM to stop indefinitely
time.Sleep(4 * time.Second)
timeout <- true
}()
select {
case <-result:
break
case <-timeout:
t.Error("Timeout before task action was run")
}
// Now, if we run 'pop', there should be no waiting tasks, waiting a moment for the thread to write the state:
time.Sleep(time.Millisecond * 250)
task, err := sm.driver.pop()
if err != ErrNoTasks {
t.Errorf("Should have had ErrNoTasks, but had %+v: %+v", err, task)
}
}
}