@@ -3,6 +3,7 @@ package hooks
33import (
44 "context"
55 "net/http"
6+ "sort"
67 "testing"
78 "time"
89
@@ -88,6 +89,7 @@ func Test_dequeueTaskExecutions_ScheduledTask(t *testing.T) {
8889 // Mock the sync of tasks
8990 // It will remove all the tasks from the database
9091 m .EXPECT ().WorkflowAllHooksList ().Return ([]sdk.NodeHook {}, nil )
92+ m .EXPECT ().WorkflowAllHooksExecutions ().Return ([]string {}, nil )
9193 m .EXPECT ().VCSConfiguration ().Return (nil , nil ).AnyTimes ()
9294 require .NoError (t , s .synchronizeTasks (ctx ))
9395
@@ -164,6 +166,7 @@ func Test_dequeueTaskExecutions_ScheduledTask(t *testing.T) {
164166 // Now we will triggered another hooks sync
165167 // The mock must return one hook
166168 m .EXPECT ().WorkflowAllHooksList ().Return ([]sdk.NodeHook {* h }, nil )
169+ m .EXPECT ().WorkflowAllHooksExecutions ().Return ([]string {}, nil )
167170 require .NoError (t , s .synchronizeTasks (context .Background ()))
168171
169172 // We must be able to find the task
@@ -177,3 +180,55 @@ func Test_dequeueTaskExecutions_ScheduledTask(t *testing.T) {
177180 assert .Equal (t , "DONE" , execs [0 ].Status )
178181 assert .Equal (t , "SCHEDULED" , execs [1 ].Status )
179182}
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