OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / go-kit / kit / circuitbreaker / hystrix_test.go
1 package circuitbreaker_test
2
3 import (
4         "io/ioutil"
5         stdlog "log"
6         "testing"
7         "time"
8
9         "github.com/afex/hystrix-go/hystrix"
10
11         "github.com/go-kit/kit/circuitbreaker"
12 )
13
14 func TestHystrix(t *testing.T) {
15         stdlog.SetOutput(ioutil.Discard)
16
17         const (
18                 commandName   = "my-endpoint"
19                 errorPercent  = 5
20                 maxConcurrent = 1000
21         )
22         hystrix.ConfigureCommand(commandName, hystrix.CommandConfig{
23                 ErrorPercentThreshold: errorPercent,
24                 MaxConcurrentRequests: maxConcurrent,
25         })
26
27         var (
28                 breaker          = circuitbreaker.Hystrix(commandName)
29                 primeWith        = hystrix.DefaultVolumeThreshold * 2
30                 shouldPass       = func(n int) bool { return (float64(n) / float64(primeWith+n)) <= (float64(errorPercent-1) / 100.0) }
31                 openCircuitError = hystrix.ErrCircuitOpen.Error()
32         )
33
34         // hystrix-go uses buffered channels to receive reports on request success/failure,
35         // and so is basically impossible to test deterministically. We have to make sure
36         // the report buffer is emptied, by injecting a sleep between each invocation.
37         requestDelay := 5 * time.Millisecond
38
39         testFailingEndpoint(t, breaker, primeWith, shouldPass, requestDelay, openCircuitError)
40 }