fix test fails

pull/211/head
kevin 6 years ago
parent 76bfc4250c
commit 34b80a81a4
  1. 11
      pkg/net/netutil/breaker/sre_breaker.go
  2. 4
      pkg/net/netutil/breaker/sre_breaker_test.go

@ -15,15 +15,14 @@ import (
// sreBreaker is a sre CircuitBreaker pattern.
type sreBreaker struct {
stat metric.RollingCounter
r *rand.Rand
// rand.New(...) returns a non thread safe object
randLock sync.Mutex
k float64
request int64
state int32
random *rand.Rand
// rand.New(...) returns a non thread safe object
randLock sync.Mutex
}
func newSRE(c *Config) Breaker {
@ -34,11 +33,11 @@ func newSRE(c *Config) Breaker {
stat := metric.NewRollingCounter(counterOpts)
return &sreBreaker{
stat: stat,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
request: c.Request,
k: c.K,
state: StateClosed,
random: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}
@ -95,7 +94,7 @@ func (b *sreBreaker) MarkFailed() {
func (b *sreBreaker) trueOnProba(proba float64) (truth bool) {
b.randLock.Lock()
truth = b.random.Float64() < proba
truth = b.r.Float64() < proba
b.randLock.Unlock()
return
}

@ -29,6 +29,7 @@ func getSREBreaker() *sreBreaker {
stat := metric.NewRollingCounter(counterOpts)
return &sreBreaker{
stat: stat,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
request: 100,
k: 2,
@ -151,8 +152,9 @@ func TestTrueOnProba(t *testing.T) {
const total = 100000
const epsilon = 0.05
var count int
b := getSREBreaker()
for i := 0; i < total; i++ {
if trueOnProba(proba) {
if b.trueOnProba(proba) {
count++
}
}

Loading…
Cancel
Save