From 6f300c9ef05019e75f2c2608da41fed05be9f321 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sun, 1 Mar 2015 15:13:09 -0800 Subject: [PATCH] Adding test at byte level --- pkg/storage/donut/data/data_v1/data.go | 7 +++---- pkg/storage/donut/data/data_v1/data_test.go | 22 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pkg/storage/donut/data/data_v1/data.go b/pkg/storage/donut/data/data_v1/data.go index a07e9debe..4c3e6e148 100644 --- a/pkg/storage/donut/data/data_v1/data.go +++ b/pkg/storage/donut/data/data_v1/data.go @@ -82,10 +82,9 @@ func WriteData(target io.Writer, header DataHeader, data io.Reader) error { // encode header encoder := gob.NewEncoder(&headerBuffer) encoder.Encode(header) - // write length of header - if err := binary.Write(target, binary.LittleEndian, int64(headerBuffer.Len())); err != nil { - return err - } + + // write version + binary.Write(target, binary.LittleEndian, uint32(1)) // write encoded header if _, err := io.Copy(target, &headerBuffer); err != nil { diff --git a/pkg/storage/donut/data/data_v1/data_test.go b/pkg/storage/donut/data/data_v1/data_test.go index 2bd05c8b7..d07fbdf59 100644 --- a/pkg/storage/donut/data/data_v1/data_test.go +++ b/pkg/storage/donut/data/data_v1/data_test.go @@ -18,6 +18,9 @@ package data_v1 import ( "bytes" + "encoding/binary" + "encoding/gob" + "io" "testing" . "gopkg.in/check.v1" @@ -46,4 +49,23 @@ func (s *MySuite) TestSingleWrite(c *C) { err := WriteData(&testBuffer, header, bytes.NewBufferString(testData)) 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) }