Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 21, 2019
1 parent 25489ef commit eb42bca
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 102 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/skycoin/skycoin v0.26.0
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5 // indirect
go.etcd.io/bbolt v1.3.3
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7
golang.org/x/net v0.0.0-20190916140828-c8589233b77d
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5 h1:Xim2mBRFdXzXmKRO8DJg/FJtn/8Fj9NOEpO6+WuMPmk=
github.com/vektra/mockery v0.0.0-20181123154057-e78b021dcbb5/go.mod h1:ppEjwdhyy7Y31EnHRDm1JkChoC7LXIJ7Ex0VYLWtZtQ=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3 h1:MUGmc65QhB3pIlaQ5bB4LwqSj6GIonVJXpZiaKNyaKk=
Expand Down Expand Up @@ -165,6 +167,7 @@ golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181112210238-4b1f3b6b1646/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190627182818-9947fec5c3ab/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Expand Down
11 changes: 5 additions & 6 deletions pkg/app2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ import (
"context"
"net"

"github.com/skycoin/skywire/pkg/app2/network"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/netutil"

"github.com/skycoin/skywire/pkg/app2/network"
"github.com/skycoin/skywire/pkg/routing"
)

// Client is used by skywire apps.
type Client struct {
pk cipher.PubKey
pid ProcID
rpc ServerRPCClient
rpc RPCClient
porter *netutil.Porter
}

