Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Parraga <[email protected]>
  • Loading branch information
Sovietaced committed Jul 30, 2024
1 parent 9a1d7a8 commit ec7ba89
Showing 1 changed file with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"

mockScope "github.com/flyteorg/flyte/flytestdlib/promutils"
Expand All @@ -17,24 +18,73 @@ func TestRecoveryInterceptor(t *testing.T) {
testScope := mockScope.NewTestScope()
recoveryInterceptor := NewRecoveryInterceptor(testScope)
unaryInterceptor := recoveryInterceptor.UnaryServerInterceptor()
info := &grpc.UnaryServerInfo{}
streamInterceptor := recoveryInterceptor.StreamServerInterceptor()
unaryInfo := &grpc.UnaryServerInfo{}
streamInfo := &grpc.StreamServerInfo{}
req := "test-request"

t.Run("should recover from panic", func(t *testing.T) {
_, err := unaryInterceptor(ctx, req, info, func(ctx context.Context, req any) (any, error) {
t.Run("unary should recover from panic", func(t *testing.T) {
_, err := unaryInterceptor(ctx, req, unaryInfo, func(ctx context.Context, req any) (any, error) {
panic("synthetic")
})
expectedErr := status.Errorf(codes.Internal, "")
require.Error(t, err)
require.Equal(t, expectedErr, err)
})

t.Run("should plumb response without panic", func(t *testing.T) {
t.Run("stream should recover from panic", func(t *testing.T) {
stream := testStream{}
err := streamInterceptor(nil, &stream, streamInfo, func(srv any, stream grpc.ServerStream) error {
panic("synthetic")
})
expectedErr := status.Errorf(codes.Internal, "")
require.Error(t, err)
require.Equal(t, expectedErr, err)
})

t.Run("unary should plumb response without panic", func(t *testing.T) {
mockedResponse := "test"
resp, err := unaryInterceptor(ctx, req, info, func(ctx context.Context, req any) (any, error) {
resp, err := unaryInterceptor(ctx, req, unaryInfo, func(ctx context.Context, req any) (any, error) {
return mockedResponse, nil
})
require.NoError(t, err)
require.Equal(t, mockedResponse, resp)
})

t.Run("stream should plumb response without panic", func(t *testing.T) {
stream := testStream{}
handlerCalled := false
err := streamInterceptor(nil, &stream, streamInfo, func(srv any, stream grpc.ServerStream) error {
handlerCalled = true
return nil
})
require.NoError(t, err)
require.True(t, handlerCalled)
})
}

// testStream is an implementation of grpc.ServerStream for testing.
type testStream struct {
}

func (s *testStream) SendMsg(m interface{}) error {
return nil
}

func (s *testStream) RecvMsg(m interface{}) error {
return nil
}

func (s *testStream) SetHeader(metadata.MD) error {
return nil
}

func (s *testStream) SendHeader(metadata.MD) error {
return nil
}

func (s *testStream) SetTrailer(metadata.MD) {}

func (s *testStream) Context() context.Context {
return context.Background()
}

0 comments on commit ec7ba89

Please sign in to comment.