Skip to content

Commit

Permalink
Increased test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Lobo committed Jan 5, 2024
1 parent e9d8e91 commit b6e92d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
25 changes: 23 additions & 2 deletions ecosystem/grpc/client_interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ type PingService struct {
func (s *PingService) Ping(_ context.Context, _ *ping.PingRequest) (*ping.PingResponse, error) {
// Your implementation of the Ping method goes here
fmt.Println("Received Ping request")
return nil, s.err
if s.err != nil {
return nil, s.err
}
return &ping.PingResponse{}, nil
}

func setupServerAndClient(port int) (*PingService, ping.PingServiceClient) {
Expand All @@ -42,6 +45,10 @@ func setupServerAndClient(port int) (*PingService, ping.PingServiceClient) {
}
}()

defaultInverseMapping := DefaultInverseMapping()
defaultInverseMapping[codes.DataLoss] = simplerr.CodeResourceExhausted
GetDefaultRegistry().SetInverseMapping(defaultInverseMapping)

interceptor := ReturnSimpleErrors(nil)

conn, err := grpc.Dial(fmt.Sprintf(":%d", port),
Expand All @@ -58,7 +65,7 @@ func setupServerAndClient(port int) (*PingService, ping.PingServiceClient) {

func TestClientInterceptor(t *testing.T) {

_, client := setupServerAndClient(50051)
server, client := setupServerAndClient(50051)
_, err := client.Ping(context.Background(), &ping.PingRequest{})

require.True(t, simplerr.HasErrorCode(err, simplerr.CodeNotFound), "simplerror code can be detected")
Expand All @@ -71,6 +78,12 @@ func TestClientInterceptor(t *testing.T) {
method, ok := simplerr.GetAttribute(err, AttrGRPCMethod)
require.True(t, ok)
require.Equal(t, "/ping.PingService/Ping", method, "can get the grpc method which errored")

// Test the custom added mapping
server.err = status.Error(codes.DataLoss, "test error")
_, err = client.Ping(context.Background(), &ping.PingRequest{})
require.True(t, simplerr.HasErrorCode(err, simplerr.CodeResourceExhausted), "simplerror code can be detected")

}

// When a non grpc error is returned, the client still returns a grpc error with code Unknown
Expand All @@ -94,3 +107,11 @@ func TestClientInterceptorNotGPRCError(t *testing.T) {
require.Equal(t, "/ping.PingService/Ping", method, "can get the grpc method which errored")

}

func TestClientInterceptorNoError(t *testing.T) {
server, client := setupServerAndClient(50053)
server.err = nil

_, err := client.Ping(context.Background(), &ping.PingRequest{})
require.Nil(t, err)
}
20 changes: 20 additions & 0 deletions ecosystem/grpc/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package simplegrpc

import (
"github.com/lobocv/simplerr"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"testing"
)

func TestGetGPRCCode(t *testing.T) {

reg := NewRegistry()
simplerrCode, found := reg.getGRPCCode(codes.NotFound)
require.True(t, found)
require.Equal(t, simplerr.CodeNotFound, simplerrCode)

simplerrCode, found = reg.getGRPCCode(codes.Code(100000))
require.False(t, found)
require.Equal(t, simplerr.CodeUnknown, simplerrCode)
}

0 comments on commit b6e92d2

Please sign in to comment.