Skip to content

Commit dca4b68

Browse files
committed
fix queuer job tests
1 parent 1bf499c commit dca4b68

4 files changed

Lines changed: 55 additions & 60 deletions

File tree

core/listener.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ type Listener[T any] struct {
1111

1212
func NewListener[T any]() *Listener[T] {
1313
return &Listener[T]{
14-
Channel: make(chan T, 1),
14+
Channel: make(chan T),
1515
}
1616
}
1717

1818
func (l *Listener[T]) Listen(ctx context.Context, notifyFunction func(data T)) {
19+
log.Printf("Context: %v, Channel: %v", ctx, l.Channel)
1920
for {
20-
log.Printf("Context: %v, Channel: %v", ctx, l.Channel)
2121
select {
2222
case <-ctx.Done():
2323
return
@@ -30,6 +30,10 @@ func (l *Listener[T]) Listen(ctx context.Context, notifyFunction func(data T)) {
3030
}
3131

3232
func (l *Listener[T]) Notify(data T) {
33-
log.Printf("Listener notified with data: %v", data)
34-
l.Channel <- data
33+
select {
34+
case l.Channel <- data:
35+
log.Printf("Listener notified with data: %v", data)
36+
default:
37+
log.Printf("No listener for data: %v", data)
38+
}
3539
}

queuerJob.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (q *Queuer) CancelAllJobsByWorker(workerRid uuid.UUID, entries int) error {
8686
return fmt.Errorf("error selecting jobs by worker RID %v: %v", workerRid, err)
8787
}
8888

89+
log.Printf("Cancelling %d jobs for worker with RID %v", len(jobs), workerRid)
8990
for _, job := range jobs {
9091
err := q.cancelJob(job)
9192
if err != nil {
@@ -288,6 +289,7 @@ func (q *Queuer) scheduleJob(job *model.Job) {
288289
}
289290

290291
func (q *Queuer) cancelJob(job *model.Job) error {
292+
log.Printf("Cancelling job with RID %v", job.RID)
291293
switch job.Status {
292294
case model.JobStatusRunning:
293295
jobRunner, found := q.activeRunners.Load(job.RID)

queuerJob_test.go

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ func TestAddJob(t *testing.T) {
6565

6666
func 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

276269
func 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)

queuerListener_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"queuer/model"
66
"testing"
7+
"time"
78

89
"github.com/google/uuid"
910
"github.com/stretchr/testify/assert"
@@ -12,52 +13,52 @@ import (
1213

1314
func TestListenForJobUpdate(t *testing.T) {
1415
q := newQueuerMock("testQueuer", 1)
15-
require.NotNil(t, q, "expected queuer to be initialized")
16-
require.NotNil(t, q.jobUpdateListener, "expected jobUpdateListener to be initialized")
17-
18-
// Start the queuer
1916
ctx, cancel := context.WithCancel(context.Background())
2017
q.Start(ctx, cancel)
2118

22-
// Use a channel to capture the notification
2319
data := &model.Job{RID: uuid.New()}
2420
notifyChannel := make(chan *model.Job)
2521
err := q.ListenForJobUpdate(func(d *model.Job) {
2622
notifyChannel <- d
2723
})
2824
require.NoError(t, err, "expected to successfully listen for job updates")
2925

26+
time.Sleep(1 * time.Second)
27+
3028
// Notify the listener manually
3129
q.jobUpdateListener.Notify(data)
3230

33-
// Check if the data was received
34-
receivedData := <-notifyChannel
35-
assert.NotNil(t, receivedData, "expected to receive job data")
36-
assert.Equal(t, data.RID, receivedData.RID, "expected to receive the same job RID")
31+
select {
32+
case receivedData := <-notifyChannel:
33+
assert.NotNil(t, receivedData, "expected to receive job data")
34+
assert.Equal(t, data.RID, receivedData.RID, "expected to receive the same job RID")
35+
default:
36+
t.Fatal("expected to receive job data, but got nothing")
37+
}
3738
}
3839

3940
func TestListenForJobDelete(t *testing.T) {
4041
q := newQueuerMock("testQueuer", 1)
41-
require.NotNil(t, q, "expected queuer to be initialized")
42-
require.NotNil(t, q.jobUpdateListener, "expected jobUpdateListener to be initialized")
43-
44-
// Start the queuer
4542
ctx, cancel := context.WithCancel(context.Background())
4643
q.Start(ctx, cancel)
4744

48-
// Use a channel to capture the notification
4945
data := &model.Job{RID: uuid.New()}
5046
notifyChannel := make(chan *model.Job)
5147
err := q.ListenForJobDelete(func(d *model.Job) {
5248
notifyChannel <- d
5349
})
5450
require.NoError(t, err, "expected to successfully listen for job deletions")
5551

52+
time.Sleep(1 * time.Second)
53+
5654
// Notify the listener manually
5755
q.jobDeleteListener.Notify(data)
5856

59-
// Check if the data was received
60-
receivedData := <-notifyChannel
61-
assert.NotNil(t, receivedData, "expected to receive job data")
62-
assert.Equal(t, data.RID, receivedData.RID, "expected to receive the same job RID")
57+
select {
58+
case receivedData := <-notifyChannel:
59+
assert.NotNil(t, receivedData, "expected to receive job data")
60+
assert.Equal(t, data.RID, receivedData.RID, "expected to receive the same job RID")
61+
default:
62+
t.Fatal("expected to receive job data, but got nothing")
63+
}
6364
}

0 commit comments

Comments
 (0)