func TestReader(t *testing.T) { var ( err error msg protocol.Message frame protocol.Frame ) for _, c := range testCases { r := newReader(bytes.NewReader(c.encoded)) for _, p := range c.decoded { var buf bytes.Buffer msg, err = r.Read() if err != nil { break } frame, err = msg.Next() if err != nil { break } else if frame.Len() != len(p) { t.Error("invalid length") } if n, err := io.Copy(&buf, frame); err != nil { break } else if n != int64(len(p)) { t.Error("invalid length") } else if !bytes.Equal(buf.Bytes(), p) { t.Errorf("%#v != %#v", buf, p) } if msg.More() { t.Fatal("more") } } if err != c.err { t.Errorf("%#v != %#v", err, c.err) } } }
// Write writes the payload. func (w *writer) Write(src protocol.Message) error { frame, err := src.Next() if err != nil { return err } if src.More() { panic("Unsupported multiple frames") } binary.BigEndian.PutUint64(w.buf[:], uint64(frame.Len())) if written, err := w.wr.Write(w.buf[:]); err != nil { w.broken = true return err } else if written != 8 { w.broken = true return errors.New("zenio: failed to write frame size") } if _, err := io.Copy(w.wr, frame); err != nil { w.broken = true return err } return nil }
// Write writes the payload. func (w *writer) Write(src protocol.Message) error { for { frame, err := src.Next() if err = w.writeFrame(frame, src.More()); err != nil { w.broken = true return err } if !src.More() { break } } return nil }
func TestReader(t *testing.T) { for i, c := range testCases { if c.skipDec { continue } var ( j int n int64 err error msg protocol.Message frame protocol.Frame ) r := newReader(bytes.NewReader(c.encoded)) if msg, err = r.Read(); err != nil { t.Fatal(err) } for _, p := range c.decoded { frame, err = msg.Next() j++ if err != nil { break } else if frame.Len() != len(p) { t.Errorf("%d invalid length", i) } var buf bytes.Buffer if n, err = io.Copy(&buf, frame); err != nil { break } else if n != int64(len(p)) { t.Errorf("%d invalid length", i) } else if !bytes.Equal(buf.Bytes(), p) { t.Errorf("%d %#v != %#v", i, buf, p) } if !msg.More() { break } } if err != c.err { t.Errorf("%d %#v != %#v", i, err, c.err) } else if msg.More() { t.Fatal("not all frames consumed") } } }