Fix integration test flake on TestScorePlugin

This commit is contained in:
Wei Huang 2020-07-27 18:13:38 -07:00
parent 575c4925be
commit 8936f9e4bf
No known key found for this signature in database
GPG Key ID: BE5E9752F8B6E005

View File

@ -19,6 +19,7 @@ package scheduler
import ( import (
"context" "context"
"fmt" "fmt"
"sync/atomic"
"testing" "testing"
"time" "time"
@ -45,7 +46,7 @@ type PreFilterPlugin struct {
type ScorePlugin struct { type ScorePlugin struct {
failScore bool failScore bool
numScoreCalled int numScoreCalled int32
highScoreNode string highScoreNode string
} }
@ -170,13 +171,13 @@ func (sp *ScorePlugin) reset() {
// Score returns the score of scheduling a pod on a specific node. // Score returns the score of scheduling a pod on a specific node.
func (sp *ScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { func (sp *ScorePlugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) {
sp.numScoreCalled++ curCalled := atomic.AddInt32(&sp.numScoreCalled, 1)
if sp.failScore { if sp.failScore {
return 0, framework.NewStatus(framework.Error, fmt.Sprintf("injecting failure for pod %v", p.Name)) return 0, framework.NewStatus(framework.Error, fmt.Sprintf("injecting failure for pod %v", p.Name))
} }
score := int64(1) score := int64(1)
if sp.numScoreCalled == 1 { if curCalled == 1 {
// The first node is scored the highest, the rest is scored lower. // The first node is scored the highest, the rest is scored lower.
sp.highScoreNode = nodeName sp.highScoreNode = nodeName
score = framework.MaxNodeScore score = framework.MaxNodeScore