Skip to content

Commit 760c2ca

Browse files
lobocvCalvin Lobo
andauthored
Removed Ping grpc server. Mock the invoker layer now. This reduces dependencies. (#17)
Upgrade golangci-lint to v2 Fixed lint issue to make it pass again Co-authored-by: Calvin Lobo <[email protected]>
1 parent 7fd070c commit 760c2ca

File tree

8 files changed

+88
-362
lines changed

8 files changed

+88
-362
lines changed

.github/workflows/golangci-lint.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
name: golangci-lint
2-
3-
# Controls when the action will run. Triggers the workflow on push or pull requests
42
on:
53
push:
64
branches:
75
- main
86
- master
97
pull_request:
8+
109
permissions:
1110
contents: read
11+
1212
jobs:
1313
golangci:
1414
name: lint
@@ -19,6 +19,6 @@ jobs:
1919
with:
2020
go-version: stable
2121
- name: golangci-lint
22-
uses: golangci/golangci-lint-action@v6
22+
uses: golangci/golangci-lint-action@v8
2323
with:
24-
version: v1.63
24+
version: v2.1

.golangci.yaml

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,52 @@
1-
# options for analysis running
2-
run:
3-
# timeout for analysis, e.g. 30s, 5m, default is 1m
4-
timeout: 2m
5-
6-
issues:
7-
# Only report issues for changes since master
8-
new-from-rev: origin/master
9-
10-
# output configuration options
1+
version: "2"
112
output:
12-
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
133
formats:
14-
- format: colored-line-number
15-
16-
linters-settings:
17-
errcheck:
18-
# report about not checking of errors in type assertions: `a := b.(MyStruct)`;
19-
# default is false: such cases aren't reported by default.
20-
check-type-assertions: true
21-
22-
# Function length check
23-
funlen:
24-
lines: 60
25-
statements: 40
26-
27-
# Report deeply nested if statements
28-
nestif:
29-
# minimal complexity of if statements to report, 5 by default
30-
min-complexity: 4
31-
32-
gofmt:
33-
# simplify code: gofmt with `-s` option, true by default
34-
simplify: true
35-
36-
govet:
37-
# report about shadowed variables
38-
enable-all: true
39-
disable:
40-
# Do not check field memory alignment because in most cases the performance gain is not worth the headache
41-
- fieldalignment
42-
4+
text:
5+
path: stdout
436
linters:
44-
# Disable the default linters so we can explicitly name the linters we want
45-
disable-all: true
46-
47-
# List of enabled linters
7+
default: none
488
enable:
49-
#####################
50-
# Default linters
51-
#####################
52-
- gofmt
53-
# Checks error handling
549
- errcheck
55-
# Linter for Go source code that specializes in simplifying a code
56-
- gosimple
57-
# Vet examines Go source code and reports suspicious constructs, such as Printf calls whose
58-
# arguments do not align with the format string
5910
- govet
60-
# Detects when assignments to existing variables are not used
6111
- ineffassign
62-
# Static code analytics
6312
- staticcheck
64-
# Reports unused function parameters.
13+
- unconvert
6514
- unparam
66-
# Check if variables or functions are unused
6715
- unused
68-
# Remove unnecessary type conversions.
69-
- unconvert
16+
settings:
17+
errcheck:
18+
check-type-assertions: true
19+
funlen:
20+
lines: 60
21+
statements: 40
22+
govet:
23+
disable:
24+
- fieldalignment
25+
enable-all: true
26+
nestif:
27+
min-complexity: 4
28+
exclusions:
29+
generated: lax
30+
presets:
31+
- comments
32+
- common-false-positives
33+
- legacy
34+
- std-error-handling
35+
paths:
36+
- third_party$
37+
- builtin$
38+
- examples$
39+
issues:
40+
new-from-rev: origin/master
41+
formatters:
42+
enable:
43+
- gofmt
44+
settings:
45+
gofmt:
46+
simplify: true
47+
exclusions:
48+
generated: lax
49+
paths:
50+
- third_party$
51+
- builtin$
52+
- examples$

ecosystem/grpc/client_interceptor.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ func ReturnSimpleErrors(registry *Registry) grpc.UnaryClientInterceptor {
3434
}
3535

3636
grpcCode := codes.Unknown
37-
msg := err.Error()
3837

39-
serr := simplerr.New(msg).Attr(AttrGRPCMethod, method) // nolint: govet
38+
serr := simplerr.Wrap(err).Attr(AttrGRPCMethod, method) // nolint: govet
4039

4140
// Check if the error is a gRPC status error
4241
// The GRPC framework seems to always return grpc errors on the client side, even if the server does not

ecosystem/grpc/client_interceptor_test.go

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,35 @@ import (
44
"context"
55
"fmt"
66
"github.com/lobocv/simplerr"
7-
"github.com/lobocv/simplerr/ecosystem/grpc/internal/ping"
87
"github.com/stretchr/testify/require"
98
"google.golang.org/grpc"
109
"google.golang.org/grpc/codes"
11-
"google.golang.org/grpc/credentials/insecure"
1210
"google.golang.org/grpc/status"
13-
"net"
1411
"testing"
1512
)
1613

17-
type PingService struct {
18-
err error
19-
}
20-
21-
func (s *PingService) Ping(_ context.Context, _ *ping.PingRequest) (*ping.PingResponse, error) {
22-
// Your implementation of the Ping method goes here
23-
fmt.Println("Received Ping request")
24-
if s.err != nil {
25-
return nil, s.err
14+
var mockInvoker = func(grpcError error) grpc.UnaryInvoker {
15+
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
16+
return grpcError
2617
}
27-
return &ping.PingResponse{}, nil
2818
}
2919

30-
func setupServerAndClient(port int) (*PingService, ping.PingServiceClient) {
31-
32-
server := grpc.NewServer()
33-
service := &PingService{err: status.Error(codes.NotFound, "test error")}
34-
ping.RegisterPingServiceServer(server, service)
35-
36-
// Create a listener on TCP port 50051
37-
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
38-
if err != nil {
39-
panic(fmt.Sprintf("Error creating listener: %v", err))
40-
}
41-
42-
go func() {
43-
if err = server.Serve(listener); err != nil {
44-
panic(fmt.Sprintf("Error serving: %v", err))
45-
}
46-
}()
20+
func makeMockGrpcCall(returnedError error) func() error {
4721

4822
defaultInverseMapping := DefaultInverseMapping()
4923
defaultInverseMapping[codes.DataLoss] = simplerr.CodeResourceExhausted
5024
GetDefaultRegistry().SetInverseMapping(defaultInverseMapping)
5125

5226
interceptor := ReturnSimpleErrors(nil)
5327

54-
conn, err := grpc.NewClient(fmt.Sprintf(":%d", port),
55-
grpc.WithUnaryInterceptor(interceptor),
56-
grpc.WithTransportCredentials(insecure.NewCredentials()),
57-
)
58-
if err != nil {
59-
panic(err)
28+
return func() error {
29+
return interceptor(context.Background(), "/ping.PingService/Ping", nil, nil, nil, mockInvoker(returnedError))
6030
}
61-
client := ping.NewPingServiceClient(conn)
62-
63-
return service, client
6431
}
6532

6633
func TestClientInterceptor(t *testing.T) {
6734

68-
server, client := setupServerAndClient(50051)
69-
_, err := client.Ping(context.Background(), &ping.PingRequest{})
35+
err := makeMockGrpcCall(status.Error(codes.NotFound, "not found"))()
7036

7137
require.True(t, simplerr.HasErrorCode(err, simplerr.CodeNotFound), "simplerror code can be detected")
7238
require.Equal(t, codes.NotFound, status.Code(err), "grpc code can be detected with grpc status package")
@@ -80,8 +46,7 @@ func TestClientInterceptor(t *testing.T) {
8046
require.Equal(t, "/ping.PingService/Ping", method, "can get the grpc method which errored")
8147

8248
// Test the custom added mapping
83-
server.err = status.Error(codes.DataLoss, "test error")
84-
_, err = client.Ping(context.Background(), &ping.PingRequest{})
49+
err = makeMockGrpcCall(status.Error(codes.DataLoss, "data loss"))()
8550
require.True(t, simplerr.HasErrorCode(err, simplerr.CodeResourceExhausted), "simplerror code can be detected")
8651

8752
}
@@ -90,28 +55,18 @@ func TestClientInterceptor(t *testing.T) {
9055
// Our interceptor should still be able to detect attributes on the error
9156
func TestClientInterceptorNotGPRCError(t *testing.T) {
9257

93-
server, client := setupServerAndClient(50052)
94-
server.err = fmt.Errorf("not a grpc error")
95-
96-
_, err := client.Ping(context.Background(), &ping.PingRequest{})
58+
err := makeMockGrpcCall(fmt.Errorf("some error"))()
9759

9860
require.True(t, simplerr.HasErrorCode(err, simplerr.CodeUnknown), "simplerror code can be detected")
9961
require.Equal(t, codes.Unknown, status.Code(err), "grpc code can be detected with grpc status package")
10062

101-
st, ok := simplerr.GetAttribute(err, AttrGRPCStatus)
102-
require.True(t, ok)
103-
require.Equal(t, codes.Unknown, st.(*status.Status).Code(), "can get the grpc Status") // nolint: errcheck
104-
10563
method, ok := simplerr.GetAttribute(err, AttrGRPCMethod)
10664
require.True(t, ok)
10765
require.Equal(t, "/ping.PingService/Ping", method, "can get the grpc method which errored")
10866

10967
}
11068

11169
func TestClientInterceptorNoError(t *testing.T) {
112-
server, client := setupServerAndClient(50053)
113-
server.err = nil
114-
115-
_, err := client.Ping(context.Background(), &ping.PingRequest{})
70+
err := makeMockGrpcCall(nil)()
11671
require.Nil(t, err)
11772
}

0 commit comments

Comments
 (0)