// NewClient creates a new `Client`. The `Client` needs to be provided with:
// - log: Logger instance.
// - localPK: The local public key of the parent skywire visor.
// - pid: The procID assigned for the process that Client is being used by.
// - rpc: RPC client to communicate with the server.
func NewClient(localPK cipher.PubKey, pid ProcID, rpc ServerRPCClient, porter *netutil.Porter) *Client {
func NewClient(localPK cipher.PubKey, pid ProcID, rpc RPCClient) *Client {
return &Client{
pk: localPK,
pid: pid,
rpc: rpc,
porter: porter,
porter: netutil.NewPorter(netutil.PorterMinEphemeral),
}
}

Expand Down
40 changes: 18 additions & 22 deletions pkg/app2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package app2
import (
"testing"

"github.com/skycoin/skywire/pkg/app2/network"

"github.com/pkg/errors"
"github.com/skycoin/dmsg/cipher"
"github.com/stretchr/testify/require"

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

"github.com/skycoin/dmsg/cipher"

"github.com/skycoin/dmsg/netutil"
)

func TestClient_Dial(t *testing.T) {
Expand All @@ -30,10 +26,10 @@ func TestClient_Dial(t *testing.T) {
dialConnID := uint16(1)
var dialErr error

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

cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral))
cl := NewClient(localPK, pid, rpc)

wantConn := &Conn{
id: dialConnID,
Expand Down Expand Up @@ -62,10 +58,10 @@ func TestClient_Dial(t *testing.T) {
t.Run("dial error", func(t *testing.T) {
dialErr := errors.New("dial error")

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

cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral))
cl := NewClient(localPK, pid, rpc)

conn, err := cl.Dial(remote)
require.Equal(t, dialErr, err)
Expand All @@ -79,6 +75,7 @@ func TestClient_Listen(t *testing.T) {

port := routing.Port(1)
local := network.Addr{
Net: network.TypeDMSG,
PubKey: localPK,
Port: port,
}
Expand All @@ -87,18 +84,18 @@ func TestClient_Listen(t *testing.T) {
listenLisID := uint16(1)
var listenErr error

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

cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral))
cl := NewClient(localPK, pid, rpc)

wantListener := &Listener{
id: listenLisID,
rpc: rpc,
addr: local,
}

listener, err := cl.Listen(port)
listener, err := cl.Listen(network.TypeDMSG, port)
require.Nil(t, err)
appListener, ok := listener.(*Listener)
require.True(t, ok)
Expand All @@ -112,30 +109,29 @@ func TestClient_Listen(t *testing.T) {
})

t.Run("port is already bound", func(t *testing.T) {
porter := netutil.NewPorter(netutil.PorterMinEphemeral)
ok, _ := porter.Reserve(uint16(port), nil)
require.True(t, ok)
rpc := &MockRPCClient{}

rpc := &MockServerRPCClient{}
cl := NewClient(localPK, pid, rpc)

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

wantErr := ErrPortAlreadyBound

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

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

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

cl := NewClient(localPK, pid, rpc, netutil.NewPorter(netutil.PorterMinEphemeral))
cl := NewClient(localPK, pid, rpc)

listener, err := cl.Listen(port)
listener, err := cl.Listen(network.TypeDMSG, port)
require.Equal(t, listenErr, err)
require.Nil(t, listener)
_, ok := cl.porter.PortValue(uint16(port))
Expand Down
2 changes: 1 addition & 1 deletion pkg/app2/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// Implements `net.Conn`.
type Conn struct {
id uint16
rpc ServerRPCClient
rpc RPCClient
local network.Addr
remote network.Addr
freeLocalPort func()
Expand Down
6 changes: 3 additions & 3 deletions pkg/app2/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestConn_Read(t *testing.T) {

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rpc := &MockServerRPCClient{}
rpc := &MockRPCClient{}
rpc.On("Read", connID, tc.readBuff).Return(tc.readN, tc.readBytes, tc.readErr)

conn := &Conn{
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestConn_Write(t *testing.T) {

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rpc := &MockServerRPCClient{}
rpc := &MockRPCClient{}
rpc.On("Write", connID, tc.writeBuff).Return(tc.writeN, tc.writeErr)

conn := &Conn{
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestConn_Close(t *testing.T) {

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rpc := &MockServerRPCClient{}
rpc := &MockRPCClient{}
rpc.On("CloseConn", connID).Return(tc.closeErr)

conn := &Conn{
Expand Down
2 changes: 1 addition & 1 deletion pkg/app2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// Implements `net.Listener`.
type Listener struct {
id uint16
rpc ServerRPCClient
rpc RPCClient
addr network.Addr
freePort func()
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/app2/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ import (
"testing"

"github.com/pkg/errors"
"github.com/skycoin/dmsg/cipher"
"github.com/stretchr/testify/require"

"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/skywire/pkg/app2/network"
"github.com/skycoin/skywire/pkg/routing"
)

func TestListener_Accept(t *testing.T) {
lisID := uint16(1)
localPK, _ := cipher.GenerateKeyPair()
local := routing.Addr{
local := network.Addr{
Net: network.TypeDMSG,
PubKey: localPK,
Port: routing.Port(100),
}

t.Run("ok", func(t *testing.T) {
acceptConnID := uint16(1)
remotePK, _ := cipher.GenerateKeyPair()
acceptRemote := routing.Addr{
acceptRemote := network.Addr{
Net: network.TypeDMSG,
PubKey: remotePK,
Port: routing.Port(100),
}
var acceptErr error

rpc := &MockServerRPCClient{}
rpc := &MockRPCClient{}
rpc.On("Accept", acceptConnID).Return(acceptConnID, acceptRemote, acceptErr)

lis := &Listener{
Expand All @@ -50,10 +53,10 @@ func TestListener_Accept(t *testing.T) {

t.Run("accept error", func(t *testing.T) {
acceptConnID := uint16(0)
acceptRemote := routing.Addr{}
acceptRemote := network.Addr{}
acceptErr := errors.New("accept error")

rpc := &MockServerRPCClient{}
rpc := &MockRPCClient{}
rpc.On("Accept", lisID).Return(acceptConnID, acceptRemote, acceptErr)

lis := &Listener{
Expand All @@ -71,7 +74,8 @@ func TestListener_Accept(t *testing.T) {
func TestListener_Close(t *testing.T) {
lisID := uint16(1)
localPK, _ := cipher.GenerateKeyPair()
local := routing.Addr{
local := network.Addr{
Net: network.TypeDMSG,
PubKey: localPK,
Port: routing.Port(100),
}
Expand All @@ -91,7 +95,7 @@ func TestListener_Close(t *testing.T) {

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
rpc := &MockServerRPCClient{}
rpc := &MockRPCClient{}
rpc.On("CloseListener", lisID).Return(tc.closeErr)

lis := &Listener{
Expand Down
24 changes: 13 additions & 11 deletions pkg/app2/mock_server_rpc_client.go → pkg/app2/mock_rpc_client.go

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

3 changes: 2 additions & 1 deletion pkg/app2/network/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/skycoin/dmsg/cipher"

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

Expand All @@ -14,7 +15,7 @@ type Addr struct {
Port routing.Port
}

// Network returns "dmsg"
// Network returns network type.
func (a Addr) Network() string {
return string(a.Net)
}
Expand Down
Loading

0 comments on commit eb42bca

Please sign in to comment.