github.com/lingyao2333/mo-zero@v1.4.1/core/queue/util_test.go (about) 1 package queue 2 3 import ( 4 "errors" 5 "math" 6 "testing" 7 8 "github.com/lingyao2333/mo-zero/core/logx" 9 "github.com/lingyao2333/mo-zero/core/mathx" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 var ( 14 proba = mathx.NewProba() 15 failProba = 0.01 16 ) 17 18 func init() { 19 logx.Disable() 20 } 21 22 func TestGenerateName(t *testing.T) { 23 pushers := []Pusher{ 24 &mockedPusher{name: "first"}, 25 &mockedPusher{name: "second"}, 26 &mockedPusher{name: "third"}, 27 } 28 29 assert.Equal(t, "first,second,third", generateName(pushers)) 30 } 31 32 func TestGenerateNameNil(t *testing.T) { 33 var pushers []Pusher 34 assert.Equal(t, "", generateName(pushers)) 35 } 36 37 func calcMean(vals []int) float64 { 38 if len(vals) == 0 { 39 return 0 40 } 41 42 var result float64 43 for _, val := range vals { 44 result += float64(val) 45 } 46 return result / float64(len(vals)) 47 } 48 49 func calcVariance(mean float64, vals []int) float64 { 50 if len(vals) == 0 { 51 return 0 52 } 53 54 var result float64 55 for _, val := range vals { 56 result += math.Pow(float64(val)-mean, 2) 57 } 58 return result / float64(len(vals)) 59 } 60 61 type mockedPusher struct { 62 name string 63 count int 64 } 65 66 func (p *mockedPusher) Name() string { 67 return p.name 68 } 69 70 func (p *mockedPusher) Push(s string) error { 71 if proba.TrueOnProba(failProba) { 72 return errors.New("dummy") 73 } 74 75 p.count++ 76 return nil 77 }