11package queuer
22
33import (
4+ "context"
45 "queuer/model"
56 "strconv"
67 "testing"
@@ -11,28 +12,26 @@ import (
1112)
1213
1314// Short running example task function
14- func TaskMock (param1 int , param2 string ) (int , error ) {
15+ func TaskMock (duration int , param2 string ) (int , error ) {
1516 // Simulate some work
16- time .Sleep (1 * time .Second )
17+ time .Sleep (time . Duration ( duration ) * time .Second )
1718
1819 // Example for some error handling
1920 param2Int , err := strconv .Atoi (param2 )
2021 if err != nil {
2122 return 0 , err
2223 }
2324
24- return param1 + param2Int , nil
25+ return duration + param2Int , nil
2526}
2627
27- // TestAddJob is a comprehensive test function for the AddJob method.
2828func TestAddJob (t * testing.T ) {
29- testQueuer := newQueuerMock ("TestQueuer" , 1 )
29+ testQueuer := newQueuerMock ("TestQueuer" , 100 )
3030
3131 t .Run ("Successfully adds a job with nil options" , func (t * testing.T ) {
3232 expectedJob := & model.Job {
3333 TaskName : "queuer.TaskMock" ,
3434 Parameters : model.Parameters {1.0 , "2" },
35- Options : nil ,
3635 }
3736
3837 params := []interface {}{1 , "2" }
@@ -44,8 +43,35 @@ func TestAddJob(t *testing.T) {
4443 assert .Equal (t , expectedJob .Options , job .Options , "AddJob should return the correct options" )
4544 })
4645
46+ t .Run ("Successfully runs a job without options" , func (t * testing.T ) {
47+ t .Parallel ()
48+ job , err := testQueuer .AddJob (TaskMock , 1 , "2" )
49+ assert .NoError (t , err , "AddJob should not return an error on success" )
50+
51+ queuedJob , err := testQueuer .GetJob (job .RID )
52+ assert .NoError (t , err , "GetJob should not return an error" )
53+ assert .NotNil (t , queuedJob , "GetJob should return the job that is currently running" )
54+
55+ // Initialize the queuer and start processing jobs
56+ testQueuer .AddTask (TaskMock )
57+ ctx , cancel := context .WithCancel (context .Background ())
58+ defer cancel ()
59+ testQueuer .Start (ctx , cancel )
60+
61+ time .Sleep (2 * time .Second )
62+
63+ jobNotExisting , err := testQueuer .GetJob (job .RID )
64+ assert .Error (t , err , "GetJob should return an error for cancelled job" )
65+ assert .Nil (t , jobNotExisting , "GetJob should return nil for cancelled job" )
66+
67+ jobArchived , err := testQueuer .dbJob .SelectJobFromArchive (job .RID )
68+ assert .NoError (t , err , "SelectJobFromArchive should not return an error for archived job" )
69+ assert .NotNil (t , jobArchived , "SelectJobFromArchive should return the archived job" )
70+ assert .Equal (t , jobArchived .Status , model .JobStatusSucceeded , "Archived job should have status Succeeded" )
71+ })
72+
4773 t .Run ("Returns error for nil function" , func (t * testing.T ) {
48- var nilTask func () // A nil function is a common nil interface{} variant
74+ var nilTask func () // Invalid nil function
4975 job , err := testQueuer .AddJob (nilTask , "param1" )
5076
5177 assert .Error (t , err , "AddJob should return an error for nil task (via addJobFn)" )
@@ -54,7 +80,7 @@ func TestAddJob(t *testing.T) {
5480 })
5581
5682 t .Run ("Returns error for invalid task type" , func (t * testing.T ) {
57- invalidTask := 123 // An integer is not a valid task type
83+ invalidTask := 123 // Invalid integer type instead of a function
5884 job , err := testQueuer .AddJob (invalidTask , "param1" )
5985
6086 assert .Error (t , err , "AddJob should return an error for invalid task type" )
@@ -110,10 +136,57 @@ func TestAddJobWithOptions(t *testing.T) {
110136 assert .EqualValues (t , expectedJob .Parameters , job .Parameters , "AddJobWithOptions should return the correct parameters" )
111137 })
112138
139+ t .Run ("Successfully runs a job with options" , func (t * testing.T ) {
140+ t .Parallel ()
141+ options := & model.Options {
142+ OnError : & model.OnError {
143+ Timeout : 5 ,
144+ MaxRetries : 3 ,
145+ RetryDelay : 1 ,
146+ RetryBackoff : model .RETRY_BACKOFF_EXPONENTIAL ,
147+ },
148+ Schedule : & model.Schedule {
149+ Start : time .Now ().Add (1 * time .Second ),
150+ Interval : 15 * time .Minute ,
151+ MaxCount : 3 ,
152+ },
153+ }
154+ job , err := testQueuer .AddJobWithOptions (options , TaskMock , 1 , "2" )
155+ assert .NoError (t , err , "AddJob should not return an error on success" )
156+
157+ queuedJob , err := testQueuer .GetJob (job .RID )
158+ assert .NoError (t , err , "GetJob should not return an error" )
159+ assert .NotNil (t , queuedJob , "GetJob should return the job that is currently running" )
160+
161+ // Initialize the queuer and start processing jobs
162+ testQueuer .AddTask (TaskMock )
163+ ctx , cancel := context .WithCancel (context .Background ())
164+ defer cancel ()
165+ testQueuer .Start (ctx , cancel )
166+
167+ // Wait for the job to be running
168+ time .Sleep (1500 * time .Millisecond )
169+ jobScheduled , err := testQueuer .GetJob (job .RID )
170+ assert .NoError (t , err , "GetJob should not return an error for running job" )
171+ assert .NotNil (t , jobScheduled , "GetJob should return the job that is currently running" )
172+ assert .Equal (t , model .JobStatusRunning , jobScheduled .Status , "Job should be in Scheduled status" )
173+
174+ // Wait for schedule time and job execution
175+ time .Sleep (2 * time .Second )
176+ jobNotExisting , err := testQueuer .GetJob (job .RID )
177+ assert .Error (t , err , "GetJob should return an error for cancelled job" )
178+ assert .Nil (t , jobNotExisting , "GetJob should return nil for cancelled job" )
179+
180+ jobArchived , err := testQueuer .dbJob .SelectJobFromArchive (job .RID )
181+ assert .NoError (t , err , "SelectJobFromArchive should not return an error for archived job" )
182+ assert .NotNil (t , jobArchived , "SelectJobFromArchive should return the archived job" )
183+ assert .Equal (t , jobArchived .Status , model .JobStatusSucceeded , "Archived job should have status Succeeded" )
184+ })
185+
113186 t .Run ("Return error for invalid options" , func (t * testing.T ) {
114187 options := & model.Options {
115188 OnError : & model.OnError {
116- Timeout : - 5 ,
189+ Timeout : - 5 , // Invalid timeout
117190 MaxRetries : 3 ,
118191 RetryDelay : 1 ,
119192 RetryBackoff : model .RETRY_BACKOFF_EXPONENTIAL ,
@@ -161,7 +234,7 @@ func TestAddJobs(t *testing.T) {
161234 t .Run ("Returns error for invalid batch job" , func (t * testing.T ) {
162235 batchJobs := []model.BatchJob {
163236 {
164- Task : nil , // Invalid task
237+ Task : nil , // Invalid nil function
165238 Parameters : []interface {}{1 , "2" },
166239 Options : nil ,
167240 },
@@ -175,7 +248,7 @@ func TestAddJobs(t *testing.T) {
175248func TestCancelJob (t * testing.T ) {
176249 testQueuer := newQueuerMock ("TestQueuer" , 1 )
177250
178- t .Run ("Successfully cancels a job" , func (t * testing.T ) {
251+ t .Run ("Successfully cancels a queued job" , func (t * testing.T ) {
179252 job , err := testQueuer .AddJob (TaskMock , 1 , "2" )
180253 assert .NoError (t , err , "AddJob should not return an error on success" )
181254
@@ -190,6 +263,37 @@ func TestCancelJob(t *testing.T) {
190263 assert .NotContains (t , jobs , cancelledJob , "Cancelled job should not be in the job list" )
191264 })
192265
266+ t .Run ("Successfully cancels a running job" , func (t * testing.T ) {
267+ t .Parallel ()
268+ job , err := testQueuer .AddJob (TaskMock , 3 , "2" )
269+ assert .NoError (t , err , "AddJob should not return an error on success" )
270+
271+ queuedJob , err := testQueuer .GetJob (job .RID )
272+ assert .NoError (t , err , "GetJob should not return an error" )
273+ assert .NotNil (t , queuedJob , "GetJob should return the job that is currently running" )
274+
275+ // Initialize the queuer and start processing jobs
276+ testQueuer .AddTask (TaskMock )
277+ ctx , cancel := context .WithCancel (context .Background ())
278+ defer cancel ()
279+ testQueuer .Start (ctx , cancel )
280+
281+ time .Sleep (1 * time .Second )
282+
283+ cancelledJob , err := testQueuer .CancelJob (job .RID )
284+ assert .NoError (t , err , "CancelJob should not return an error on success" )
285+ assert .Equal (t , job .RID , cancelledJob .RID , "CancelJob should return the correct job RID" )
286+
287+ jobNotExisting , err := testQueuer .GetJob (job .RID )
288+ assert .Error (t , err , "GetJob should return an error for cancelled job" )
289+ assert .Nil (t , jobNotExisting , "GetJob should return nil for cancelled job" )
290+
291+ jobArchived , err := testQueuer .dbJob .SelectJobFromArchive (job .RID )
292+ assert .NoError (t , err , "SelectJobFromArchive should not return an error for archived job" )
293+ assert .NotNil (t , jobArchived , "SelectJobFromArchive should return the archived job" )
294+ assert .Equal (t , jobArchived .Status , model .JobStatusCancelled , "Archived job should have status Cancelled" )
295+ })
296+
193297 t .Run ("Returns error for non-existent job" , func (t * testing.T ) {
194298 cancelledJob , err := testQueuer .CancelJob (uuid .New ())
195299 assert .Error (t , err , "CancelJob should return an error for non-existent job" )
0 commit comments