2020-10-30 23:27:33 +03:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package hystrix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
2021-01-29 16:47:26 +03:00
|
|
|
rrouter "github.com/unistack-org/micro-router-register/v3"
|
|
|
|
"github.com/unistack-org/micro/register/memory"
|
2020-10-30 23:27:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBreaker(t *testing.T) {
|
|
|
|
// setup
|
2021-01-29 16:47:26 +03:00
|
|
|
register := memory.NewRegister()
|
2020-10-30 23:27:33 +03:00
|
|
|
|
|
|
|
c := client.NewClient(
|
|
|
|
// set the selector
|
2021-01-29 16:47:26 +03:00
|
|
|
client.Router(rrouter.NewRouter(router.Register(register))),
|
2020-10-30 23:27:33 +03:00
|
|
|
// add the breaker wrapper
|
|
|
|
client.Wrap(NewClientWrapper()),
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|