-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue_test.go
More file actions
202 lines (171 loc) · 4.2 KB
/
queue_test.go
File metadata and controls
202 lines (171 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"sync"
"testing"
"time"
"github.com/google/go-github/v72/github"
)
func TestSimplifiedJobQueue(t *testing.T) {
// Create a test queue
queue := &Queue{
jobs: make([]*Job, 0),
maxConcurrency: 2,
}
queue.schedulerCond = sync.NewCond(&queue.mutex)
// Create a test service with minimal config
s := &Service{
config: Config{
MaxConcurrency: 2,
},
queue: queue,
}
// Test queue status initially
queued, running := s.queue.getQueueStatus()
if queued != 0 {
t.Errorf("Expected 0 queued jobs, got %d", queued)
}
if running != 0 {
t.Errorf("Expected 0 running jobs, got %d", running)
}
// Create a test job
job := &Job{
ID: "test-job-1",
Name: "test",
Priority: 0,
Event: &Event{},
}
// Enqueue a job
s.queue.enqueueJob(job, s)
// Check queue status
queued, running = s.queue.getQueueStatus()
if queued != 1 {
t.Errorf("Expected 1 queued job, got %d", queued)
}
if running != 0 {
t.Errorf("Expected 0 running jobs, got %d", running)
}
// Check that the job is in queued state
if job.State != JobStateQueued {
t.Errorf("Expected job state to be queued, got %v", job.State)
}
}
func TestJobPriorityOrdering(t *testing.T) {
// Create a test queue
queue := &Queue{
jobs: make([]*Job, 0),
maxConcurrency: 1,
}
queue.schedulerCond = sync.NewCond(&queue.mutex)
s := &Service{
config: Config{
MaxConcurrency: 1,
},
queue: queue,
}
// Create jobs with different priorities
job1 := &Job{ID: "job1", Name: "low", Priority: 1, Event: &Event{}}
job2 := &Job{ID: "job2", Name: "high", Priority: 10, Event: &Event{}}
job3 := &Job{ID: "job3", Name: "medium", Priority: 5, Event: &Event{}}
// Enqueue in random order
s.queue.enqueueJob(job1, s)
s.queue.enqueueJob(job2, s)
s.queue.enqueueJob(job3, s)
// Check that findJobToStart picks the highest priority job
queue.mutex.Lock()
jobToStart := queue.findJobToStart()
queue.mutex.Unlock()
if jobToStart == nil {
t.Errorf("Expected to find a job to start")
} else if jobToStart.ID != "job2" {
t.Errorf("Expected job2 (highest priority) to be selected, got %s", jobToStart.ID)
}
}
func TestDeduplication(t *testing.T) {
// Create a test queue
queue := &Queue{
jobs: make([]*Job, 0),
maxConcurrency: 2,
}
queue.schedulerCond = sync.NewCond(&queue.mutex)
s := &Service{
config: Config{
MaxConcurrency: 2,
},
queue: queue,
}
// Create jobs with same dedup key
job1 := &Job{
ID: "job1",
Name: "test",
Priority: 1,
Dedup: DedupKill,
Event: &Event{
Repo: &github.Repository{
Owner: &github.User{Login: github.Ptr("owner")},
Name: github.Ptr("repo"),
},
},
}
job2 := &Job{
ID: "job2",
Name: "test",
Priority: 1,
Dedup: DedupKill,
Event: &Event{
Repo: &github.Repository{
Owner: &github.User{Login: github.Ptr("owner")},
Name: github.Ptr("repo"),
},
},
}
// Enqueue first job
s.queue.enqueueJob(job1, s)
// Set first job to running state
queue.mutex.Lock()
job1.State = JobStateRunning
queue.mutex.Unlock()
// Enqueue second job with same dedup key
s.queue.enqueueJob(job2, s)
// First job should be cancelled due to deduplication
time.Sleep(10 * time.Millisecond) // Give time for cancel to propagate
}
func TestJobStates(t *testing.T) {
// Create a test queue
queue := &Queue{
jobs: make([]*Job, 0),
maxConcurrency: 1,
}
queue.schedulerCond = sync.NewCond(&queue.mutex)
s := &Service{
config: Config{
MaxConcurrency: 1,
},
queue: queue,
}
job := &Job{
ID: "test-job",
Name: "test",
Priority: 1,
Event: &Event{},
}
// Test initial state after enqueue
s.queue.enqueueJob(job, s)
if job.State != JobStateQueued {
t.Errorf("Expected job state to be queued, got %v", job.State)
}
// Test running state
queue.mutex.Lock()
job.State = JobStateRunning
job.StartedAt = time.Now()
queue.mutex.Unlock()
if job.State != JobStateRunning {
t.Errorf("Expected job state to be running, got %v", job.State)
}
// Test job finished
s.queue.onJobFinished(job)
// Job should be removed from the list
allJobs := s.queue.getAllJobs()
if len(allJobs) != 0 {
t.Errorf("Expected no jobs after finish, got %d", len(allJobs))
}
}