Merge pull request #239 from fkautz/pr_out_adding_test_at_byte_level

master
Frederick F. Kautz IV 10 years ago
commit 60454c53a2
  1. 7
      pkg/storage/donut/data/data_v1/data.go
  2. 22
      pkg/storage/donut/data/data_v1/data_test.go

@ -82,10 +82,9 @@ func WriteData(target io.Writer, header DataHeader, data io.Reader) error {
// encode header // encode header
encoder := gob.NewEncoder(&headerBuffer) encoder := gob.NewEncoder(&headerBuffer)
encoder.Encode(header) encoder.Encode(header)
// write length of header
if err := binary.Write(target, binary.LittleEndian, int64(headerBuffer.Len())); err != nil { // write version
return err binary.Write(target, binary.LittleEndian, uint32(1))
}
// write encoded header // write encoded header
if _, err := io.Copy(target, &headerBuffer); err != nil { if _, err := io.Copy(target, &headerBuffer); err != nil {

@ -18,6 +18,9 @@ package data_v1
import ( import (
"bytes" "bytes"
"encoding/binary"
"encoding/gob"
"io"
"testing" "testing"
. "gopkg.in/check.v1" . "gopkg.in/check.v1"
@ -46,4 +49,23 @@ func (s *MySuite) TestSingleWrite(c *C) {
err := WriteData(&testBuffer, header, bytes.NewBufferString(testData)) err := WriteData(&testBuffer, header, bytes.NewBufferString(testData))
c.Assert(err, IsNil) c.Assert(err, IsNil)
actualVersion := make([]byte, 4)
_, err = testBuffer.Read(actualVersion)
c.Assert(err, IsNil)
c.Assert(binary.LittleEndian.Uint32(actualVersion), DeepEquals, uint32(1))
actualHeader := DataHeader{}
decoder := gob.NewDecoder(&testBuffer)
decoder.Decode(&actualHeader)
c.Assert(actualHeader, DeepEquals, header)
var actualData bytes.Buffer
dataLength, err := io.Copy(&actualData, &testBuffer)
c.Assert(dataLength, Equals, int64(len(testData)))
c.Assert(actualData.Bytes(), DeepEquals, []byte(testData))
c.Assert(err, IsNil)
} }

Loading…
Cancel
Save