Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 21, 2019
1 parent eb42bca commit 0714e55
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 2 deletions.
107 changes: 107 additions & 0 deletions pkg/app2/network/mock_networker.go

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

6 changes: 4 additions & 2 deletions pkg/app2/network/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
)

//go:generate mockery -name Networker -case underscore -inpkg

var (
// ErrNoSuchNetworker is being returned when there's no suitable networker.
ErrNoSuchNetworker = errors.New("no such networker")
Expand All @@ -15,11 +17,11 @@ var (
)

var (
networkers = map[Type]Networker{}
networkers = make(map[Type]Networker)
networkersMx sync.RWMutex
)

// AddNetworker associated Networker with the `network`.
// AddNetworker associates Networker with the `network`.
func AddNetworker(t Type, n Networker) error {
networkersMx.Lock()
defer networkersMx.Unlock()
Expand Down
123 changes: 123 additions & 0 deletions pkg/app2/network/networker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package network

import (
"context"
"net"
"testing"

"github.com/skycoin/skywire/pkg/routing"

"github.com/skycoin/dmsg/cipher"

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

func TestAddNetworker(t *testing.T) {
clearNetworkers()

nType := Type(TypeDMSG)
var n Networker

err := AddNetworker(nType, n)
require.NoError(t, err)

err = AddNetworker(nType, n)
require.Equal(t, err, ErrNetworkerAlreadyExists)
}

func TestResolveNetworker(t *testing.T) {
clearNetworkers()

nType := Type(TypeDMSG)
var n Networker

n, err := ResolveNetworker(nType)
require.Equal(t, err, ErrNoSuchNetworker)

err = AddNetworker(nType, n)
require.NoError(t, err)

gotN, err := ResolveNetworker(nType)
require.NoError(t, err)
require.Equal(t, gotN, n)
}

func TestDial(t *testing.T) {
addr := prepAddr()

t.Run("no such networker", func(t *testing.T) {
clearNetworkers()

_, err := Dial(addr)
require.Equal(t, err, ErrNoSuchNetworker)
})

t.Run("ok", func(t *testing.T) {
clearNetworkers()

dialCtx := context.Background()
var (
dialConn net.Conn
dialErr error
)

n := &MockNetworker{}
n.On("DialContext", dialCtx, addr).Return(dialConn, dialErr)

err := AddNetworker(addr.Net, n)
require.NoError(t, err)

conn, err := Dial(addr)
require.NoError(t, err)
require.Equal(t, conn, dialConn)
})
}

func TestListen(t *testing.T) {
addr := prepAddr()

t.Run("no such networker", func(t *testing.T) {
clearNetworkers()

_, err := Listen(addr)
require.Equal(t, err, ErrNoSuchNetworker)
})

t.Run("ok", func(t *testing.T) {
clearNetworkers()

listenCtx := context.Background()
var (
listenLis net.Listener
listenErr error
)

n := &MockNetworker{}
n.On("ListenContext", listenCtx, addr).Return(listenLis, listenErr)

err := AddNetworker(addr.Net, n)
require.NoError(t, err)

lis, err := Listen(addr)
require.NoError(t, err)
require.Equal(t, lis, listenLis)
})
}

func prepAddr() Addr {
addrPK, _ := cipher.GenerateKeyPair()
addrPort := routing.Port(100)

return Addr{
Net: TypeDMSG,
PubKey: addrPK,
Port: addrPort,
}
}

func clearNetworkers() {
networkersMx.Lock()
defer networkersMx.Unlock()

networkers = make(map[Type]Networker)
}
32 changes: 32 additions & 0 deletions pkg/app2/network/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package network

import (
"testing"

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

func TestType_IsValid(t *testing.T) {
tt := []struct {
name string
t Type
want bool
}{
{
name: "valid",
t: TypeDMSG,
want: true,
},
{
name: "not valid",
t: "not valid",
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
valid := tc.t.IsValid()
require.Equal(t, tc.want, valid)
})
}
}

0 comments on commit 0714e55

Please sign in to comment.