2020-03-16 11:37:09 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-04-07 19:59:32 +00:00
|
|
|
"github.com/cbeuw/connutil"
|
2020-03-16 11:37:09 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFirstBuffedConn_Read(t *testing.T) {
|
2020-04-07 19:59:32 +00:00
|
|
|
mockConn, writingEnd := connutil.AsyncPipe()
|
2020-03-16 11:37:09 +00:00
|
|
|
|
|
|
|
expectedFirstPacket := []byte{1, 2, 3}
|
|
|
|
firstBuffedConn := &firstBuffedConn{
|
|
|
|
Conn: mockConn,
|
|
|
|
firstRead: false,
|
|
|
|
firstPacket: expectedFirstPacket,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 1024)
|
2020-04-07 19:59:32 +00:00
|
|
|
n, err := firstBuffedConn.Read(buf)
|
2020-03-16 11:37:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-04-07 19:59:32 +00:00
|
|
|
if !bytes.Equal(expectedFirstPacket, buf[:n]) {
|
2020-03-16 11:37:09 +00:00
|
|
|
t.Error("first read doesn't produce given packet")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:59:32 +00:00
|
|
|
expectedSecondPacket := []byte{4, 5, 6, 7}
|
|
|
|
writingEnd.Write(expectedSecondPacket)
|
|
|
|
n, err = firstBuffedConn.Read(buf)
|
2020-03-16 11:37:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2020-04-07 19:59:32 +00:00
|
|
|
if !bytes.Equal(expectedSecondPacket, buf[:n]) {
|
2020-03-16 11:37:09 +00:00
|
|
|
t.Error("second read doesn't produce subsequently written packet")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:59:32 +00:00
|
|
|
func TestWsAcceptor(t *testing.T) {
|
|
|
|
mockConn := connutil.Discard()
|
2020-03-16 11:37:09 +00:00
|
|
|
expectedFirstPacket := []byte{1, 2, 3}
|
|
|
|
|
2020-04-07 19:59:32 +00:00
|
|
|
wsAcceptor := newWsAcceptor(mockConn, expectedFirstPacket)
|
|
|
|
_, err := wsAcceptor.Accept()
|
2020-03-16 11:37:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:59:32 +00:00
|
|
|
_, err = wsAcceptor.Accept()
|
|
|
|
if err == nil {
|
2020-03-16 11:37:09 +00:00
|
|
|
t.Error("accepting second time doesn't return error")
|
|
|
|
}
|
2020-04-07 19:59:32 +00:00
|
|
|
}
|