@@ -21,13 +21,13 @@ package grpc
21
21
import (
22
22
"bytes"
23
23
"compress/gzip"
24
- "errors"
25
24
"io"
26
25
"math"
27
26
"reflect"
28
27
"testing"
29
28
30
29
"github.com/google/go-cmp/cmp"
30
+ "github.com/google/go-cmp/cmp/cmpopts"
31
31
"google.golang.org/grpc/codes"
32
32
"google.golang.org/grpc/encoding"
33
33
_ "google.golang.org/grpc/encoding/gzip"
@@ -300,27 +300,33 @@ func BenchmarkGZIPCompressor1MiB(b *testing.B) {
300
300
}
301
301
302
302
// compressData compresses data using gzip and returns the compressed bytes.
303
- func compressData (data []byte ) []byte {
303
+ // It now accepts *testing.T to handle errors during compression.
304
+ func compressData (t * testing.T , data []byte ) []byte {
304
305
var buf bytes.Buffer
305
306
gz := gzip .NewWriter (& buf )
306
- _ , _ = gz .Write (data )
307
- _ = gz .Close ()
307
+ if _ , err := gz .Write (data ); err != nil {
308
+ t .Fatalf ("compressData() failed to write data: %v" , err )
309
+ }
310
+
311
+ if err := gz .Close (); err != nil {
312
+ t .Fatalf ("compressData() failed to close gzip writer: %v" , err )
313
+ }
308
314
return buf .Bytes ()
309
315
}
310
316
317
+ // compressInput compresses input data and returns a BufferSlice.
318
+ func compressInput (input []byte ) mem.BufferSlice {
319
+ compressedData := compressData (nil , input )
320
+ return mem.BufferSlice {mem .NewBuffer (& compressedData , nil )}
321
+ }
322
+
311
323
// TestDecompress tests the decompress function with various scenarios, including
312
324
// successful decompression, error handling, and edge cases like overflow or
313
325
// premature data end. It ensures that the function behaves correctly with different
314
326
// inputs, buffer sizes, and error conditions, using the "gzip" compressor for testing.
315
-
316
327
func TestDecompress (t * testing.T ) {
317
328
c := encoding .GetCompressor ("gzip" )
318
329
319
- compressInput := func (input []byte ) mem.BufferSlice {
320
- compressedData := compressData (input )
321
- return mem.BufferSlice {mem .NewBuffer (& compressedData , nil )}
322
- }
323
-
324
330
tests := []struct {
325
331
name string
326
332
compressor encoding.Compressor
@@ -354,10 +360,10 @@ func TestDecompress(t *testing.T) {
354
360
wantErr : nil ,
355
361
},
356
362
{
357
- name : "Handles maxReceiveMessageSize as MaxInt64 " ,
363
+ name : "Handles maxReceiveMessageSize as MaxInt " ,
358
364
compressor : c ,
359
365
input : []byte ("small message" ),
360
- maxReceiveMessageSize : math .MaxInt64 ,
366
+ maxReceiveMessageSize : math .MaxInt ,
361
367
want : []byte ("small message" ),
362
368
wantErr : nil ,
363
369
},
@@ -368,15 +374,8 @@ func TestDecompress(t *testing.T) {
368
374
compressedMsg := compressInput (tt .input )
369
375
output , err := decompress (tt .compressor , compressedMsg , tt .maxReceiveMessageSize , mem .DefaultBufferPool ())
370
376
371
- if tt .wantErr != nil {
372
- if ! errors .Is (err , tt .wantErr ) {
373
- t .Fatalf ("decompress() error = %v, wantErr = %v" , err , tt .wantErr )
374
- }
375
- return
376
- }
377
-
378
- if err != nil {
379
- t .Fatalf ("decompress() unexpected error = %v" , err )
377
+ if ! cmp .Equal (err , tt .wantErr , cmpopts .EquateErrors ()) {
378
+ t .Fatalf ("decompress() error = %v, wantErr = %v" , err , tt .wantErr )
380
379
}
381
380
382
381
if diff := cmp .Diff (tt .want , output .Materialize ()); diff != "" {
0 commit comments