diff --git a/pkg/app2/conn_test.go b/pkg/app2/conn_test.go index 9ced6c05d7..d25de2716b 100644 --- a/pkg/app2/conn_test.go +++ b/pkg/app2/conn_test.go @@ -10,119 +10,113 @@ import ( func TestConn_Read(t *testing.T) { connID := uint16(1) - t.Run("ok", func(t *testing.T) { - readBuff := make([]byte, 100) - readN := 20 - readBytes := make([]byte, 100) - for i := 0; i < readN; i++ { - readBytes[i] = 2 - } - var readErr error - - rpc := &MockServerRPCClient{} - rpc.On("Read", connID, readBuff).Return(readN, readBytes, readErr) - - conn := &Conn{ - id: connID, - rpc: rpc, - } - - n, err := conn.Read(readBuff) - require.NoError(t, err) - require.Equal(t, n, readN) - require.Equal(t, readBuff[:n], readBytes[:n]) - }) - - t.Run("read error", func(t *testing.T) { - readBuff := make([]byte, 100) - readN := 0 - var readBytes []byte - readErr := errors.New("read error") - - rpc := &MockServerRPCClient{} - rpc.On("Read", connID, readBuff).Return(readN, readBytes, readErr) - - conn := &Conn{ - id: connID, - rpc: rpc, - } - - n, err := conn.Read(readBuff) - require.Equal(t, readErr, err) - require.Equal(t, readN, n) - }) + tt := []struct { + name string + readBuff []byte + readN int + readBytes []byte + readErr error + wantBuff []byte + }{ + { + name: "ok", + readBuff: make([]byte, 10), + readN: 2, + readBytes: []byte{1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + wantBuff: []byte{1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + { + name: "read error", + readBuff: make([]byte, 10), + readErr: errors.New("read error"), + wantBuff: make([]byte, 10), + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + rpc := &MockServerRPCClient{} + rpc.On("Read", connID, tc.readBuff).Return(tc.readN, tc.readBytes, tc.readErr) + + conn := &Conn{ + id: connID, + rpc: rpc, + } + + n, err := conn.Read(tc.readBuff) + require.Equal(t, tc.readErr, err) + require.Equal(t, tc.readN, n) + require.Equal(t, tc.wantBuff, tc.readBuff) + }) + } } func TestConn_Write(t *testing.T) { connID := uint16(1) - t.Run("ok", func(t *testing.T) { - writeBuff := make([]byte, 100) - writeN := 20 - var writeErr error - - rpc := &MockServerRPCClient{} - rpc.On("Write", connID, writeBuff).Return(writeN, writeErr) - - conn := &Conn{ - id: connID, - rpc: rpc, - } - - n, err := conn.Write(writeBuff) - require.NoError(t, err) - require.Equal(t, writeN, n) - }) - - t.Run("write error", func(t *testing.T) { - writeBuff := make([]byte, 100) - writeN := 0 - writeErr := errors.New("write error") - - rpc := &MockServerRPCClient{} - rpc.On("Write", connID, writeBuff).Return(writeN, writeErr) - - conn := &Conn{ - id: connID, - rpc: rpc, - } - - n, err := conn.Write(writeBuff) - require.Equal(t, writeErr, err) - require.Equal(t, writeN, n) - }) + tt := []struct { + name string + writeBuff []byte + writeN int + writeErr error + }{ + { + name: "ok", + writeBuff: make([]byte, 10), + writeN: 2, + }, + { + name: "write error", + writeBuff: make([]byte, 10), + writeErr: errors.New("write error"), + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + rpc := &MockServerRPCClient{} + rpc.On("Write", connID, tc.writeBuff).Return(tc.writeN, tc.writeErr) + + conn := &Conn{ + id: connID, + rpc: rpc, + } + + n, err := conn.Write(tc.writeBuff) + require.Equal(t, tc.writeErr, err) + require.Equal(t, tc.writeN, n) + }) + } } func TestConn_Close(t *testing.T) { connID := uint16(1) - t.Run("ok", func(t *testing.T) { - var closeErr error - - rpc := &MockServerRPCClient{} - rpc.On("CloseConn", connID).Return(closeErr) - - conn := &Conn{ - id: connID, - rpc: rpc, - } - - err := conn.Close() - require.NoError(t, err) - }) - - t.Run("close error", func(t *testing.T) { - closeErr := errors.New("close error") - - rpc := &MockServerRPCClient{} - rpc.On("CloseConn", connID).Return(closeErr) - - conn := &Conn{ - id: connID, - rpc: rpc, - } - - err := conn.Close() - require.Equal(t, closeErr, err) - }) + tt := []struct { + name string + closeErr error + }{ + { + name: "ok", + }, + { + name: "close error", + closeErr: errors.New("close error"), + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + rpc := &MockServerRPCClient{} + rpc.On("CloseConn", connID).Return(tc.closeErr) + + conn := &Conn{ + id: connID, + rpc: rpc, + } + + err := conn.Close() + require.Equal(t, tc.closeErr, err) + }) + } } diff --git a/pkg/app2/listener_test.go b/pkg/app2/listener_test.go index e9336d2788..e43b384f86 100644 --- a/pkg/app2/listener_test.go +++ b/pkg/app2/listener_test.go @@ -76,37 +76,33 @@ func TestListener_Close(t *testing.T) { Port: routing.Port(100), } - t.Run("ok", func(t *testing.T) { - var closeErr error - - rpc := &MockServerRPCClient{} - rpc.On("CloseListener", lisID).Return(closeErr) - - lis := &Listener{ - id: lisID, - rpc: rpc, - addr: local, - freePort: func() {}, - } - - err := lis.Close() - require.NoError(t, err) - }) - - t.Run("close error", func(t *testing.T) { - closeErr := errors.New("close error") - - rpc := &MockServerRPCClient{} - rpc.On("CloseListener", lisID).Return(closeErr) - - lis := &Listener{ - id: lisID, - rpc: rpc, - addr: local, - freePort: func() {}, - } + tt := []struct { + name string + closeErr error + }{ + { + name: "ok", + }, + { + name: "close error", + closeErr: errors.New("close error"), + }, + } - err := lis.Close() - require.Equal(t, closeErr, err) - }) + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + rpc := &MockServerRPCClient{} + rpc.On("CloseListener", lisID).Return(tc.closeErr) + + lis := &Listener{ + id: lisID, + rpc: rpc, + addr: local, + freePort: func() {}, + } + + err := lis.Close() + require.Equal(t, tc.closeErr, err) + }) + } }