You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.2 KiB
Go
78 lines
1.2 KiB
Go
4 years ago
|
package queue
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"math"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"github.com/tal-tech/go-zero/core/logx"
|
||
|
"github.com/tal-tech/go-zero/core/mathx"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
proba = mathx.NewProba()
|
||
|
failProba = 0.01
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
logx.Disable()
|
||
|
}
|
||
|
|
||
|
func TestGenerateName(t *testing.T) {
|
||
|
pushers := []Pusher{
|
||
|
&mockedPusher{name: "first"},
|
||
|
&mockedPusher{name: "second"},
|
||
|
&mockedPusher{name: "third"},
|
||
|
}
|
||
|
|
||
|
assert.Equal(t, "first,second,third", generateName(pushers))
|
||
|
}
|
||
|
|
||
|
func TestGenerateNameNil(t *testing.T) {
|
||
|
var pushers []Pusher
|
||
|
assert.Equal(t, "", generateName(pushers))
|
||
|
}
|
||
|
|
||
|
func calcMean(vals []int) float64 {
|
||
|
if len(vals) == 0 {
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
var result float64
|
||
|
for _, val := range vals {
|
||
|
result += float64(val)
|
||
|
}
|
||
|
return result / float64(len(vals))
|
||
|
}
|
||
|
|
||
|
func calcVariance(mean float64, vals []int) float64 {
|
||
|
if len(vals) == 0 {
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
var result float64
|
||
|
for _, val := range vals {
|
||
|
result += math.Pow(float64(val)-mean, 2)
|
||
|
}
|
||
|
return result / float64(len(vals))
|
||
|
}
|
||
|
|
||
|
type mockedPusher struct {
|
||
|
name string
|
||
|
count int
|
||
|
}
|
||
|
|
||
|
func (p *mockedPusher) Name() string {
|
||
|
return p.name
|
||
|
}
|
||
|
|
||
|
func (p *mockedPusher) Push(s string) error {
|
||
|
if proba.TrueOnProba(failProba) {
|
||
|
return errors.New("dummy")
|
||
|
}
|
||
|
|
||
|
p.count++
|
||
|
return nil
|
||
|
}
|