mirror of https://github.com/cbeuw/Cloak
Handle closing frame properly in datagramBuffer
This commit is contained in:
parent
3bfaa5c1c1
commit
9dacb9d8fd
|
|
@ -64,7 +64,13 @@ func (d *datagramBuffer) Write(f Frame) error {
|
||||||
}
|
}
|
||||||
d.rwCond.Wait()
|
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))
|
data := make([]byte, len(f.Payload))
|
||||||
copy(data, f.Payload)
|
copy(data, f.Payload)
|
||||||
d.buf = append(d.buf, data)
|
d.buf = append(d.buf, data)
|
||||||
|
|
|
||||||
|
|
@ -7,18 +7,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDatagramBuffer_RW(t *testing.T) {
|
func TestDatagramBuffer_RW(t *testing.T) {
|
||||||
pipe := NewDatagramBuffer()
|
|
||||||
b := []byte{0x01, 0x02, 0x03}
|
b := []byte{0x01, 0x02, 0x03}
|
||||||
|
t.Run("simple write", func(t *testing.T) {
|
||||||
|
pipe := NewDatagramBuffer()
|
||||||
err := pipe.Write(Frame{Payload: b})
|
err := pipe.Write(Frame{Payload: b})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(
|
t.Error(
|
||||||
"For", "simple write",
|
|
||||||
"expecting", "nil error",
|
"expecting", "nil error",
|
||||||
"got", err,
|
"got", err,
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("simple read", func(t *testing.T) {
|
||||||
|
pipe := NewDatagramBuffer()
|
||||||
|
_ = pipe.Write(Frame{Payload: b})
|
||||||
b2 := make([]byte, len(b))
|
b2 := make([]byte, len(b))
|
||||||
n, err := pipe.Read(b2)
|
n, err := pipe.Read(b2)
|
||||||
if n != len(b) {
|
if n != len(b) {
|
||||||
|
|
@ -31,7 +35,6 @@ func TestDatagramBuffer_RW(t *testing.T) {
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(
|
t.Error(
|
||||||
"For", "simple read",
|
|
||||||
"expecting", "nil error",
|
"expecting", "nil error",
|
||||||
"got", err,
|
"got", err,
|
||||||
)
|
)
|
||||||
|
|
@ -39,7 +42,6 @@ func TestDatagramBuffer_RW(t *testing.T) {
|
||||||
}
|
}
|
||||||
if !bytes.Equal(b, b2) {
|
if !bytes.Equal(b, b2) {
|
||||||
t.Error(
|
t.Error(
|
||||||
"For", "simple read",
|
|
||||||
"expecting", b,
|
"expecting", b,
|
||||||
"got", b2,
|
"got", b2,
|
||||||
)
|
)
|
||||||
|
|
@ -49,6 +51,22 @@ func TestDatagramBuffer_RW(t *testing.T) {
|
||||||
return
|
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) {
|
func TestDatagramBuffer_BlockingRead(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue