Skip to content

Commit

Permalink
Refactor tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 18, 2019
1 parent f43c6bf commit 7ef6861
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 137 deletions.
204 changes: 99 additions & 105 deletions pkg/app2/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
60 changes: 28 additions & 32 deletions pkg/app2/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 7ef6861

Please sign in to comment.