micro-wrapper-breaker-sony/gobreaker_test.go
Vasiliy Tolstov 0ad2f5886d fix import path for v2 release (#457)
* fix import path for v2 release

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-10-02 13:04:21 +03:00

84 lines
1.8 KiB
Go

package gobreaker
import (
"context"
"testing"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/selector"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/registry/memory"
"github.com/sony/gobreaker"
)
func TestBreaker(t *testing.T) {
// setup
r := memory.NewRegistry()
s := selector.NewSelector(selector.Registry(r))
c := client.NewClient(
// set the selector
client.Selector(s),
// 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 < 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()
s := selector.NewSelector(selector.Registry(r))
c := client.NewClient(
// set the selector
client.Selector(s),
// add the breaker wrapper
client.Wrap(NewCustomClientWrapper(
gobreaker.Settings{},
BreakService,
)),
)
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)
}
}