@@ -65,6 +65,10 @@ func TestAddJob(t *testing.T) {
6565
6666func TestAddJobRunning (t * testing.T ) {
6767 testQueuer := newQueuerMock ("TestQueuer" , 100 )
68+ testQueuer .AddTask (TaskMock )
69+ ctx , cancel := context .WithCancel (context .Background ())
70+ defer cancel ()
71+ testQueuer .Start (ctx , cancel )
6872
6973 t .Run ("Successfully runs a job without options" , func (t * testing.T ) {
7074 job , err := testQueuer .AddJob (TaskMock , 1 , "2" )
@@ -74,17 +78,11 @@ func TestAddJobRunning(t *testing.T) {
7478 assert .NoError (t , err , "GetJob should not return an error" )
7579 assert .NotNil (t , queuedJob , "GetJob should return the job that is currently running" )
7680
77- // Initialize the queuer and start processing jobs
78- testQueuer .AddTask (TaskMock )
79- ctx , cancel := context .WithCancel (context .Background ())
80- defer cancel ()
81- testQueuer .Start (ctx , cancel )
82-
8381 time .Sleep (2 * time .Second )
8482
8583 jobNotExisting , err := testQueuer .GetJob (job .RID )
86- assert .Error (t , err , "GetJob should return an error for cancelled job" )
87- assert .Nil (t , jobNotExisting , "GetJob should return nil for cancelled job" )
84+ assert .Error (t , err , "GetJob should return an error for ended job" )
85+ assert .Nil (t , jobNotExisting , "GetJob should return nil for ended job" )
8886
8987 jobArchived , err := testQueuer .dbJob .SelectJobFromArchive (job .RID )
9088 assert .NoError (t , err , "SelectJobFromArchive should not return an error for archived job" )
@@ -113,24 +111,19 @@ func TestAddJobRunning(t *testing.T) {
113111 require .NoError (t , err , "GetJob should not return an error" )
114112 require .NotNil (t , queuedJob , "GetJob should return the job that is currently running" )
115113
116- // Initialize the queuer and start processing jobs
117- testQueuer .AddTask (TaskMock )
118- ctx , cancel := context .WithCancel (context .Background ())
119- defer cancel ()
120- testQueuer .Start (ctx , cancel )
114+ time .Sleep (500 * time .Millisecond )
121115
122- // Wait for the job to be running (not scheduled because of less than 1 minute delay)
123- time .Sleep (1 * time .Second )
124116 jobScheduled , err := testQueuer .GetJob (job .RID )
125117 assert .NoError (t , err , "GetJob should not return an error for running job" )
126- assert .NotNil (t , jobScheduled , "GetJob should return the job that is currently running" )
118+ require .NotNil (t , jobScheduled , "GetJob should return the job that is currently running" )
127119 assert .Equal (t , model .JobStatusRunning , jobScheduled .Status , "Job should be in Scheduled status" )
128120
129121 // Wait for schedule time and job execution
130122 time .Sleep (15 * time .Second + maxDeviation )
123+
131124 jobNotExisting , err := testQueuer .GetJob (job .RID )
132- assert .Error (t , err , "GetJob should return an error for cancelled job" )
133- assert .Nil (t , jobNotExisting , "GetJob should return nil for cancelled job" )
125+ assert .Error (t , err , "GetJob should return an error for ended job" )
126+ assert .Nil (t , jobNotExisting , "GetJob should return nil for ended job" )
134127
135128 jobArchived , err := testQueuer .dbJob .SelectJobFromArchive (job .RID )
136129 assert .NoError (t , err , "SelectJobFromArchive should not return an error for archived job" )
@@ -274,7 +267,13 @@ func TestCancelJob(t *testing.T) {
274267}
275268
276269func TestCancelJobRunning (t * testing.T ) {
270+ // Only works with a running queuer because the worker needs to process jobs
271+ // to be able to cancel them.
277272 testQueuer := newQueuerMock ("TestQueuer" , 100 )
273+ testQueuer .AddTask (TaskMock )
274+ ctx , cancel := context .WithCancel (context .Background ())
275+ defer cancel ()
276+ testQueuer .Start (ctx , cancel )
278277
279278 t .Run ("Successfully cancels a running job" , func (t * testing.T ) {
280279 job , err := testQueuer .AddJob (TaskMock , 3 , "2" )
@@ -284,12 +283,6 @@ func TestCancelJobRunning(t *testing.T) {
284283 assert .NoError (t , err , "GetJob should not return an error" )
285284 assert .NotNil (t , queuedJob , "GetJob should return the job that is currently running" )
286285
287- // Initialize the queuer and start processing jobs
288- testQueuer .AddTask (TaskMock )
289- ctx , cancel := context .WithCancel (context .Background ())
290- defer cancel ()
291- testQueuer .Start (ctx , cancel )
292-
293286 time .Sleep (1 * time .Second )
294287
295288 cancelledJob , err := testQueuer .CancelJob (job .RID )
@@ -311,32 +304,27 @@ func TestCancelAllJobsByWorkerRunning(t *testing.T) {
311304 // Only works with a running queuer because the worker needs to process jobs
312305 // to be able to cancel them.
313306 testQueuer := newQueuerMock ("TestQueuer" , 100 )
307+ testQueuer .AddTask (TaskMock )
308+ ctx , cancel := context .WithCancel (context .Background ())
309+ defer cancel ()
310+ testQueuer .Start (ctx , cancel )
314311
315312 t .Run ("Successfully cancels all jobs by worker RID" , func (t * testing.T ) {
316- worker , err := testQueuer .GetWorker (testQueuer .worker .RID )
317- require .NoError (t , err , "GetWorker should not return an error on success" )
318-
319313 job1 , err := testQueuer .AddJob (TaskMock , 10 , "2" )
320314 require .NoError (t , err , "AddJob should not return an error on success" )
321- job1 .WorkerRID = worker .RID
322315
323316 job2 , err := testQueuer .AddJob (TaskMock , 10 , "4" )
324317 require .NoError (t , err , "AddJob should not return an error on success" )
325- job2 .WorkerRID = worker .RID
326-
327- // Initialize the queuer and start processing jobs
328- testQueuer .AddTask (TaskMock )
329- ctx , cancel := context .WithCancel (context .Background ())
330- defer cancel ()
331- testQueuer .Start (ctx , cancel )
332318
333319 time .Sleep (1 * time .Second )
334320
335- jobs , err := testQueuer .dbJob .SelectAllJobsByWorkerRID (worker .RID , 0 , 10 )
321+ jobs , err := testQueuer .dbJob .SelectAllJobsByWorkerRID (testQueuer . worker .RID , 0 , 10 )
336322 assert .NoError (t , err , "SelectAllJobsByWorkerRID should not return an error" )
337- assert .Len (t , jobs , 2 , "There should be two jobs for the worker" )
323+ require .Len (t , jobs , 2 , "There should be two jobs for the worker" )
324+ assert .Equal (t , model .JobStatusRunning , jobs [0 ].Status , "Job1 should be in Running status" )
325+ assert .Equal (t , model .JobStatusRunning , jobs [1 ].Status , "Job2 should be in Running status" )
338326
339- err = testQueuer .CancelAllJobsByWorker (worker .RID , 10 )
327+ err = testQueuer .CancelAllJobsByWorker (testQueuer . worker .RID , 10 )
340328 assert .NoError (t , err , "CancelAllJobsByWorker should not return an error on success" )
341329
342330 jobs , err = testQueuer .GetJobs (0 , 10 )
0 commit comments