Skip to content

Commit

Permalink
Fix Header.Status
Browse files Browse the repository at this point in the history
The size indices were not adjusted when we changed to int16 recently, which made it
overwrite the header status. Which shows the importance of good tests.
  • Loading branch information
bep committed Sep 8, 2022
1 parent 3a67fc8 commit 2c5a478
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client_test.go
Expand Up @@ -144,7 +144,7 @@ func TestExecTyped(t *testing.T) {
c.Assert(err, qt.IsNil)
c.Assert(len(logMessages), qt.Equals, 2)
c.Assert(string(logMessages[0].Body), qt.Equals, "first log message")
c.Assert(logMessages[0].Header.Status, qt.Equals, uint16(0))
c.Assert(logMessages[0].Header.Status, qt.Equals, uint16(150))
c.Assert(logMessages[0].Header.Version, qt.Equals, uint16(32))
c.Assert(client.Close(), qt.IsNil)
})
Expand Down
4 changes: 2 additions & 2 deletions message.go
Expand Up @@ -50,7 +50,7 @@ func (h *Header) Read(r io.Reader) error {
h.ID = binary.BigEndian.Uint32(buf[0:4])
h.Version = binary.BigEndian.Uint16(buf[4:6])
h.Status = binary.BigEndian.Uint16(buf[6:8])
h.Size = binary.BigEndian.Uint32(buf[6:])
h.Size = binary.BigEndian.Uint32(buf[8:])
return nil
}

Expand All @@ -60,7 +60,7 @@ func (h Header) Write(w io.Writer) error {
binary.BigEndian.PutUint32(buff[0:4], h.ID)
binary.BigEndian.PutUint16(buff[4:6], h.Version)
binary.BigEndian.PutUint16(buff[6:8], h.Status)
binary.BigEndian.PutUint32(buff[6:], h.Size)
binary.BigEndian.PutUint32(buff[8:], h.Size)
_, err := w.Write(buff)
return err
}
34 changes: 34 additions & 0 deletions message_test.go
@@ -0,0 +1,34 @@
package execrpc

import (
"bytes"
"testing"

qt "github.com/frankban/quicktest"
)

func TestMessage(t *testing.T) {
c := qt.New(t)

m1 := Message{
Body: []byte("hello"),
Header: Header{
ID: 2,
Version: 3,
Status: 4,
Size: 5,
},
}

var b bytes.Buffer

c.Assert(m1.Header.Write(&b), qt.IsNil)
c.Assert(m1.Write(&b), qt.IsNil)

var m2 Message
c.Assert(m2.Header.Read(&b), qt.IsNil)
c.Assert(m2.Read(&b), qt.IsNil)

c.Assert(m2, qt.DeepEquals, m1)

}

0 comments on commit 2c5a478

Please sign in to comment.