Repository: grpc/grpc-go
File: rpc_util_test.go (Go)

Code before:
			input:                 []byte("small message that is too large"),
			maxReceiveMessageSize: 5,
			want:                  nil,
			wantErr:               errMaxMessageSizeExceeded,
		},
		{
			name:                  "Decompresses to exactly maxReceiveMessageSize",
			compressor:            c,
			input:                 []byte("exact size message"),
			maxReceiveMessageSize: len("exact size message"),
			want:                  []byte("exact size message"),
			wantErr:               nil,
		},
		{
			name:                  "Handles maxReceiveMessageSize as MaxInt",
			compressor:            c,
			input:                 []byte("small message"),
			maxReceiveMessageSize: math.MaxInt,
			want:                  []byte("small message"),
			wantErr:               nil,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			compressedMsg := compressInput(tt.input)
			output, err := decompress(tt.compressor, compressedMsg, tt.maxReceiveMessageSize, mem.DefaultBufferPool())

			if !cmp.Equal(err, tt.wantErr, cmpopts.EquateErrors()) {
				t.Fatalf("decompress() error = %v, wantErr = %v", err, tt.wantErr)
			}

			if diff := cmp.Diff(tt.want, output.Materialize()); diff != "" {
				t.Fatalf("decompress() mismatch (-want +got):\n%s", diff)
			}
		})
	}
}


Review comment (suggestion):
We need to pass `t` as a param to `compressInput` so that it can call `t.Fatalf`. Right now, `compressInput` is passing a `nil` pointer to `compressData` which would cause a panic.

Code after:
func (s) TestDecompress(t *testing.T) {
	compressor := encoding.GetCompressor("gzip")
	validDecompressor := &MockDecompressor{ShouldError: false}
	invalidFormatDecompressor := &MockDecompressor{ShouldError: true}

	testCases := []struct {
		name                  string
		input                 mem.BufferSlice
		dc                    Decompressor
		maxReceiveMessageSize int
		want                  []byte
		wantErr               error
	}{
		{
			name:                  "Decompresses successfully with sufficient buffer size",
			input:                 compressWithDeterministicError(t, []byte("decompressed data")),
			dc:                    nil,
			maxReceiveMessageSize: 50,
			want:                  []byte("decompressed data"),
			wantErr:               nil,
		},
		{
			name:                  "Fails due to exceeding maxReceiveMessageSize",
			input:                 compressWithDeterministicError(t, []byte("message that is too large")),
			dc:                    nil,
			maxReceiveMessageSize: len("message that is too large") - 1,
			want:                  nil,
			wantErr:               status.Errorf(codes.ResourceExhausted, "grpc: received message after decompression larger than max %d", len("message that is too large")-1),
		},
		{
			name:                  "Decompresses to exactly maxReceiveMessageSize",
			input:                 compressWithDeterministicError(t, []byte("exact size message")),
			dc:                    nil,
			maxReceiveMessageSize: len("exact size message"),
			want:                  []byte("exact size message"),
			wantErr:               nil,
		},
		{
			name:                  "Decompresses successfully with maxReceiveMessageSize MaxInt",
			input:                 compressWithDeterministicError(t, []byte("large message")),
			dc:                    nil,
			maxReceiveMessageSize: math.MaxInt,
			want:                  []byte("large message"),
			wantErr:               nil,
		},
		{
			name:                  "Fails with decompression error due to invalid format",
			input:                 compressWithDeterministicError(t, []byte("invalid compressed data")),
			dc:                    invalidFormatDecompressor,
			maxReceiveMessageSize: 50,
			want:                  nil,