@@ -79,7 +79,7 @@ func TestCPUBalanceOnce(t *testing.T) {
7979 backends := make ([]scoredBackend , 0 , len (test .cpus ))
8080 values := make ([]* model.SampleStream , 0 , len (test .cpus ))
8181 for j := 0 ; j < len (test .cpus ); j ++ {
82- backends = append (backends , createBackend (j , 0 , 0 ))
82+ backends = append (backends , createBackend (j , 100 , 100 ))
8383 values = append (values , createSampleStream (test .cpus [j ], j , model .Now ()))
8484 }
8585 mmr := & mockMetricsReader {
@@ -100,7 +100,8 @@ func TestCPUBalanceOnce(t *testing.T) {
100100 }
101101 require .Equal (t , test .scoreOrder , sortedIdx , "test index %d" , i )
102102 from , to := backends [len (backends )- 1 ], backends [0 ]
103- balanceCount , _ := fc .BalanceCount (from , to )
103+ advice , balanceCount , _ := fc .BalanceCount (from , to )
104+ require .Equal (t , test .balanced , advice == AdviceNeutral , "test index %d" , i )
104105 require .Equal (t , test .balanced , balanceCount < 0.0001 , "test index %d" , i )
105106 }
106107}
@@ -207,8 +208,8 @@ func TestCPUBalanceContinuously(t *testing.T) {
207208 t .Fatal ("balance doesn't stop" )
208209 }
209210 updateScore (fc , backends )
210- balanceCount , _ := fc .BalanceCount (backends [len (backends )- 1 ], backends [0 ])
211- if balanceCount == 0 {
211+ advice , balanceCount , _ := fc .BalanceCount (backends [len (backends )- 1 ], backends [0 ])
212+ if advice != AdvicePositive || balanceCount == 0 {
212213 break
213214 }
214215 count := int (balanceCount + 0.9999 )
@@ -414,3 +415,53 @@ func TestCPUScore(t *testing.T) {
414415 require .Equal (t , test .scores , scores , "test index %d" , i )
415416 }
416417}
418+
419+ func TestCPURejectBalance (t * testing.T ) {
420+ tests := []struct {
421+ cpus [][]float64
422+ conns []int
423+ advice BalanceAdvice
424+ }{
425+ {
426+ cpus : [][]float64 {{0.7 }, {0.7 }},
427+ conns : []int {10 , 10 },
428+ advice : AdviceNegtive ,
429+ },
430+ {
431+ cpus : [][]float64 {{0.75 }, {0.7 }},
432+ conns : []int {10 , 10 },
433+ advice : AdviceNegtive ,
434+ },
435+ {
436+ cpus : [][]float64 {{0.78 }, {0.7 }},
437+ conns : []int {20 , 20 },
438+ advice : AdviceNeutral ,
439+ },
440+ {
441+ cpus : [][]float64 {{0.8 }, {0.6 }},
442+ conns : []int {10 , 10 },
443+ advice : AdvicePositive ,
444+ },
445+ }
446+
447+ for i , test := range tests {
448+ backends := make ([]scoredBackend , 0 , len (test .cpus ))
449+ values := make ([]* model.SampleStream , 0 , len (test .cpus ))
450+ for j := 0 ; j < len (test .cpus ); j ++ {
451+ backends = append (backends , createBackend (j , test .conns [j ], test .conns [j ]))
452+ values = append (values , createSampleStream (test .cpus [j ], j , model .Now ()))
453+ }
454+ mmr := & mockMetricsReader {
455+ qrs : map [string ]metricsreader.QueryResult {
456+ "cpu" : {
457+ UpdateTime : time .Now (),
458+ Value : model .Matrix (values ),
459+ },
460+ },
461+ }
462+ fc := NewFactorCPU (mmr , zap .NewNop ())
463+ updateScore (fc , backends )
464+ advice , _ , _ := fc .BalanceCount (backends [1 ], backends [0 ])
465+ require .Equal (t , test .advice , advice , "test index %d" , i )
466+ }
467+ }
0 commit comments