micro-wrapper-breaker-sony/gobreaker_test.go

48 lines
954 B
Go
Raw Normal View History

2016-03-15 21:38:16 +03:00
package gobreaker
import (
"testing"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/registry/mock"
"github.com/micro/go-micro/selector"
"github.com/sony/gobreaker"
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(
gobreaker.NewCircuitBreaker(gobreaker.Settings{}),
)),
)
req := c.NewJsonRequest("test.service", "Test.Method", map[string]string{
"foo": "bar",
})
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")
}
if err.Error() != "circuit breaker is open" {
2016-03-15 21:45:54 +03:00
t.Errorf("Expecting tripped breaker, got %v", err)
2016-03-15 21:38:16 +03:00
}
}