Skip to content

Commit

Permalink
Rename the test cases and refactor some of them
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothkumarr227 committed Dec 24, 2024
1 parent 6ff9321 commit 82124a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ func decompress(compressor encoding.Compressor, d mem.BufferSlice, maxReceiveMes
}

if doesReceiveMessageOverflow(out.Len(), maxReceiveMessageSize, dcReader) {
out.Free()
return nil, errMaxMessageSizeExceeded
}
return out, nil
Expand Down
22 changes: 11 additions & 11 deletions rpc_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ func BenchmarkGZIPCompressor1MiB(b *testing.B) {
bmCompressor(b, 1024*1024, NewGZIPCompressor())
}

// mustCompress compresses the input data and returns a BufferSlice.
func mustCompress(t *testing.T, input []byte) mem.BufferSlice {
// compressWithDeterministicError compresses the input data and returns a BufferSlice.
func compressWithDeterministicError(t *testing.T, input []byte) mem.BufferSlice {
t.Helper()
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
Expand All @@ -314,10 +314,10 @@ func mustCompress(t *testing.T, input []byte) mem.BufferSlice {
return mem.BufferSlice{mem.NewBuffer(&compressedData, nil)}
}

// TestDecompress tests the decompress function with various scenarios, including
// successful decompression, error handling, and edge cases like overflow or
// premature data end. It ensures that the function behaves correctly with different
// inputs, buffer sizes, and error conditions, using the "gzip" compressor for testing.
// TestDecompress tests the decompress function behaves correctly for following scenarios
// decompress successfully when message is <= maxReceiveMessageSize
// errors when message > maxReceiveMessageSize
// decompress successfully when maxReceiveMessageSize is MaxInt.
func (s) TestDecompress(t *testing.T) {
compressor := encoding.GetCompressor("gzip")

Expand All @@ -330,28 +330,28 @@ func (s) TestDecompress(t *testing.T) {
}{
{
name: "Decompresses successfully with sufficient buffer size",
input: mustCompress(t, []byte("decompressed data")),
input: compressWithDeterministicError(t, []byte("decompressed data")),
maxReceiveMessageSize: 50,
want: []byte("decompressed data"),
wantErr: nil,
},
{
name: "Fails due to exceeding maxReceiveMessageSize",
input: mustCompress(t, []byte("message that is too large")),
input: compressWithDeterministicError(t, []byte("message that is too large")),
maxReceiveMessageSize: len("message that is too large") - 1,
want: nil,
wantErr: errMaxMessageSizeExceeded,
},
{
name: "Decompresses to exactly maxReceiveMessageSize",
input: mustCompress(t, []byte("exact size message")),
input: compressWithDeterministicError(t, []byte("exact size message")),
maxReceiveMessageSize: len("exact size message"),
want: []byte("exact size message"),
wantErr: nil,
},
{
name: "Handles large buffer size MaxInt",
input: mustCompress(t, []byte("large message")),
name: "Decompresses successfully with maxReceiveMessageSize MaxInt",
input: compressWithDeterministicError(t, []byte("large message")),
maxReceiveMessageSize: math.MaxInt,
want: []byte("large message"),
wantErr: nil,
Expand Down

0 comments on commit 82124a4

Please sign in to comment.