Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dequeue offset tracking #4

Merged
merged 5 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
// Reader interface.
type Reader interface {
io.Closer
ReadEntry(*Entry) (common.ErrCode, error)
io.Seeker
ReadEntry(*Entry) (common.ErrCode, int, error)
}

// Writer interface.
Expand Down Expand Up @@ -60,22 +61,22 @@ func (e Entry) marshalV1(w WriteFlusher) (code common.ErrCode, err error) {
}

// Unmarshal from reader.
func (e *Entry) Unmarshal(r io.Reader, format common.EntryFormat) (common.ErrCode, error) {
func (e *Entry) Unmarshal(r io.Reader, format common.EntryFormat) (common.ErrCode, int, error) {
switch format {
case common.EntryV1:
return e.unmarshalV1(r)

default:
return common.EntryUnsupportedFormat, common.ErrEntryUnsupportedFormat
return common.EntryUnsupportedFormat, 0, common.ErrEntryUnsupportedFormat
}
}

// [Length - uint32][Checksum - uint32][Payload - bytes]
func (e *Entry) unmarshalV1(r io.Reader) (code common.ErrCode, err error) {
func (e *Entry) unmarshalV1(r io.Reader) (code common.ErrCode, n int, err error) {
var buffer [8]byte

// read length
_, err = io.ReadFull(r, buffer[:])
n, err = io.ReadFull(r, buffer[:])
if errors.Is(err, io.EOF) {
code, err = common.EntryNoMore, nil
return
Expand All @@ -100,7 +101,9 @@ func (e *Entry) unmarshalV1(r io.Reader) (code common.ErrCode, err error) {
// read payload
data := e.alloc(int(size))

_, err = io.ReadFull(r, data)
n_, err := io.ReadFull(r, data)
n += n_

if err != nil {
code = common.EntryCorrupted
return
Expand Down
38 changes: 28 additions & 10 deletions entry/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,69 @@ import (
func TestEntryUnmarshal(t *testing.T) {
t.Run("UnsupportedFormat", func(t *testing.T) {
var e Entry
code, err := e.Unmarshal(nil, 123)
code, n, err := e.Unmarshal(nil, 123)
require.Equal(t, common.EntryUnsupportedFormat, code)
require.Equal(t, common.ErrEntryUnsupportedFormat, err)
require.Equal(t, 0, n)
})

t.Run("Corrupted", func(t *testing.T) {
var e Entry

// length missing
{
code, err := e.Unmarshal(bytes.NewBuffer([]byte{1, 2}), common.EntryV1)
code, n, err := e.Unmarshal(bytes.NewBuffer([]byte{1, 2}), common.EntryV1)
require.Equal(t, common.EntryCorrupted, code)
require.Error(t, err)
require.Equal(t, 2, n)
}

// length zero
{
code, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 0, 0, 0, 0, 0}), common.EntryV1)
code, n, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 0, 0, 0, 0, 0}), common.EntryV1)
require.Equal(t, common.EntryZeroSize, code)
require.NoError(t, err)
require.Equal(t, 8, n)
}

// too big
{
code, err := e.Unmarshal(bytes.NewBuffer([]byte{255, 255, 0, 0, 0, 0, 0, 0}), common.EntryV1)
code, n, err := e.Unmarshal(bytes.NewBuffer([]byte{255, 255, 0, 0, 0, 0, 0, 0}), common.EntryV1)
require.Equal(t, common.EntryTooBig, code)
require.NoError(t, err)
require.Equal(t, 8, n)
}

// missing payload
{
code, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 2, 1}), common.EntryV1)
code, n, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 2, 1}), common.EntryV1)
require.Equal(t, common.EntryCorrupted, code)
require.Error(t, err)
require.Equal(t, 5, n)
}

// missing sum
{
code, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 2, 1, 1}), common.EntryV1)
code, n, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 2, 1, 1}), common.EntryV1)
require.Equal(t, common.EntryCorrupted, code)
require.Error(t, err)
require.Equal(t, 6, n)
}

// invalid sum
{
code, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 2, 1, 1, 1, 2, 3, 4}), common.EntryV1)
code, n, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 2, 1, 1, 1, 2, 3, 4}), common.EntryV1)
require.Equal(t, common.EntryCorrupted, code)
require.Equal(t, common.ErrEntryInvalidCheckSum, err)
require.Equal(t, 10, n)
}

// corrupt
{
code, n, err := e.Unmarshal(bytes.NewBuffer([]byte{0, 0, 0, 2, 1, 1, 1, 2, 3}), common.EntryV1)
require.Equal(t, common.EntryCorrupted, code)
require.Error(t, err)
require.Equal(t, 9, n)
}
})

Expand All @@ -70,13 +85,15 @@ func TestEntryUnmarshal(t *testing.T) {

buf := bytes.NewBuffer([]byte{0, 0, 0, 2, 173, 62, 94, 152, 19, 31})

code, err := e.Unmarshal(buf, common.EntryV1)
code, n, err := e.Unmarshal(buf, common.EntryV1)
require.Equal(t, common.NoError, code)
require.NoError(t, err)
require.Equal(t, 10, n)

code, err = e.Unmarshal(buf, common.EntryV1)
code, n, err = e.Unmarshal(buf, common.EntryV1)
require.Equal(t, common.EntryNoMore, code)
require.NoError(t, err)
require.Equal(t, 0, n)
})

t.Run("Clone", func(t *testing.T) {
Expand Down Expand Up @@ -153,9 +170,10 @@ func TestEntry(t *testing.T) {
require.Equal(t, common.NoError, code)

var tmp Entry
code, err = tmp.Unmarshal(&buf, common.EntryV1)
code, n, err := tmp.Unmarshal(&buf, common.EntryV1)
require.NoError(t, err)
require.Equal(t, common.NoError, code)
require.Equal(t, 131, n)

require.EqualValues(t, e, tmp)
}
Loading