Skip to content

Commit

Permalink
correctly propagate the hystrix error (#165)
Browse files Browse the repository at this point in the history
* correctly propagate the hystrix error

* fix

* add timeout error unit tests

* remove errors.Is(), not supported by go1.12

* fix data race
  • Loading branch information
stevenCarousell authored Sep 1, 2021
1 parent 92b31a1 commit ec021a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 24 additions & 0 deletions interceptors/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"strings"
"testing"
"time"

"google.golang.org/grpc/status"

"google.golang.org/grpc/codes"

"github.com/afex/hystrix-go/hystrix"
"github.com/carousell/Orion/utils/errors"
"google.golang.org/grpc"
)
Expand All @@ -33,6 +35,28 @@ func TestHystrixClientInterceptorPanicRecovery(t *testing.T) {
}
}

func TestHystrixClientInterceptorTimeoutError(t *testing.T) {

// default hystrix timeout is 1000 ms
invoker := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
time.Sleep(1100 * time.Millisecond)
return nil
}

err := HystrixClientInterceptor()(
context.Background(),
"method",
nil,
nil,
nil,
invoker,
)

if !strings.Contains(err.Error(), hystrix.ErrTimeout.Error()) {
t.Errorf("hystrixInterceptor doesn't propagate the hystrix timeout error properly, got:%v", err)
}
}

func TestHystrixClientInterceptorOptionsCanIgnore(t *testing.T) {
err1 := errors.New("hello world")
tests := []struct {
Expand Down
6 changes: 4 additions & 2 deletions interceptors/unary_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func HystrixClientInterceptor() grpc.UnaryClientInterceptor {
newCtx, cancel := context.WithCancel(ctx)
defer cancel()
var err error
_ = hystrix.Do(options.cmdName, func() (e error) {
herr := hystrix.Do(options.cmdName, func() (e error) {
defer func() {
if r := recover(); r != nil {
err = errors.Wrap(fmt.Errorf("panic inside hystrix Method: %s, req: %v, reply: %v", method, req, reply), "Hystrix")
Expand All @@ -157,7 +157,9 @@ func HystrixClientInterceptor() grpc.UnaryClientInterceptor {
return err
}
}, options.fallbackFunc)

if herr != nil {
return herr
}
return err
}
}
Expand Down

0 comments on commit ec021a7

Please sign in to comment.