2016-03-15 21:38:16 +03:00
|
|
|
package gobreaker
|
|
|
|
|
|
|
|
import (
|
2019-03-05 15:24:45 +03:00
|
|
|
"context"
|
2016-03-15 21:38:16 +03:00
|
|
|
"testing"
|
|
|
|
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/errors"
|
|
|
|
"github.com/micro/go-micro/v2/registry/memory"
|
2020-07-17 01:29:28 +03:00
|
|
|
"github.com/micro/go-micro/v2/router"
|
2016-03-15 21:38:16 +03:00
|
|
|
"github.com/sony/gobreaker"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
c := client.NewClient(
|
|
|
|
// set the selector
|
2020-07-17 01:29:28 +03:00
|
|
|
client.Router(rrouter.NewRouter(router.Registry(registry))),
|
2016-03-15 21:38:16 +03:00
|
|
|
// add the breaker wrapper
|
2019-03-05 15:24:45 +03:00
|
|
|
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 < 6; 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
merr := err.(*errors.Error)
|
|
|
|
if merr.Code != 502 {
|
|
|
|
t.Errorf("Expecting tripped breaker, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomBreaker(t *testing.T) {
|
|
|
|
// setup
|
|
|
|
r := memory.NewRegistry()
|
|
|
|
|
|
|
|
c := client.NewClient(
|
|
|
|
// set the selector
|
2020-07-17 01:29:28 +03:00
|
|
|
client.Router(rrouter.NewRouter(router.Registry(registry))),
|
2019-03-05 15:24:45 +03:00
|
|
|
// add the breaker wrapper
|
|
|
|
client.Wrap(NewCustomClientWrapper(
|
|
|
|
gobreaker.Settings{},
|
|
|
|
BreakService,
|
2016-03-15 21:38:16 +03:00
|
|
|
)),
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
for i := 0; i < 6; 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")
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:24:45 +03:00
|
|
|
merr := err.(*errors.Error)
|
|
|
|
if merr.Code != 502 {
|
2016-03-15 21:45:54 +03:00
|
|
|
t.Errorf("Expecting tripped breaker, got %v", err)
|
2016-03-15 21:38:16 +03:00
|
|
|
}
|
|
|
|
}
|