Consolidated redundant socket mocking function.
This commit is contained in:
@@ -80,7 +80,7 @@ func TestDisconnectedConnectionClose(t *testing.T) {
|
||||
|
||||
func TestConnectedConnectionClose(t *testing.T) {
|
||||
t.Run("blocked on ReadMessage, unblocks on closed", func(t *testing.T) {
|
||||
conn, _, incomingData, _ := setupTestConnection(t, nil)
|
||||
conn, _, incomingData, _ := setupTestConnection(t)
|
||||
|
||||
// Send a message to ensure reader loop is blocking
|
||||
canary := []byte("canary")
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
func TestStartReader(t *testing.T) {
|
||||
t.Run("text messages route to incoming channel", func(t *testing.T) {
|
||||
conn, _, incomingData, _ := setupTestConnection(t, nil)
|
||||
conn, _, incomingData, _ := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
testData := []byte("hello")
|
||||
@@ -24,7 +24,7 @@ func TestStartReader(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("binary messages route to incoming channel", func(t *testing.T) {
|
||||
conn, _, incomingData, _ := setupTestConnection(t, nil)
|
||||
conn, _, incomingData, _ := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
testData := []byte{0x00, 0x01, 0x02}
|
||||
@@ -38,7 +38,7 @@ func TestStartReader(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("multiple messages processed sequentially", func(t *testing.T) {
|
||||
conn, _, incomingData, _ := setupTestConnection(t, nil)
|
||||
conn, _, incomingData, _ := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
messages := [][]byte{[]byte("first"), []byte("second"), []byte("third")}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func TestConnectionSend(t *testing.T) {
|
||||
t.Run("writes message to socket", func(t *testing.T) {
|
||||
conn, _, _, outgoingData := setupTestConnection(t, nil)
|
||||
conn, _, _, outgoingData := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
testData := []byte("test message")
|
||||
@@ -24,7 +24,7 @@ func TestConnectionSend(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("writes multiple message to socket", func(t *testing.T) {
|
||||
conn, _, _, outgoingData := setupTestConnection(t, nil)
|
||||
conn, _, _, outgoingData := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
messages := [][]byte{[]byte("first"), []byte("second"), []byte("third")}
|
||||
@@ -39,7 +39,7 @@ func TestConnectionSend(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("concurrent sends write messages to socket", func(t *testing.T) {
|
||||
conn, _, _, outgoingData := setupTestConnection(t, nil)
|
||||
conn, _, _, outgoingData := setupTestConnection(t)
|
||||
defer conn.Close()
|
||||
|
||||
mu := sync.Mutex{}
|
||||
@@ -92,7 +92,7 @@ func TestConnectionSend(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("send fails when connection is closed", func(t *testing.T) {
|
||||
conn, _, _, _ := setupTestConnection(t, nil)
|
||||
conn, _, _, _ := setupTestConnection(t)
|
||||
conn.Close()
|
||||
|
||||
testData := []byte("test message")
|
||||
|
||||
@@ -539,54 +539,18 @@ func TestConnectionErrors(t *testing.T) {
|
||||
|
||||
// Test helpers
|
||||
|
||||
func setupTestConnection(t *testing.T, config *ConnectionConfig) (
|
||||
func setupTestConnection(t *testing.T) (
|
||||
conn *Connection,
|
||||
mockSocket *honeybeetest.MockSocket,
|
||||
incomingData chan honeybeetest.MockIncomingData,
|
||||
outgoingData chan honeybeetest.MockOutgoingData,
|
||||
socket *honeybeetest.MockSocket,
|
||||
incoming chan honeybeetest.MockIncomingData,
|
||||
outgoing chan honeybeetest.MockOutgoingData,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
incomingData = make(chan honeybeetest.MockIncomingData, 10)
|
||||
outgoingData = make(chan honeybeetest.MockOutgoingData, 10)
|
||||
|
||||
mockSocket = honeybeetest.NewMockSocket()
|
||||
|
||||
mockSocket.CloseFunc = func() error {
|
||||
mockSocket.Once.Do(func() {
|
||||
close(mockSocket.Closed)
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// Wire ReadMessage to pull from incomingData channel
|
||||
mockSocket.ReadMessageFunc = func() (int, []byte, error) {
|
||||
select {
|
||||
case data, ok := <-incomingData:
|
||||
if !ok {
|
||||
return 0, nil, io.EOF
|
||||
}
|
||||
return data.MsgType, data.Data, data.Err
|
||||
case <-mockSocket.Closed:
|
||||
return 0, nil, io.EOF
|
||||
}
|
||||
}
|
||||
|
||||
// Wire WriteMessage to push to outgoingData channel
|
||||
mockSocket.WriteMessageFunc = func(msgType int, data []byte) error {
|
||||
select {
|
||||
case outgoingData <- honeybeetest.MockOutgoingData{MsgType: msgType, Data: data}:
|
||||
return nil
|
||||
case <-mockSocket.Closed:
|
||||
return io.EOF
|
||||
default:
|
||||
return fmt.Errorf("mock outgoing chanel unavailable")
|
||||
}
|
||||
}
|
||||
socket, incoming, outgoing = honeybeetest.SetupTestSocket(t)
|
||||
|
||||
var err error
|
||||
conn, err = NewConnectionFromSocket(mockSocket, config, nil)
|
||||
conn, err = NewConnectionFromSocket(socket, nil, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
return conn, mockSocket, incomingData, outgoingData
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user