Skip to content

Commit

Permalink
Fix client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 24, 2019
1 parent 677fb24 commit 3a9fdba
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 65 deletions.
117 changes: 104 additions & 13 deletions pkg/app2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app2
import (
"testing"

"github.com/skycoin/skycoin/src/util/logging"

"github.com/pkg/errors"
"github.com/skycoin/dmsg/cipher"
"github.com/stretchr/testify/require"
Expand All @@ -12,56 +14,101 @@ import (
)

func TestClient_Dial(t *testing.T) {
l := logging.MustGetLogger("app2_client")
localPK, _ := cipher.GenerateKeyPair()
pid := ProcID(1)

remotePK, _ := cipher.GenerateKeyPair()
remotePort := routing.Port(120)
remote := network.Addr{
Net: network.TypeDMSG,
PubKey: remotePK,
Port: remotePort,
}

t.Run("ok", func(t *testing.T) {
dialConnID := uint16(1)
dialAssignedPort := routing.Port(1)
var dialErr error

rpc := &MockRPCClient{}
rpc.On("Dial", remote).Return(dialConnID, dialErr)
rpc.On("Dial", remote).Return(dialConnID, dialAssignedPort, dialErr)

cl := NewClient(localPK, pid, rpc)
cl := NewClient(l, localPK, pid, rpc)

wantConn := &Conn{
id: dialConnID,
rpc: rpc,
local: network.Addr{
Net: remote.Net,
PubKey: localPK,
Port: dialAssignedPort,
},
remote: remote,
}

conn, err := cl.Dial(remote)
require.NoError(t, err)

appConn, ok := conn.(*Conn)
require.True(t, ok)

require.NoError(t, err)
require.Equal(t, wantConn.id, appConn.id)
require.Equal(t, wantConn.rpc, appConn.rpc)
require.Equal(t, wantConn.local.PubKey, appConn.local.PubKey)
require.Equal(t, wantConn.local, appConn.local)
require.Equal(t, wantConn.remote, appConn.remote)
require.NotNil(t, appConn.freeLocalPort)
portVal, ok := cl.porter.PortValue(uint16(appConn.local.Port))
require.True(t, ok)
require.Nil(t, portVal)
require.NotNil(t, appConn.freeConn)
})

t.Run("conn already exists", func(t *testing.T) {
dialConnID := uint16(1)
dialAssignedPort := routing.Port(1)
var dialErr error

var closeErr error

rpc := &MockRPCClient{}
rpc.On("Dial", remote).Return(dialConnID, dialAssignedPort, dialErr)
rpc.On("CloseConn", dialConnID).Return(closeErr)

cl := NewClient(l, localPK, pid, rpc)

_, err := cl.cm.add(dialConnID, nil)
require.NoError(t, err)

conn, err := cl.Dial(remote)
require.Equal(t, err, errValueAlreadyExists)
require.Nil(t, conn)
})

t.Run("conn already exists, conn closed with error", func(t *testing.T) {
dialConnID := uint16(1)
dialAssignedPort := routing.Port(1)
var dialErr error

closeErr := errors.New("close error")

rpc := &MockRPCClient{}
rpc.On("Dial", remote).Return(dialConnID, dialAssignedPort, dialErr)
rpc.On("CloseConn", dialConnID).Return(closeErr)

cl := NewClient(l, localPK, pid, rpc)

_, err := cl.cm.add(dialConnID, nil)
require.NoError(t, err)

conn, err := cl.Dial(remote)
require.Equal(t, err, errValueAlreadyExists)
require.Nil(t, conn)
})

t.Run("dial error", func(t *testing.T) {
dialErr := errors.New("dial error")

rpc := &MockRPCClient{}
rpc.On("Dial", remote).Return(uint16(0), dialErr)
rpc.On("Dial", remote).Return(uint16(0), uint16(0), dialErr)

cl := NewClient(localPK, pid, rpc)
cl := NewClient(l, localPK, pid, rpc)

conn, err := cl.Dial(remote)
require.Equal(t, dialErr, err)
Expand All @@ -70,6 +117,7 @@ func TestClient_Dial(t *testing.T) {
}

func TestClient_Listen(t *testing.T) {
l := logging.MustGetLogger("app2_client")
localPK, _ := cipher.GenerateKeyPair()
pid := ProcID(1)

Expand All @@ -87,7 +135,7 @@ func TestClient_Listen(t *testing.T) {
rpc := &MockRPCClient{}
rpc.On("Listen", local).Return(listenLisID, listenErr)

cl := NewClient(localPK, pid, rpc)
cl := NewClient(l, localPK, pid, rpc)

wantListener := &Listener{
id: listenLisID,
Expand All @@ -97,12 +145,15 @@ func TestClient_Listen(t *testing.T) {

listener, err := cl.Listen(network.TypeDMSG, port)
require.Nil(t, err)

appListener, ok := listener.(*Listener)
require.True(t, ok)

require.Equal(t, wantListener.id, appListener.id)
require.Equal(t, wantListener.rpc, appListener.rpc)
require.Equal(t, wantListener.addr, appListener.addr)
require.NotNil(t, appListener.freePort)
require.NotNil(t, appListener.freeLis)
portVal, ok := cl.porter.PortValue(uint16(port))
require.True(t, ok)
require.Nil(t, portVal)
Expand All @@ -111,7 +162,7 @@ func TestClient_Listen(t *testing.T) {
t.Run("port is already bound", func(t *testing.T) {
rpc := &MockRPCClient{}

cl := NewClient(localPK, pid, rpc)
cl := NewClient(l, localPK, pid, rpc)

ok, _ := cl.porter.Reserve(uint16(port), nil)
require.True(t, ok)
Expand All @@ -123,13 +174,53 @@ func TestClient_Listen(t *testing.T) {
require.Nil(t, listener)
})

t.Run("listener already exists", func(t *testing.T) {
listenLisID := uint16(1)
var listenErr error

var closeErr error

rpc := &MockRPCClient{}
rpc.On("Listen", local).Return(listenLisID, listenErr)
rpc.On("CloseListener", listenLisID).Return(closeErr)

cl := NewClient(l, localPK, pid, rpc)

_, err := cl.lm.add(listenLisID, nil)
require.NoError(t, err)

listener, err := cl.Listen(network.TypeDMSG, port)
require.Equal(t, err, errValueAlreadyExists)
require.Nil(t, listener)
})

t.Run("listener already exists, listener closed with error", func(t *testing.T) {
listenLisID := uint16(1)
var listenErr error

closeErr := errors.New("close error")

rpc := &MockRPCClient{}
rpc.On("Listen", local).Return(listenLisID, listenErr)
rpc.On("CloseListener", listenLisID).Return(closeErr)

cl := NewClient(l, localPK, pid, rpc)

_, err := cl.lm.add(listenLisID, nil)
require.NoError(t, err)

listener, err := cl.Listen(network.TypeDMSG, port)
require.Equal(t, err, errValueAlreadyExists)
require.Nil(t, listener)
})

t.Run("listen error", func(t *testing.T) {
listenErr := errors.New("listen error")

rpc := &MockRPCClient{}
rpc.On("Listen", local).Return(uint16(0), listenErr)

cl := NewClient(localPK, pid, rpc)
cl := NewClient(l, localPK, pid, rpc)

listener, err := cl.Listen(network.TypeDMSG, port)
require.Equal(t, listenErr, err)
Expand Down
6 changes: 5 additions & 1 deletion pkg/app2/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func (c *Conn) Write(b []byte) (int, error) {
}

func (c *Conn) Close() error {
defer c.freeConn()
defer func() {
if c.freeConn != nil {
c.freeConn()
}
}()

return c.rpc.CloseConn(c.id)
}
Expand Down
29 changes: 15 additions & 14 deletions pkg/app2/id_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ func TestIDManager_NextID(t *testing.T) {
t.Run("simple call", func(t *testing.T) {
m := newIDManager()

nextKey, err := m.nextKey()
// TODO: use free
nextID, _, err := m.reserveNextID()
require.NoError(t, err)
v, ok := m.values[*nextKey]
v, ok := m.values[*nextID]
require.True(t, ok)
require.Nil(t, v)
require.Equal(t, *nextKey, uint16(1))
require.Equal(t, *nextKey, m.lstKey)
require.Equal(t, *nextID, uint16(1))
require.Equal(t, *nextID, m.lstID)

nextKey, err = m.nextKey()
nextID, _, err = m.reserveNextID()
require.NoError(t, err)
v, ok = m.values[*nextKey]
v, ok = m.values[*nextID]
require.True(t, ok)
require.Nil(t, v)
require.Equal(t, *nextKey, uint16(2))
require.Equal(t, *nextKey, m.lstKey)
require.Equal(t, *nextID, uint16(2))
require.Equal(t, *nextID, m.lstID)
})

t.Run("call on full manager", func(t *testing.T) {
Expand All @@ -35,7 +36,7 @@ func TestIDManager_NextID(t *testing.T) {
}
m.values[math.MaxUint16] = nil

_, err := m.nextKey()
_, _, err := m.reserveNextID()
require.Error(t, err)
})

Expand All @@ -47,7 +48,7 @@ func TestIDManager_NextID(t *testing.T) {
errs := make(chan error)
for i := 0; i < valsToReserve; i++ {
go func() {
_, err := m.nextKey()
_, _, err := m.reserveNextID()
errs <- err
}()
}
Expand All @@ -57,7 +58,7 @@ func TestIDManager_NextID(t *testing.T) {
}
close(errs)

require.Equal(t, m.lstKey, uint16(valsToReserve))
require.Equal(t, m.lstID, uint16(valsToReserve))
for i := uint16(1); i < uint16(valsToReserve); i++ {
v, ok := m.values[i]
require.True(t, ok)
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestIDManager_Set(t *testing.T) {
t.Run("simple call", func(t *testing.T) {
m := newIDManager()

nextKey, err := m.nextKey()
nextKey, _, err := m.reserveNextID()
require.NoError(t, err)

v := "value"
Expand Down Expand Up @@ -173,7 +174,7 @@ func TestIDManager_Set(t *testing.T) {

concurrency := 1000

nextKeyPtr, err := m.nextKey()
nextKeyPtr, _, err := m.reserveNextID()
require.NoError(t, err)

nextKey := *nextKeyPtr
Expand Down Expand Up @@ -212,7 +213,7 @@ func TestIDManager_Get(t *testing.T) {
prepManagerWithVal := func(v interface{}) (*idManager, uint16) {
m := newIDManager()

nextKey, err := m.nextKey()
nextKey, _, err := m.reserveNextID()
require.NoError(t, err)

err = m.set(*nextKey, v)
Expand Down
4 changes: 3 additions & 1 deletion pkg/app2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func (l *Listener) Accept() (net.Conn, error) {
func (l *Listener) Close() error {
defer func() {
l.freePort()
l.freeLis()
if l.freeLis != nil {
l.freeLis()
}

var conns []net.Conn
l.cm.doRange(func(_ uint16, v interface{}) bool {
Expand Down
24 changes: 15 additions & 9 deletions pkg/app2/mock_rpc_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions pkg/app2/network/mock_networker.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3a9fdba

Please sign in to comment.