@@ -5,10 +5,12 @@ package testpb
5
5
6
6
import (
7
7
"context"
8
+ "errors"
8
9
"fmt"
9
10
"net"
10
11
"testing"
11
12
13
+ "github.com/stretchr/testify/assert"
12
14
"github.com/stretchr/testify/require"
13
15
"google.golang.org/grpc"
14
16
"google.golang.org/grpc/codes"
@@ -89,3 +91,41 @@ func TestPingServiceOnWire(t *testing.T) {
89
91
default :
90
92
}
91
93
}
94
+
95
+ func TestTestServicePing_PingError (t * testing.T ) {
96
+ testCases := map [string ]struct {
97
+ request * PingErrorRequest
98
+ err error
99
+ unwrapped error
100
+ msg string
101
+ }{
102
+ "NotFound" : {
103
+ request : & PingErrorRequest {ErrorCodeReturned : uint32 (codes .NotFound ), Value : "not found" },
104
+ err : & wrappedErrFields {wrappedErr : status .Error (codes .NotFound , "Userspace error" ), fields : []any {"error-field" , "plop" }},
105
+ unwrapped : status .Error (codes .NotFound , "Userspace error" ),
106
+ msg : "rpc error: code = NotFound desc = Userspace error" ,
107
+ },
108
+ "OK" : {
109
+ request : & PingErrorRequest {ErrorCodeReturned : uint32 (codes .OK ), Value : "ok" },
110
+ err : & wrappedErrFields {wrappedErr : nil , fields : []any {"error-field" , "plop" }},
111
+ unwrapped : nil ,
112
+ msg : "" ,
113
+ },
114
+ }
115
+
116
+ for name , testCase := range testCases {
117
+ t .Run (name , func (t * testing.T ) {
118
+ svc := & TestPingService {}
119
+
120
+ _ , err := svc .PingError (context .Background (), testCase .request )
121
+ require .Equal (t , testCase .err , err )
122
+
123
+ var we * wrappedErrFields
124
+ ok := errors .As (err , & we )
125
+ require .True (t , ok )
126
+
127
+ assert .Equal (t , testCase .unwrapped , we .Unwrap ())
128
+ assert .Equal (t , testCase .msg , we .Error ())
129
+ })
130
+ }
131
+ }
0 commit comments