Handle closing frame properly in datagramBuffer

This commit is contained in:
Andy Wang 2019-08-30 21:00:15 +01:00
parent 3bfaa5c1c1
commit 9dacb9d8fd
2 changed files with 64 additions and 40 deletions

View File

@ -64,7 +64,13 @@ func (d *datagramBuffer) Write(f Frame) error {
}
d.rwCond.Wait()
}
// TODO: deal with closing frame here
if f.Closing == 1 {
d.closed = true
d.rwCond.Broadcast()
return nil
}
data := make([]byte, len(f.Payload))
copy(data, f.Payload)
d.buf = append(d.buf, data)

View File

@ -7,18 +7,22 @@ import (
)
func TestDatagramBuffer_RW(t *testing.T) {
pipe := NewDatagramBuffer()
b := []byte{0x01, 0x02, 0x03}
t.Run("simple write", func(t *testing.T) {
pipe := NewDatagramBuffer()
err := pipe.Write(Frame{Payload: b})
if err != nil {
t.Error(
"For", "simple write",
"expecting", "nil error",
"got", err,
)
return
}
})
t.Run("simple read", func(t *testing.T) {
pipe := NewDatagramBuffer()
_ = pipe.Write(Frame{Payload: b})
b2 := make([]byte, len(b))
n, err := pipe.Read(b2)
if n != len(b) {
@ -31,7 +35,6 @@ func TestDatagramBuffer_RW(t *testing.T) {
}
if err != nil {
t.Error(
"For", "simple read",
"expecting", "nil error",
"got", err,
)
@ -39,7 +42,6 @@ func TestDatagramBuffer_RW(t *testing.T) {
}
if !bytes.Equal(b, b2) {
t.Error(
"For", "simple read",
"expecting", b,
"got", b2,
)
@ -49,6 +51,22 @@ func TestDatagramBuffer_RW(t *testing.T) {
return
}
})
t.Run("writing closing frame", func(t *testing.T) {
pipe := NewDatagramBuffer()
err := pipe.Write(Frame{Closing: 1})
if err != nil {
t.Error(
"expecting", "nil error",
"got", err,
)
return
}
if !pipe.closed {
t.Error("expecting closed pipe, not closed")
}
})
}
func TestDatagramBuffer_BlockingRead(t *testing.T) {