Skip to content

Commit 6a7acfe

Browse files
committed
add first draft running queuer tests
1 parent 9d42b2a commit 6a7acfe

4 files changed

Lines changed: 125 additions & 19 deletions

File tree

helper/database_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
_ "github.com/lib/pq"
1212
)
1313

14-
var port string
14+
var dbPort string
1515

1616
func TestMain(m *testing.M) {
1717
var teardown func(ctx context.Context, opts ...testcontainers.TerminateOption) error
1818
var err error
19-
teardown, port, err = MustStartPostgresContainer()
19+
teardown, dbPort, err = MustStartPostgresContainer()
2020
if err != nil {
2121
log.Fatalf("error starting postgres container: %v", err)
2222
}
@@ -29,14 +29,14 @@ func TestMain(m *testing.M) {
2929
}
3030

3131
func TestNew(t *testing.T) {
32-
dbConfig := NewTestDatabaseConfig(port)
32+
dbConfig := NewTestDatabaseConfig(dbPort)
3333
database := NewTestDatabase(dbConfig)
3434

3535
assert.NotNil(t, database, "expected NewDatabase to return a non-nil instance")
3636
}
3737

3838
func TestHealth(t *testing.T) {
39-
dbConfig := NewTestDatabaseConfig(port)
39+
dbConfig := NewTestDatabaseConfig(dbPort)
4040
database := NewTestDatabase(dbConfig)
4141

4242
stats := database.Health()
@@ -48,7 +48,7 @@ func TestHealth(t *testing.T) {
4848
}
4949

5050
func TestClose(t *testing.T) {
51-
dbConfig := NewTestDatabaseConfig(port)
51+
dbConfig := NewTestDatabaseConfig(dbPort)
5252
database := NewTestDatabase(dbConfig)
5353

5454
assert.NotNil(t, database, "expected NewDatabase to return a non-nil instance")

main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"github.com/testcontainers/testcontainers-go"
1414
)
1515

16-
var port string
16+
var dbPort string
1717

1818
func TestMain(m *testing.M) {
1919
var teardown func(ctx context.Context, opts ...testcontainers.TerminateOption) error
2020
var err error
21-
teardown, port, err = helper.MustStartPostgresContainer()
21+
teardown, dbPort, err = helper.MustStartPostgresContainer()
2222
if err != nil {
2323
log.Fatalf("error starting postgres container: %v", err)
2424
}
@@ -35,7 +35,7 @@ func newQueuerMock(name string, maxConcurrency int, options ...*model.OnError) *
3535
logger := log.New(os.Stdout, "Queuer: ", log.Ltime)
3636

3737
// Database
38-
dbConfig := helper.NewTestDatabaseConfig(port)
38+
dbConfig := helper.NewTestDatabaseConfig(dbPort)
3939
dbConnection := helper.NewTestDatabase(dbConfig)
4040

4141
// DBs

queuerJob.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package queuer
22

33
import (
44
"fmt"
5+
"log"
56
"queuer/core"
67
"queuer/model"
78
"time"
@@ -191,6 +192,7 @@ func (q *Queuer) runJobInitial() error {
191192
if err != nil {
192193
return fmt.Errorf("error creating scheduler: %v", err)
193194
}
195+
log.Printf("Scheduling job with RID %v to run at %v", job.RID, job.Options.Schedule.Start)
194196
go scheduler.Go(q.ctx)
195197
} else {
196198
go func() {

queuerJob_test.go

Lines changed: 115 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package queuer
22

33
import (
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.
2828
func 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) {
175248
func 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

Comments
 (0)