2015-02-23 14:57:56 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJsonPackerUnpacker(t *testing.T) {
|
|
|
|
e := []Entry{
|
|
|
|
Entry{
|
|
|
|
Type: SegmentType,
|
|
|
|
Payload: []byte("how"),
|
|
|
|
},
|
|
|
|
Entry{
|
|
|
|
Type: SegmentType,
|
|
|
|
Payload: []byte("y'all"),
|
|
|
|
},
|
|
|
|
Entry{
|
|
|
|
Type: FileType,
|
|
|
|
Name: "./hurr.txt",
|
|
|
|
Payload: []byte("deadbeef"),
|
|
|
|
},
|
|
|
|
Entry{
|
|
|
|
Type: SegmentType,
|
|
|
|
Payload: []byte("doin"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte{}
|
|
|
|
b := bytes.NewBuffer(buf)
|
|
|
|
|
|
|
|
func() {
|
|
|
|
jp := NewJsonPacker(b)
|
|
|
|
for i := range e {
|
|
|
|
if _, err := jp.AddEntry(e[i]); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
b = bytes.NewBuffer(b.Bytes())
|
2015-02-24 20:49:21 +00:00
|
|
|
entries := Entries{}
|
2015-02-23 14:57:56 +00:00
|
|
|
func() {
|
|
|
|
jup := NewJsonUnpacker(b)
|
|
|
|
for {
|
|
|
|
entry, err := jup.Next()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-02-24 20:49:21 +00:00
|
|
|
entries = append(entries, *entry)
|
|
|
|
t.Logf("got %#v", entry)
|
2015-02-23 14:57:56 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-02-24 20:49:21 +00:00
|
|
|
if len(entries) != len(e) {
|
|
|
|
t.Errorf("expected %d entries, got %d", len(e), len(entries))
|
|
|
|
}
|
2015-02-23 14:57:56 +00:00
|
|
|
|
|
|
|
}
|