From 8c5cabe017870f7ccef9a6e3e9f5071f8ed96fc1 Mon Sep 17 00:00:00 2001 From: billprovince Date: Thu, 4 May 2023 10:09:52 -0700 Subject: [PATCH 1/2] fix(PageBufferReader): PageBufferReader should conform to Reader interface --- y/y.go | 2 +- y/y_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/y/y.go b/y/y.go index 7821ee8a6..83da2c1d5 100644 --- a/y/y.go +++ b/y/y.go @@ -508,7 +508,7 @@ func (r *PageBufferReader) Read(p []byte) (int, error) { } } - if read == 0 { + if read == 0 && len(p) > 0 { return read, io.EOF } diff --git a/y/y_test.go b/y/y_test.go index f3b80c1d4..07e0c938d 100644 --- a/y/y_test.go +++ b/y/y_test.go @@ -259,6 +259,28 @@ func TestPagebufferReader4(t *testing.T) { require.Equal(t, n, 0) } +// Test when reading into 0 length readBuffer +func TestPagebufferReader5(t *testing.T) { + b := NewPageBuffer(32) + readerWhenEmptyPageBuffer := b.NewReaderAt(0) + + readBuffer := []byte{} // Intentionally empty readBuffer. + n, err := rewaderWhenEmptyPageBuffer.Read(readBuffer) + require.NoError(t, err, "reading into empty buffer should return no error") + require.Equal(t, 0, n, "read into empty buffer should return 0 bytes") + + var wb [20]byte + rand.Read(wb[:]) + n, err = b.Write(wb[:]) + require.Equal(t, n, len(wb), "length of buffer and length written should be equal") + require.NoError(t, err, "unable to write bytes to buffer") + + readerWhenNonEmptyPageBuffer := b.NewReaderAt(0) + n, err = readerWhenNonEmptyPageBuffer.Read(readBuffer) + require.NoError(t, err, "reading into empty buffer should return no error") + require.Equal(t, 0, n, "read into empty buffer should return 0 bytes") +} + func TestSizeVarintForZero(t *testing.T) { siz := sizeVarint(0) require.Equal(t, 1, siz) From 98a3f36fc74b64e88ca2a76a62decab5b738b924 Mon Sep 17 00:00:00 2001 From: billprovince Date: Thu, 4 May 2023 10:17:09 -0700 Subject: [PATCH 2/2] Fix typo in test. --- y/y_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/y/y_test.go b/y/y_test.go index 07e0c938d..9948e2285 100644 --- a/y/y_test.go +++ b/y/y_test.go @@ -265,7 +265,7 @@ func TestPagebufferReader5(t *testing.T) { readerWhenEmptyPageBuffer := b.NewReaderAt(0) readBuffer := []byte{} // Intentionally empty readBuffer. - n, err := rewaderWhenEmptyPageBuffer.Read(readBuffer) + n, err := readerWhenEmptyPageBuffer.Read(readBuffer) require.NoError(t, err, "reading into empty buffer should return no error") require.Equal(t, 0, n, "read into empty buffer should return 0 bytes")