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"
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/client/selector"
|
|
|
|
"github.com/micro/go-micro/v2/registry/memory"
|
2016-03-15 21:38:16 +03:00
|
|
|
|
2018-03-03 15:28:44 +03:00
|
|
|
"context"
|
2016-03-15 21:38:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBreaker(t *testing.T) {
|
|
|
|
// setup
|
2019-01-14 18:30:33 +03:00
|
|
|
r := memory.NewRegistry()
|
2016-03-15 21:38:16 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|