micro-wrapper-breaker-hystrix/hystrix_test.go

45 lines
1.0 KiB
Go
Raw Normal View History

2016-03-15 21:38:16 +03:00
package hystrix
import (
"context"
2016-03-15 21:38:16 +03:00
"testing"
2016-03-15 21:45:59 +03:00
"github.com/afex/hystrix-go/hystrix"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/registry/memory"
"github.com/micro/go-micro/v2/router"
rrouter "github.com/micro/go-micro/v2/router/registry"
2016-03-15 21:38:16 +03:00
)
func TestBreaker(t *testing.T) {
// setup
registry := memory.NewRegistry()
2016-03-15 21:38:16 +03:00
c := client.NewClient(
// set the selector
client.Router(rrouter.NewRouter(router.Registry(registry))),
2016-03-15 21:38:16 +03:00
// 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
}
}