Skip to content

Commit

Permalink
Fix chainUnaryInterceptors to allow retry
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminc committed Sep 23, 2022
1 parent a238ceb commit 1d05928
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions server.go
Expand Up @@ -1152,6 +1152,7 @@ func chainUnaryInterceptors(interceptors []UnaryServerInterceptor) UnaryServerIn
return interceptors[state.i](ctx, req, info, handler)
}
state.i++
defer func() { state.i-- }()
return interceptors[state.i-1](ctx, req, info, state.next)
}
return state.next(ctx, req)
Expand Down
28 changes: 28 additions & 0 deletions server_test.go
Expand Up @@ -130,6 +130,34 @@ func (s) TestGetServiceInfo(t *testing.T) {
}
}

func (s) TestRetryChainedInterceptor(t *testing.T) {
var records []int
i1 := func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error) {
records = append(records, 1)
// call handler twice to simulate a retry here.
handler(ctx, req)
return handler(ctx, req)
}
i2 := func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error) {
records = append(records, 2)
return handler(ctx, req)
}
i3 := func(ctx context.Context, req interface{}, info *UnaryServerInfo, handler UnaryHandler) (resp interface{}, err error) {
records = append(records, 3)
return handler(ctx, req)
}

ii := chainUnaryInterceptors([]UnaryServerInterceptor{i1, i2, i3})

handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, nil
}
ii(context.Background(), nil, nil, handler)
if !reflect.DeepEqual(records, []int{1, 2, 3, 2, 3}) {
t.Fatalf("retry failed on chained interceptors: %v", records)
}
}

func (s) TestStreamContext(t *testing.T) {
expectedStream := &transport.Stream{}
ctx := NewContextWithServerTransportStream(context.Background(), expectedStream)
Expand Down

0 comments on commit 1d05928

Please sign in to comment.