@@ -36,6 +36,7 @@ import (
36
36
"google.golang.org/grpc/internal/grpctest"
37
37
"google.golang.org/grpc/internal/grpcutil"
38
38
"google.golang.org/grpc/internal/stubserver"
39
+ "google.golang.org/grpc/mem"
39
40
"google.golang.org/grpc/metadata"
40
41
"google.golang.org/grpc/status"
41
42
@@ -90,18 +91,18 @@ type errProtoCodec struct {
90
91
decodingErr error
91
92
}
92
93
93
- func (c * errProtoCodec ) Marshal (v any ) ([] byte , error ) {
94
+ func (c * errProtoCodec ) Marshal (v any ) (mem. BufferSlice , error ) {
94
95
if c .encodingErr != nil {
95
96
return nil , c .encodingErr
96
97
}
97
- return encoding .GetCodec (proto .Name ).Marshal (v )
98
+ return encoding .GetCodecV2 (proto .Name ).Marshal (v )
98
99
}
99
100
100
- func (c * errProtoCodec ) Unmarshal (data [] byte , v any ) error {
101
+ func (c * errProtoCodec ) Unmarshal (data mem. BufferSlice , v any ) error {
101
102
if c .decodingErr != nil {
102
103
return c .decodingErr
103
104
}
104
- return encoding .GetCodec (proto .Name ).Unmarshal (data , v )
105
+ return encoding .GetCodecV2 (proto .Name ).Unmarshal (data , v )
105
106
}
106
107
107
108
func (c * errProtoCodec ) Name () string {
@@ -118,7 +119,7 @@ func (s) TestEncodeDoesntPanicOnServer(t *testing.T) {
118
119
ec := & errProtoCodec {name : t .Name (), encodingErr : encodingErr }
119
120
120
121
// Start a server with the above codec.
121
- backend := stubserver .StartTestService (t , nil , grpc .ForceServerCodec (ec ))
122
+ backend := stubserver .StartTestService (t , nil , grpc .ForceServerCodecV2 (ec ))
122
123
defer backend .Stop ()
123
124
124
125
// Create a channel to the above server.
@@ -154,7 +155,7 @@ func (s) TestDecodeDoesntPanicOnServer(t *testing.T) {
154
155
ec := & errProtoCodec {name : t .Name (), decodingErr : decodingErr }
155
156
156
157
// Start a server with the above codec.
157
- backend := stubserver .StartTestService (t , nil , grpc .ForceServerCodec (ec ))
158
+ backend := stubserver .StartTestService (t , nil , grpc .ForceServerCodecV2 (ec ))
158
159
defer backend .Stop ()
159
160
160
161
// Create a channel to the above server. Since we do not specify any codec
@@ -206,15 +207,15 @@ func (s) TestEncodeDoesntPanicOnClient(t *testing.T) {
206
207
ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
207
208
defer cancel ()
208
209
client := testgrpc .NewTestServiceClient (cc )
209
- _ , err = client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (ec ))
210
+ _ , err = client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodecV2 (ec ))
210
211
if err == nil || ! strings .Contains (err .Error (), encodingErr .Error ()) {
211
212
t .Fatalf ("RPC failed with error: %v, want: %v" , err , encodingErr )
212
213
}
213
214
214
215
// Configure the codec on the client to not return errors anymore and expect
215
216
// the RPC to succeed.
216
217
ec .encodingErr = nil
217
- if _ , err := client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (ec )); err != nil {
218
+ if _ , err := client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodecV2 (ec )); err != nil {
218
219
t .Fatalf ("RPC failed with error: %v" , err )
219
220
}
220
221
}
@@ -242,15 +243,15 @@ func (s) TestDecodeDoesntPanicOnClient(t *testing.T) {
242
243
ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
243
244
defer cancel ()
244
245
client := testgrpc .NewTestServiceClient (cc )
245
- _ , err = client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (ec ))
246
+ _ , err = client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodecV2 (ec ))
246
247
if err == nil || ! strings .Contains (err .Error (), decodingErr .Error ()) {
247
248
t .Fatalf ("RPC failed with error: %v, want: %v" , err , decodingErr )
248
249
}
249
250
250
251
// Configure the codec on the client to not return errors anymore and expect
251
252
// the RPC to succeed.
252
253
ec .decodingErr = nil
253
- if _ , err := client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (ec )); err != nil {
254
+ if _ , err := client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodecV2 (ec )); err != nil {
254
255
t .Fatalf ("RPC failed with error: %v" , err )
255
256
}
256
257
}
@@ -265,14 +266,14 @@ type countingProtoCodec struct {
265
266
unmarshalCount int32
266
267
}
267
268
268
- func (p * countingProtoCodec ) Marshal (v any ) ([] byte , error ) {
269
+ func (p * countingProtoCodec ) Marshal (v any ) (mem. BufferSlice , error ) {
269
270
atomic .AddInt32 (& p .marshalCount , 1 )
270
- return encoding .GetCodec (proto .Name ).Marshal (v )
271
+ return encoding .GetCodecV2 (proto .Name ).Marshal (v )
271
272
}
272
273
273
- func (p * countingProtoCodec ) Unmarshal (data [] byte , v any ) error {
274
+ func (p * countingProtoCodec ) Unmarshal (data mem. BufferSlice , v any ) error {
274
275
atomic .AddInt32 (& p .unmarshalCount , 1 )
275
- return encoding .GetCodec (proto .Name ).Unmarshal (data , v )
276
+ return encoding .GetCodecV2 (proto .Name ).Unmarshal (data , v )
276
277
}
277
278
278
279
func (p * countingProtoCodec ) Name () string {
@@ -284,7 +285,7 @@ func (p *countingProtoCodec) Name() string {
284
285
func (s ) TestForceServerCodec (t * testing.T ) {
285
286
// Create an server with the counting proto codec.
286
287
codec := & countingProtoCodec {name : t .Name ()}
287
- backend := stubserver .StartTestService (t , nil , grpc .ForceServerCodec (codec ))
288
+ backend := stubserver .StartTestService (t , nil , grpc .ForceServerCodecV2 (codec ))
288
289
defer backend .Stop ()
289
290
290
291
// Create a channel to the above server.
@@ -317,7 +318,7 @@ func (s) TestForceServerCodec(t *testing.T) {
317
318
318
319
// renameProtoCodec wraps the proto codec and allows customizing the Name().
319
320
type renameProtoCodec struct {
320
- encoding.Codec
321
+ encoding.CodecV2
321
322
name string
322
323
}
323
324
@@ -356,9 +357,9 @@ func (s) TestForceCodecName(t *testing.T) {
356
357
357
358
// Force the use of the custom codec on the client with the ForceCodec call
358
359
// option. Confirm the name is converted to lowercase before transmitting.
359
- codec := & renameProtoCodec {Codec : encoding .GetCodec (proto .Name ), name : t .Name ()}
360
+ codec := & renameProtoCodec {CodecV2 : encoding .GetCodecV2 (proto .Name ), name : t .Name ()}
360
361
wantContentTypeCh <- []string {fmt .Sprintf ("application/grpc+%s" , strings .ToLower (t .Name ()))}
361
- if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (codec )); err != nil {
362
+ if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodecV2 (codec )); err != nil {
362
363
t .Fatalf ("ss.Client.EmptyCall(_, _) = _, %v; want _, nil" , err )
363
364
}
364
365
}
0 commit comments