func TestDecoder(t *testing.T) { frames := [][]byte{ make([]byte, 1025), make([]byte, 1024*5), make([]byte, 1024*1024*5), make([]byte, 1025), } pr, pw := io.Pipe() fw := framer.NewLengthDelimitedFrameWriter(pw) go func() { for i := range frames { fw.Write(frames[i]) } pw.Close() }() r := framer.NewLengthDelimitedFrameReader(pr) d := &fakeDecoder{} dec := NewDecoder(r, d) if _, _, err := dec.Decode(nil, nil); err != nil || !bytes.Equal(d.got, frames[0]) { t.Fatalf("unexpected %v %v", err, len(d.got)) } if _, _, err := dec.Decode(nil, nil); err != nil || !bytes.Equal(d.got, frames[1]) { t.Fatalf("unexpected %v %v", err, len(d.got)) } if _, _, err := dec.Decode(nil, nil); err != ErrObjectTooLarge || !bytes.Equal(d.got, frames[1]) { t.Fatalf("unexpected %v %v", err, len(d.got)) } if _, _, err := dec.Decode(nil, nil); err != nil || !bytes.Equal(d.got, frames[3]) { t.Fatalf("unexpected %v %v", err, len(d.got)) } if _, _, err := dec.Decode(nil, nil); err != io.EOF { t.Fatalf("unexpected %v %v", err, len(d.got)) } }
// NewFrameReader implements stream framing for this serializer func (lengthDelimitedFramer) NewFrameReader(r io.ReadCloser) io.ReadCloser { return framer.NewLengthDelimitedFrameReader(r) }