Skip to content

Commit

Permalink
Merge pull request #73022 from denkensk/fix-data-race-in-scheduling-q…
Browse files Browse the repository at this point in the history
…ueue-test

fix data race in TestPriorityQueue_AssignedPodAdded
  • Loading branch information
k8s-ci-robot committed Jan 18, 2019
2 parents 49c8390 + 28e6bbc commit c999dc3
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions pkg/scheduler/internal/queue/scheduling_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ var highPriorityPod, highPriNominatedPod, medPriorityPod, unschedulablePod = v1.
},
}

func addOrUpdateUnschedulablePod(p *PriorityQueue, pod *v1.Pod) {
p.lock.Lock()
defer p.lock.Unlock()
p.unschedulableQ.addOrUpdate(pod)
}

func getUnschedulablePod(p *PriorityQueue, pod *v1.Pod) *v1.Pod {
p.lock.Lock()
defer p.lock.Unlock()
return p.unschedulableQ.get(pod)
}

func TestPriorityQueue_Add(t *testing.T) {
q := NewPriorityQueue(nil)
if err := q.Add(&medPriorityPod); err != nil {
Expand Down Expand Up @@ -134,7 +146,7 @@ func TestPriorityQueue_Add(t *testing.T) {

func TestPriorityQueue_AddIfNotPresent(t *testing.T) {
q := NewPriorityQueue(nil)
q.unschedulableQ.addOrUpdate(&highPriNominatedPod)
addOrUpdateUnschedulablePod(q, &highPriNominatedPod)
q.AddIfNotPresent(&highPriNominatedPod) // Must not add anything.
q.AddIfNotPresent(&medPriorityPod)
q.AddIfNotPresent(&unschedulablePod)
Expand All @@ -159,7 +171,7 @@ func TestPriorityQueue_AddIfNotPresent(t *testing.T) {
if len(q.nominatedPods.nominatedPods["node1"]) != 2 {
t.Errorf("Expected medPriorityPod and unschedulablePod to be still present in nomindatePods: %v", q.nominatedPods.nominatedPods["node1"])
}
if q.unschedulableQ.get(&highPriNominatedPod) != &highPriNominatedPod {
if getUnschedulablePod(q, &highPriNominatedPod) != &highPriNominatedPod {
t.Errorf("Pod %v was not found in the unschedulableQ.", highPriNominatedPod.Name)
}
}
Expand Down Expand Up @@ -192,7 +204,7 @@ func TestPriorityQueue_AddUnschedulableIfNotPresent(t *testing.T) {
if len(q.nominatedPods.nominatedPods) != 1 {
t.Errorf("Expected nomindatePods to have one element: %v", q.nominatedPods)
}
if q.unschedulableQ.get(&unschedulablePod) != &unschedulablePod {
if getUnschedulablePod(q, &unschedulablePod) != &unschedulablePod {
t.Errorf("Pod %v was not found in the unschedulableQ.", unschedulablePod.Name)
}
}
Expand Down Expand Up @@ -277,8 +289,8 @@ func TestPriorityQueue_Delete(t *testing.T) {
func TestPriorityQueue_MoveAllToActiveQueue(t *testing.T) {
q := NewPriorityQueue(nil)
q.Add(&medPriorityPod)
q.unschedulableQ.addOrUpdate(&unschedulablePod)
q.unschedulableQ.addOrUpdate(&highPriorityPod)
addOrUpdateUnschedulablePod(q, &unschedulablePod)
addOrUpdateUnschedulablePod(q, &highPriorityPod)
q.MoveAllToActiveQueue()
if q.activeQ.Len() != 3 {
t.Error("Expected all items to be in activeQ.")
Expand Down Expand Up @@ -324,19 +336,19 @@ func TestPriorityQueue_AssignedPodAdded(t *testing.T) {
q := NewPriorityQueue(nil)
q.Add(&medPriorityPod)
// Add a couple of pods to the unschedulableQ.
q.unschedulableQ.addOrUpdate(&unschedulablePod)
q.unschedulableQ.addOrUpdate(affinityPod)
addOrUpdateUnschedulablePod(q, &unschedulablePod)
addOrUpdateUnschedulablePod(q, affinityPod)
// Simulate addition of an assigned pod. The pod has matching labels for
// affinityPod. So, affinityPod should go to activeQ.
q.AssignedPodAdded(&labelPod)
if q.unschedulableQ.get(affinityPod) != nil {
if getUnschedulablePod(q, affinityPod) != nil {
t.Error("affinityPod is still in the unschedulableQ.")
}
if _, exists, _ := q.activeQ.Get(affinityPod); !exists {
t.Error("affinityPod is not moved to activeQ.")
}
// Check that the other pod is still in the unschedulableQ.
if q.unschedulableQ.get(&unschedulablePod) == nil {
if getUnschedulablePod(q, &unschedulablePod) == nil {
t.Error("unschedulablePod is not in the unschedulableQ.")
}
}
Expand All @@ -361,8 +373,8 @@ func TestPriorityQueue_NominatedPodsForNode(t *testing.T) {
func TestPriorityQueue_PendingPods(t *testing.T) {
q := NewPriorityQueue(nil)
q.Add(&medPriorityPod)
q.unschedulableQ.addOrUpdate(&unschedulablePod)
q.unschedulableQ.addOrUpdate(&highPriorityPod)
addOrUpdateUnschedulablePod(q, &unschedulablePod)
addOrUpdateUnschedulablePod(q, &highPriorityPod)
expectedList := []*v1.Pod{&medPriorityPod, &unschedulablePod, &highPriorityPod}
if !reflect.DeepEqual(expectedList, q.PendingPods()) {
t.Error("Unexpected list of pending Pods for node.")
Expand Down

0 comments on commit c999dc3

Please sign in to comment.