@@ -4,69 +4,35 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"github.com/lobocv/simplerr"
7
- "github.com/lobocv/simplerr/ecosystem/grpc/internal/ping"
8
7
"github.com/stretchr/testify/require"
9
8
"google.golang.org/grpc"
10
9
"google.golang.org/grpc/codes"
11
- "google.golang.org/grpc/credentials/insecure"
12
10
"google.golang.org/grpc/status"
13
- "net"
14
11
"testing"
15
12
)
16
13
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
26
17
}
27
- return & ping.PingResponse {}, nil
28
18
}
29
19
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 {
47
21
48
22
defaultInverseMapping := DefaultInverseMapping ()
49
23
defaultInverseMapping [codes .DataLoss ] = simplerr .CodeResourceExhausted
50
24
GetDefaultRegistry ().SetInverseMapping (defaultInverseMapping )
51
25
52
26
interceptor := ReturnSimpleErrors (nil )
53
27
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 ))
60
30
}
61
- client := ping .NewPingServiceClient (conn )
62
-
63
- return service , client
64
31
}
65
32
66
33
func TestClientInterceptor (t * testing.T ) {
67
34
68
- server , client := setupServerAndClient (50051 )
69
- _ , err := client .Ping (context .Background (), & ping.PingRequest {})
35
+ err := makeMockGrpcCall (status .Error (codes .NotFound , "not found" ))()
70
36
71
37
require .True (t , simplerr .HasErrorCode (err , simplerr .CodeNotFound ), "simplerror code can be detected" )
72
38
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) {
80
46
require .Equal (t , "/ping.PingService/Ping" , method , "can get the grpc method which errored" )
81
47
82
48
// 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" ))()
85
50
require .True (t , simplerr .HasErrorCode (err , simplerr .CodeResourceExhausted ), "simplerror code can be detected" )
86
51
87
52
}
@@ -90,28 +55,18 @@ func TestClientInterceptor(t *testing.T) {
90
55
// Our interceptor should still be able to detect attributes on the error
91
56
func TestClientInterceptorNotGPRCError (t * testing.T ) {
92
57
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" ))()
97
59
98
60
require .True (t , simplerr .HasErrorCode (err , simplerr .CodeUnknown ), "simplerror code can be detected" )
99
61
require .Equal (t , codes .Unknown , status .Code (err ), "grpc code can be detected with grpc status package" )
100
62
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
-
105
63
method , ok := simplerr .GetAttribute (err , AttrGRPCMethod )
106
64
require .True (t , ok )
107
65
require .Equal (t , "/ping.PingService/Ping" , method , "can get the grpc method which errored" )
108
66
109
67
}
110
68
111
69
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 )()
116
71
require .Nil (t , err )
117
72
}
0 commit comments