2016-03-15 21:38:16 +03:00
|
|
|
package hystrix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2016-03-15 21:45:59 +03:00
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
2016-03-15 21:38:16 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/registry/mock"
|
|
|
|
"github.com/micro/go-micro/selector"
|
|
|
|
|
2018-03-03 15:28:44 +03:00
|
|
|
"context"
|
2016-03-15 21:38:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBreaker(t *testing.T) {
|
|
|
|
// setup
|
|
|
|
r := mock.NewRegistry()
|
|
|
|
s := selector.NewSelector(selector.Registry(r))
|
|
|
|
|
|
|
|
c := client.NewClient(
|
|
|
|
// set the selector
|
|
|
|
client.Selector(s),
|
|
|
|
// add the breaker wrapper
|
|
|
|
client.Wrap(NewClientWrapper()),
|
|
|
|
)
|
|
|
|
|
2018-04-17 13:26:18 +03:00
|
|
|
req := c.NewRequest("test.service", "Test.Method", map[string]string{
|
2016-03-15 21:38:16 +03:00
|
|
|
"foo": "bar",
|
2018-04-17 13:26:18 +03:00
|
|
|
}, client.WithContentType("application/json"))
|
2016-03-15 21:38:16 +03:00
|
|
|
|
|
|
|
var rsp map[string]interface{}
|
|
|
|
|
|
|
|
// Force to point of trip
|
2017-01-09 17:50:28 +03:00
|
|
|
for i := 0; i < (hystrix.DefaultVolumeThreshold * 3); i++ {
|
2016-03-15 21:38:16 +03:00
|
|
|
c.Call(context.TODO(), req, rsp)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.Call(context.TODO(), req, rsp)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expecting tripped breaker, got nil error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != "hystrix: circuit open" {
|
2016-03-15 21:45:59 +03:00
|
|
|
t.Errorf("Expecting tripped breaker, got %v", err)
|
2016-03-15 21:38:16 +03:00
|
|
|
}
|
|
|
|
}
|