Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 25, 2019
1 parent 17ba146 commit d560351
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 96 deletions.
94 changes: 72 additions & 22 deletions pkg/app2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ func TestClient_Dial(t *testing.T) {
require.Equal(t, wantConn.local, appConn.local)
require.Equal(t, wantConn.remote, appConn.remote)
require.NotNil(t, appConn.freeConn)

cmConnIfc, ok := cl.cm.values[appConn.id]
require.True(t, ok)
require.NotNil(t, cmConnIfc)

cmConn, ok := cmConnIfc.(*Conn)
require.True(t, ok)
require.NotNil(t, cmConn.freeConn)
})

t.Run("conn already exists", func(t *testing.T) {
Expand Down Expand Up @@ -105,7 +113,7 @@ func TestClient_Dial(t *testing.T) {
dialErr := errors.New("dial error")

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

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

Expand Down Expand Up @@ -151,26 +159,7 @@ func TestClient_Listen(t *testing.T) {
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)
})

t.Run("port is already bound", func(t *testing.T) {
rpc := &MockRPCClient{}

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

ok, _ := cl.porter.Reserve(uint16(port), nil)
require.True(t, ok)

wantErr := ErrPortAlreadyBound

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

t.Run("listener already exists", func(t *testing.T) {
Expand Down Expand Up @@ -224,7 +213,68 @@ func TestClient_Listen(t *testing.T) {
listener, err := cl.Listen(network.TypeDMSG, port)
require.Equal(t, listenErr, err)
require.Nil(t, listener)
_, ok := cl.porter.PortValue(uint16(port))
require.False(t, ok)
})
}

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

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

rpc := &MockRPCClient{}

lisID1 := uint16(1)
lisID2 := uint16(2)

rpc.On("CloseListener", lisID1).Return(closeNoErr)
rpc.On("CloseListener", lisID2).Return(closeErr)

lm := newIDManager()

lis1 := &Listener{id: lisID1, rpc: rpc, cm: newIDManager()}
freeLis1, err := lm.add(lisID1, lis1)
require.NoError(t, err)
lis1.freeLis = freeLis1

lis2 := &Listener{id: lisID2, rpc: rpc, cm: newIDManager()}
freeLis2, err := lm.add(lisID2, lis2)
require.NoError(t, err)
lis2.freeLis = freeLis2

connID1 := uint16(1)
connID2 := uint16(2)

rpc.On("CloseConn", connID1).Return(closeNoErr)
rpc.On("CloseConn", connID2).Return(closeErr)

cm := newIDManager()

conn1 := &Conn{id: connID1, rpc: rpc}
freeConn1, err := cm.add(connID1, conn1)
require.NoError(t, err)
conn1.freeConn = freeConn1

conn2 := &Conn{id: connID2, rpc: rpc}
freeConn2, err := cm.add(connID2, conn2)
require.NoError(t, err)
conn2.freeConn = freeConn2

cl := NewClient(l, localPK, pid, rpc)
cl.cm = cm
cl.lm = lm

cl.Close()

_, ok := lm.values[lisID1]
require.False(t, ok)
_, ok = lm.values[lisID2]
require.False(t, ok)

_, ok = cm.values[connID1]
require.False(t, ok)
_, ok = cm.values[connID2]
require.False(t, ok)
}
141 changes: 140 additions & 1 deletion pkg/app2/id_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package app2

import (
"math"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

func TestIDManager_NextID(t *testing.T) {
func TestIDManager_ReserveNextID(t *testing.T) {
t.Run("simple call", func(t *testing.T) {
m := newIDManager()

Expand Down Expand Up @@ -133,6 +134,70 @@ func TestIDManager_Pop(t *testing.T) {
})
}

func TestIDManager_Add(t *testing.T) {
t.Run("simple call", func(t *testing.T) {
m := newIDManager()

id := uint16(1)
v := "value"

free, err := m.add(id, v)
require.Nil(t, err)
require.NotNil(t, free)

gotV, ok := m.values[id]
require.True(t, ok)
require.Equal(t, gotV, v)

v2 := "value2"

free, err = m.add(id, v2)
require.Equal(t, err, errValueAlreadyExists)
require.Nil(t, free)

gotV, ok = m.values[id]
require.True(t, ok)
require.Equal(t, gotV, v)
})

t.Run("concurrent run", func(t *testing.T) {
m := newIDManager()

id := uint16(1)

concurrency := 1000

addV := make(chan int)
errs := make(chan error)
for i := 0; i < concurrency; i++ {
go func(v int) {
_, err := m.add(id, v)
errs <- err
if err == nil {
addV <- v
}
}(i)
}

errsCount := 0
for i := 0; i < concurrency; i++ {
if err := <-errs; err != nil {
errsCount++
}
}
close(errs)

v := <-addV
close(addV)

require.Equal(t, concurrency-1, errsCount)

gotV, ok := m.values[id]
require.True(t, ok)
require.Equal(t, gotV, v)
})
}

func TestIDManager_Set(t *testing.T) {
t.Run("simple call", func(t *testing.T) {
m := newIDManager()
Expand Down Expand Up @@ -208,6 +273,8 @@ func TestIDManager_Set(t *testing.T) {
v := <-setV
close(setV)

require.Equal(t, concurrency-1, errsCount)

gotV, ok := m.values[nextID]
require.True(t, ok)
require.Equal(t, gotV, v)
Expand Down Expand Up @@ -274,3 +341,75 @@ func TestIDManager_Get(t *testing.T) {
close(res)
})
}

func TestIDManager_DoRange(t *testing.T) {
m := newIDManager()

valsCount := 5

vals := make([]int, 0, valsCount)
for i := 0; i < valsCount; i++ {
vals = append(vals, i)
}

for i, v := range vals {
_, err := m.add(uint16(i), v)
require.NoError(t, err)
}

// run full range
gotVals := make([]int, 0, valsCount)
m.doRange(func(_ uint16, v interface{}) bool {
val, ok := v.(int)
require.True(t, ok)

gotVals = append(gotVals, val)

return true
})
sort.Ints(gotVals)
require.Equal(t, gotVals, vals)

// run part range
var gotVal int
gotValsCount := 0
m.doRange(func(_ uint16, v interface{}) bool {
if gotValsCount == 1 {
return false
}

val, ok := v.(int)
require.True(t, ok)

gotVal = val

gotValsCount++

return true
})

found := false
for _, v := range vals {
if v == gotVal {
found = true
}
}
require.True(t, found)
}

func TestIDManager_ConstructFreeFunc(t *testing.T) {
m := newIDManager()

id := uint16(1)
v := "value"

free, err := m.add(id, v)
require.NoError(t, err)
require.NotNil(t, free)

free()

gotV, ok := m.values[id]
require.False(t, ok)
require.Nil(t, gotV)
}
Loading

0 comments on commit d560351

Please sign in to comment.