2020-12-26 15:32:45 +00:00
|
|
|
package hystrix
|
|
|
|
|
|
|
|
import (
|
2021-06-20 16:28:15 +08:00
|
|
|
"context"
|
2021-09-13 16:23:26 +08:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-12-26 15:32:45 +00:00
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
2021-10-12 12:55:53 +01:00
|
|
|
"go-micro.dev/v4/client"
|
|
|
|
merrors "go-micro.dev/v4/errors"
|
|
|
|
"go-micro.dev/v4/registry"
|
|
|
|
"go-micro.dev/v4/selector"
|
2020-12-26 15:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBreaker(t *testing.T) {
|
|
|
|
// setup
|
2021-09-13 16:23:26 +08:00
|
|
|
r := registry.NewMemoryRegistry()
|
2020-12-26 15:32:45 +00:00
|
|
|
s := selector.NewSelector(selector.Registry(r))
|
|
|
|
c := client.NewClient(
|
|
|
|
// set the selector
|
|
|
|
client.Selector(s),
|
|
|
|
// add the breaker wrapper
|
2021-09-13 16:23:26 +08:00
|
|
|
client.Wrap(NewClientWrapper()),
|
2020-12-26 15:32:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
req := c.NewRequest("test.service", "Test.Method", map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}, client.WithContentType("application/json"))
|
|
|
|
|
|
|
|
var rsp map[string]interface{}
|
|
|
|
|
|
|
|
// Force to point of trip
|
|
|
|
for i := 0; i < (hystrix.DefaultVolumeThreshold * 3); i++ {
|
|
|
|
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" {
|
|
|
|
t.Errorf("Expecting tripped breaker, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 16:23:26 +08:00
|
|
|
|
|
|
|
func TestBreakerWithFilter(t *testing.T) {
|
|
|
|
r := registry.NewMemoryRegistry()
|
|
|
|
s := selector.NewSelector(selector.Registry(r))
|
|
|
|
c := client.NewClient(
|
|
|
|
client.Selector(s),
|
2021-09-28 18:23:04 +08:00
|
|
|
client.Wrap(NewClientWrapper(WithFilter(func(c context.Context, e error) bool {
|
2021-09-13 16:23:26 +08:00
|
|
|
var merr *merrors.Error
|
|
|
|
if errors.As(e, &merr) && merr.Detail == "service test.service: not found" {
|
2021-09-28 18:23:04 +08:00
|
|
|
return true
|
2021-09-13 16:23:26 +08:00
|
|
|
}
|
2021-09-28 18:23:04 +08:00
|
|
|
return false
|
2021-09-13 16:23:26 +08:00
|
|
|
}))),
|
|
|
|
)
|
|
|
|
|
|
|
|
req := c.NewRequest("test.service", "Test.FilterMethod", nil)
|
|
|
|
for i := 0; i < (hystrix.DefaultVolumeThreshold); i++ {
|
|
|
|
c.Call(context.TODO(), req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
circuit, _, _ := hystrix.GetCircuit("test.service.Test.FilterMethod")
|
|
|
|
if circuit.IsOpen() {
|
|
|
|
t.Errorf("breaker should not be opened")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.Call(context.TODO(), req, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("original error should be throw out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBreakerWithFallback(t *testing.T) {
|
|
|
|
r := registry.NewMemoryRegistry()
|
|
|
|
s := selector.NewSelector(selector.Registry(r))
|
|
|
|
c := client.NewClient(
|
|
|
|
client.Selector(s),
|
|
|
|
client.Wrap(NewClientWrapper(WithFallback(func(c context.Context, e error) error {
|
|
|
|
var merr *merrors.Error
|
|
|
|
if errors.As(e, &merr) && merr.Detail == "service test.service: not found" {
|
|
|
|
return hystrix.ErrCircuitOpen
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}))),
|
|
|
|
)
|
|
|
|
|
|
|
|
// trigger fallback to open circuit breaker
|
|
|
|
req := c.NewRequest("test.service", "Test.FallbackMethod", nil)
|
|
|
|
for i := 0; i < (hystrix.DefaultVolumeThreshold); i++ {
|
|
|
|
c.Call(context.TODO(), req, nil)
|
|
|
|
}
|
|
|
|
err := c.Call(context.TODO(), req, nil)
|
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), "fallback failed with 'hystrix: circuit open'") {
|
|
|
|
t.Error("fallback-failure error should be throw out")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
circuit, _, _ := hystrix.GetCircuit("test.service.Test.FallbackMethod")
|
|
|
|
if !circuit.IsOpen() {
|
|
|
|
t.Errorf("breaker should be opened")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Call(context.TODO(), req, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("original error should be throw out")
|
|
|
|
}
|
|
|
|
}
|