recordio_internal_test.go 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
package recordio

import (
	"bytes"
	"testing"
	"unsafe"

	"github.com/stretchr/testify/assert"
)

func TestChunkHead(t *testing.T) {
	assert := assert.New(t)

	c := &Header{
		checkSum:       123,
		compressor:     456,
		compressedSize: 789,
	}

	var buf bytes.Buffer
	_, e := c.write(&buf)
	assert.Nil(e)

	cc, e := parseHeader(&buf)
	assert.Nil(e)
	assert.Equal(c, cc)
}

func TestWriteAndRead(t *testing.T) {
	assert := assert.New(t)

	data := []string{
		"12345",
		"1234",
		"12"}

	var buf bytes.Buffer
	w := NewWriter(&buf, 10, NoCompression) // use a small maxChunkSize.

	n, e := w.Write([]byte(data[0])) // not exceed chunk size.
	assert.Nil(e)
	assert.Equal(5, n)

	n, e = w.Write([]byte(data[1])) // not exceed chunk size.
	assert.Nil(e)
	assert.Equal(4, n)

	n, e = w.Write([]byte(data[2])) // exeeds chunk size, dump and create a new chunk.
	assert.Nil(e)
	assert.Equal(n, 2)

	assert.Nil(w.Close()) // flush the second chunk.
	assert.Nil(w.Writer)

	n, e = w.Write([]byte("anything")) // not effective after close.
	assert.NotNil(e)
	assert.Equal(n, 0)

	idx, e := LoadIndex(bytes.NewReader(buf.Bytes()))
	assert.Nil(e)
	assert.Equal([]uint32{2, 1}, idx.chunkLens)
	assert.Equal(
		[]int64{0,
			int64(4 + // magic number
				unsafe.Sizeof(Header{}) +
				5 + // first record
				4 + // second record
				2*4)}, // two record legnths
		idx.chunkOffsets)

H
Helin Wang 已提交
71
	s := NewRangeScanner(bytes.NewReader(buf.Bytes()), idx, -1, -1)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	i := 0
	for s.Scan() {
		assert.Equal(data[i], string(s.Record()))
		i++
	}
}

func TestWriteEmptyFile(t *testing.T) {
	assert := assert.New(t)

	var buf bytes.Buffer
	w := NewWriter(&buf, 10, NoCompression) // use a small maxChunkSize.
	assert.Nil(w.Close())
	assert.Equal(0, buf.Len())

	idx, e := LoadIndex(bytes.NewReader(buf.Bytes()))
	assert.Nil(e)
	assert.Equal(0, idx.NumRecords())
}