github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/memberlist/awareness_test.go (about) 1 package memberlist 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestAwareness(t *testing.T) { 9 cases := []struct { 10 delta int 11 score int 12 timeout time.Duration 13 }{ 14 {0, 0, 1 * time.Second}, 15 {-1, 0, 1 * time.Second}, 16 {-10, 0, 1 * time.Second}, 17 {1, 1, 2 * time.Second}, 18 {-1, 0, 1 * time.Second}, 19 {10, 7, 8 * time.Second}, 20 {-1, 6, 7 * time.Second}, 21 {-1, 5, 6 * time.Second}, 22 {-1, 4, 5 * time.Second}, 23 {-1, 3, 4 * time.Second}, 24 {-1, 2, 3 * time.Second}, 25 {-1, 1, 2 * time.Second}, 26 {-1, 0, 1 * time.Second}, 27 {-1, 0, 1 * time.Second}, 28 } 29 30 a := newAwareness(8) 31 for i, c := range cases { 32 a.ApplyDelta(c.delta) 33 if a.GetHealthScore() != c.score { 34 t.Errorf("case %d: score mismatch %d != %d", i, a.score, c.score) 35 } 36 if timeout := a.ScaleTimeout(1 * time.Second); timeout != c.timeout { 37 t.Errorf("case %d: scaled timeout mismatch %9.6f != %9.6f", 38 i, timeout.Seconds(), c.timeout.Seconds()) 39 } 40 } 41 }