Skip to content

Commit f116308

Browse files
authored
Merge pull request kubernetes#88331 from skilxn-go/FixSchedulerTestDataRace
fix data races in scheduler unit tests
2 parents 343ccde + 74718ad commit f116308

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

pkg/scheduler/internal/queue/scheduling_queue_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,34 +310,42 @@ func TestPriorityQueue_Pop(t *testing.T) {
310310
func TestPriorityQueue_Update(t *testing.T) {
311311
q := createAndRunPriorityQueue(newDefaultFramework())
312312
q.Update(nil, &highPriorityPod)
313+
q.lock.RLock()
313314
if _, exists, _ := q.activeQ.Get(newPodInfoNoTimestamp(&highPriorityPod)); !exists {
314315
t.Errorf("Expected %v to be added to activeQ.", highPriorityPod.Name)
315316
}
317+
q.lock.RUnlock()
316318
if len(q.nominatedPods.nominatedPods) != 0 {
317319
t.Errorf("Expected nomindatePods to be empty: %v", q.nominatedPods)
318320
}
319321
// Update highPriorityPod and add a nominatedNodeName to it.
320322
q.Update(&highPriorityPod, &highPriNominatedPod)
323+
q.lock.RLock()
321324
if q.activeQ.Len() != 1 {
322325
t.Error("Expected only one item in activeQ.")
323326
}
327+
q.lock.RUnlock()
324328
if len(q.nominatedPods.nominatedPods) != 1 {
325329
t.Errorf("Expected one item in nomindatePods map: %v", q.nominatedPods)
326330
}
327331
// Updating an unschedulable pod which is not in any of the two queues, should
328332
// add the pod to activeQ.
329333
q.Update(&unschedulablePod, &unschedulablePod)
334+
q.lock.RLock()
330335
if _, exists, _ := q.activeQ.Get(newPodInfoNoTimestamp(&unschedulablePod)); !exists {
331336
t.Errorf("Expected %v to be added to activeQ.", unschedulablePod.Name)
332337
}
338+
q.lock.RUnlock()
333339
// Updating a pod that is already in activeQ, should not change it.
334340
q.Update(&unschedulablePod, &unschedulablePod)
335341
if len(q.unschedulableQ.podInfoMap) != 0 {
336342
t.Error("Expected unschedulableQ to be empty.")
337343
}
344+
q.lock.RLock()
338345
if _, exists, _ := q.activeQ.Get(newPodInfoNoTimestamp(&unschedulablePod)); !exists {
339346
t.Errorf("Expected: %v to be added to activeQ.", unschedulablePod.Name)
340347
}
348+
q.lock.RUnlock()
341349
if p, err := q.Pop(); err != nil || p.Pod != &highPriNominatedPod {
342350
t.Errorf("Expected: %v after Pop, but got: %v", highPriorityPod.Name, p.Pod.Name)
343351
}
@@ -362,12 +370,14 @@ func TestPriorityQueue_Delete(t *testing.T) {
362370
if err := q.Delete(&highPriNominatedPod); err != nil {
363371
t.Errorf("delete failed: %v", err)
364372
}
373+
q.lock.RLock()
365374
if _, exists, _ := q.activeQ.Get(newPodInfoNoTimestamp(&unschedulablePod)); !exists {
366375
t.Errorf("Expected %v to be in activeQ.", unschedulablePod.Name)
367376
}
368377
if _, exists, _ := q.activeQ.Get(newPodInfoNoTimestamp(&highPriNominatedPod)); exists {
369378
t.Errorf("Didn't expect %v to be in activeQ.", highPriorityPod.Name)
370379
}
380+
q.lock.RUnlock()
371381
if len(q.nominatedPods.nominatedPods) != 1 {
372382
t.Errorf("Expected nomindatePods to have only 'unschedulablePod': %v", q.nominatedPods.nominatedPods)
373383
}
@@ -385,6 +395,8 @@ func TestPriorityQueue_MoveAllToActiveOrBackoffQueue(t *testing.T) {
385395
q.AddUnschedulableIfNotPresent(q.newPodInfo(&unschedulablePod), q.SchedulingCycle())
386396
q.AddUnschedulableIfNotPresent(q.newPodInfo(&highPriorityPod), q.SchedulingCycle())
387397
q.MoveAllToActiveOrBackoffQueue("test")
398+
q.lock.RLock()
399+
defer q.lock.RUnlock()
388400
if q.activeQ.Len() != 1 {
389401
t.Error("Expected 1 item to be in activeQ")
390402
}
@@ -444,9 +456,11 @@ func TestPriorityQueue_AssignedPodAdded(t *testing.T) {
444456
if getUnschedulablePod(q, affinityPod) != nil {
445457
t.Error("affinityPod is still in the unschedulableQ.")
446458
}
459+
q.lock.RLock()
447460
if _, exists, _ := q.activeQ.Get(newPodInfoNoTimestamp(affinityPod)); !exists {
448461
t.Error("affinityPod is not moved to activeQ.")
449462
}
463+
q.lock.RUnlock()
450464
// Check that the other pod is still in the unschedulableQ.
451465
if getUnschedulablePod(q, &unschedulablePod) == nil {
452466
t.Error("unschedulablePod is not in the unschedulableQ.")
@@ -1175,13 +1189,15 @@ func TestPodTimestamp(t *testing.T) {
11751189
op(queue, test.operands[i])
11761190
}
11771191

1192+
queue.lock.Lock()
11781193
for i := 0; i < len(test.expected); i++ {
11791194
if pInfo, err := queue.activeQ.Pop(); err != nil {
11801195
t.Errorf("Error while popping the head of the queue: %v", err)
11811196
} else {
11821197
podInfoList = append(podInfoList, pInfo.(*framework.PodInfo))
11831198
}
11841199
}
1200+
queue.lock.Unlock()
11851201

11861202
if !reflect.DeepEqual(test.expected, podInfoList) {
11871203
t.Errorf("Unexpected PodInfo list. Expected: %v, got: %v",
@@ -1559,9 +1575,11 @@ func TestBackOffFlow(t *testing.T) {
15591575
// An event happens.
15601576
q.MoveAllToActiveOrBackoffQueue("deleted pod")
15611577

1578+
q.lock.RLock()
15621579
if _, ok, _ := q.podBackoffQ.Get(podInfo); !ok {
15631580
t.Errorf("pod %v is not in the backoff queue", podID)
15641581
}
1582+
q.lock.RUnlock()
15651583

15661584
// Check backoff duration.
15671585
deadline := q.getBackoffTime(podInfo)
@@ -1574,15 +1592,19 @@ func TestBackOffFlow(t *testing.T) {
15741592
cl.Step(time.Millisecond)
15751593
q.flushBackoffQCompleted()
15761594
// Still in backoff queue after an early flush.
1595+
q.lock.RLock()
15771596
if _, ok, _ := q.podBackoffQ.Get(podInfo); !ok {
15781597
t.Errorf("pod %v is not in the backoff queue", podID)
15791598
}
1599+
q.lock.RUnlock()
15801600
// Moved out of the backoff queue after timeout.
15811601
cl.Step(backoff)
15821602
q.flushBackoffQCompleted()
1603+
q.lock.RLock()
15831604
if _, ok, _ := q.podBackoffQ.Get(podInfo); ok {
15841605
t.Errorf("pod %v is still in the backoff queue", podID)
15851606
}
1607+
q.lock.RUnlock()
15861608
})
15871609
}
15881610
}

0 commit comments

Comments
 (0)