Skip to content

Commit

Permalink
test(store): add ut of wal.record.Pack() (vanus-labs#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifplusor authored Jun 13, 2022
1 parent 2fdbbca commit 7d09ca9
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions internal/store/wal/record/packing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,76 @@
// limitations under the License.

package record

import (
// standard libraries.
"bytes"
"testing"

// third-party libraries.
. "github.com/smartystreets/goconvey/convey"
)

const (
blockSize = 4 * 1024
)

func TestPack(t *testing.T) {
Convey("pack with enough space in first block", t, func() {
records := Pack(rawData, blockSize, blockSize)
So(len(records), ShouldEqual, 1)
r0 := &records[0]
So(r0.Type, ShouldEqual, Full)
So(r0.Length, ShouldEqual, len(rawData))
So(bytes.Equal(r0.Data, rawData), ShouldEqual, true)
})

Convey("pack with not enough space in first block", t, func() {
records := Pack(rawData, HeaderSize+1, blockSize)
So(len(records), ShouldEqual, 2)
r0 := &records[0]
So(r0.Type, ShouldEqual, First)
So(r0.Length, ShouldEqual, 1)
So(bytes.Equal(r0.Data, rawData[:1]), ShouldEqual, true)
r1 := &records[1]
So(r1.Type, ShouldEqual, Last)
So(r1.Length, ShouldEqual, len(rawData)-1)
So(bytes.Equal(r1.Data, rawData[1:]), ShouldEqual, true)
})

Convey("pack with no space in first block", t, func() {
records := Pack(rawData, HeaderSize, blockSize)
So(len(records), ShouldEqual, 2)
r0 := &records[0]
So(r0.Type, ShouldEqual, First)
So(r0.Length, ShouldEqual, 0)
So(bytes.Equal(r0.Data, []byte{}), ShouldEqual, true)
r1 := &records[1]
So(r1.Type, ShouldEqual, Last)
So(r1.Length, ShouldEqual, len(rawData))
So(bytes.Equal(r1.Data, rawData), ShouldEqual, true)
})

Convey("pack big data", t, func() {
str := ""
for i := 0; i < 2*blockSize; i++ {
str += string("a"[0] + byte(i%26))
}
bigData := []byte(str)

records := Pack(bigData, blockSize, blockSize)
So(len(records), ShouldEqual, 3)
r0 := &records[0]
So(r0.Type, ShouldEqual, First)
So(r0.Length, ShouldEqual, blockSize-HeaderSize)
So(bytes.Equal(r0.Data, bigData[:r0.Length]), ShouldEqual, true)
r1 := &records[1]
So(r1.Type, ShouldEqual, Middle)
So(r1.Length, ShouldEqual, blockSize-HeaderSize)
So(bytes.Equal(r1.Data, bigData[r0.Length:r0.Length+r1.Length]), ShouldEqual, true)
r2 := &records[2]
So(r2.Type, ShouldEqual, Last)
So(r2.Length, ShouldEqual, 2*HeaderSize)
So(bytes.Equal(r2.Data, bigData[2*(blockSize-HeaderSize):]), ShouldEqual, true)
})
}

0 comments on commit 7d09ca9

Please sign in to comment.