@@ -3,6 +3,7 @@ package hooks
3
3
import (
4
4
"context"
5
5
"net/http"
6
+ "sort"
6
7
"testing"
7
8
"time"
8
9
@@ -88,6 +89,7 @@ func Test_dequeueTaskExecutions_ScheduledTask(t *testing.T) {
88
89
// Mock the sync of tasks
89
90
// It will remove all the tasks from the database
90
91
m .EXPECT ().WorkflowAllHooksList ().Return ([]sdk.NodeHook {}, nil )
92
+ m .EXPECT ().WorkflowAllHooksExecutions ().Return ([]string {}, nil )
91
93
m .EXPECT ().VCSConfiguration ().Return (nil , nil ).AnyTimes ()
92
94
require .NoError (t , s .synchronizeTasks (ctx ))
93
95
@@ -164,6 +166,7 @@ func Test_dequeueTaskExecutions_ScheduledTask(t *testing.T) {
164
166
// Now we will triggered another hooks sync
165
167
// The mock must return one hook
166
168
m .EXPECT ().WorkflowAllHooksList ().Return ([]sdk.NodeHook {* h }, nil )
169
+ m .EXPECT ().WorkflowAllHooksExecutions ().Return ([]string {}, nil )
167
170
require .NoError (t , s .synchronizeTasks (context .Background ()))
168
171
169
172
// We must be able to find the task
@@ -177,3 +180,55 @@ func Test_dequeueTaskExecutions_ScheduledTask(t *testing.T) {
177
180
assert .Equal (t , "DONE" , execs [0 ].Status )
178
181
assert .Equal (t , "SCHEDULED" , execs [1 ].Status )
179
182
}
183
+
184
+ func Test_synchronizeTasks (t * testing.T ) {
185
+ log .Factory = log .NewTestingWrapper (t )
186
+ s , cancel := setupTestHookService (t )
187
+ defer cancel ()
188
+
189
+ ctx , cancel := context .WithTimeout (context .TODO (), 5 * time .Second )
190
+ defer cancel ()
191
+
192
+ // Get the mock
193
+ m := s .Client .(* mock_cdsclient.MockInterface )
194
+
195
+ m .EXPECT ().VCSConfiguration ().Return (nil , nil ).AnyTimes ()
196
+
197
+ m .EXPECT ().WorkflowAllHooksList ().Return ([]sdk.NodeHook {}, nil )
198
+ m .EXPECT ().WorkflowAllHooksExecutions ().Return ([]string {}, nil )
199
+ require .NoError (t , s .synchronizeTasks (ctx ))
200
+
201
+ tasks , err := s .Dao .FindAllTasks (ctx )
202
+ require .NoError (t , err )
203
+ require .Len (t , tasks , 0 )
204
+
205
+ require .NoError (t , s .Dao .SaveTask (& sdk.Task {
206
+ UUID : "1" ,
207
+ Type : TypeScheduler ,
208
+ }))
209
+ require .NoError (t , s .Dao .SaveTask (& sdk.Task {
210
+ UUID : sdk .UUID (),
211
+ Type : TypeScheduler ,
212
+ }))
213
+ require .NoError (t , s .Dao .SaveTask (& sdk.Task {
214
+ UUID : "2" ,
215
+ Type : TypeOutgoingWorkflow ,
216
+ }))
217
+ require .NoError (t , s .Dao .SaveTask (& sdk.Task {
218
+ UUID : sdk .UUID (),
219
+ Type : TypeOutgoingWorkflow ,
220
+ }))
221
+
222
+ m .EXPECT ().WorkflowAllHooksList ().Return ([]sdk.NodeHook {{UUID : "1" }}, nil )
223
+ m .EXPECT ().WorkflowAllHooksExecutions ().Return ([]string {"2" }, nil )
224
+ require .NoError (t , s .synchronizeTasks (ctx ))
225
+
226
+ tasks , err = s .Dao .FindAllTasks (ctx )
227
+ require .NoError (t , err )
228
+ require .Len (t , tasks , 2 )
229
+ sort .Slice (tasks , func (i , j int ) bool { return tasks [i ].UUID < tasks [j ].UUID })
230
+ require .Equal (t , "1" , tasks [0 ].UUID )
231
+ require .Equal (t , TypeScheduler , tasks [0 ].Type )
232
+ require .Equal (t , "2" , tasks [1 ].UUID )
233
+ require .Equal (t , TypeOutgoingWorkflow , tasks [1 ].Type )
234
+ }
0 commit comments