Skip to content

Commit

Permalink
And more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Sep 22, 2019
1 parent 3491994 commit 07d5b80
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 36 deletions.
141 changes: 141 additions & 0 deletions pkg/app2/mock_conn.go

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

65 changes: 65 additions & 0 deletions pkg/app2/mock_listener.go

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

64 changes: 64 additions & 0 deletions pkg/app2/network/dmsg_conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package network

import (
"net"
"time"

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

"github.com/skycoin/dmsg"
)

type DMSGConn struct {
tp *dmsg.Transport
}

func (c *DMSGConn) Read(b []byte) (n int, err error) {
return c.tp.Read(b)
}

func (c *DMSGConn) Write(b []byte) (n int, err error) {
return c.tp.Write(b)
}

func (c *DMSGConn) Close() error {
return c.tp.Close()
}

func (c *DMSGConn) LocalAddr() net.Addr {
dmsgAddr, ok := c.tp.LocalAddr().(dmsg.Addr)
if !ok {
return c.tp.LocalAddr()
}

return Addr{
Net: TypeDMSG,
PubKey: dmsgAddr.PK,
Port: routing.Port(dmsgAddr.Port),
}
}

func (c *DMSGConn) RemoteAddr() net.Addr {
dmsgAddr, ok := c.tp.RemoteAddr().(dmsg.Addr)
if !ok {
return c.tp.RemoteAddr()
}

return Addr{
Net: TypeDMSG,
PubKey: dmsgAddr.PK,
Port: routing.Port(dmsgAddr.Port),
}
}

func (c *DMSGConn) SetDeadline(t time.Time) error {
return c.tp.SetDeadline(t)
}

func (c *DMSGConn) SetReadDeadline(t time.Time) error {
return c.tp.SetReadDeadline(t)
}

func (c *DMSGConn) SetWriteDeadline(t time.Time) error {
return c.tp.SetWriteDeadline(t)
}
7 changes: 6 additions & 1 deletion pkg/app2/network/dmsg_networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func (n *DMSGNetworker) Dial(addr Addr) (net.Conn, error) {

// DialContext dials remote `addr` via dmsg network with context.
func (n *DMSGNetworker) DialContext(ctx context.Context, addr Addr) (net.Conn, error) {
return n.dmsgC.Dial(ctx, addr.PubKey, uint16(addr.Port))
tp, err := n.dmsgC.Dial(ctx, addr.PubKey, uint16(addr.Port))
if err != nil {
return nil, err
}

return &DMSGConn{tp: tp}, nil
}

// Listen starts listening on local `addr` in the dmsg network.
Expand Down
8 changes: 8 additions & 0 deletions pkg/app2/network/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func ResolveNetworker(t Type) (Networker, error) {
return n, nil
}

// ClearNetworkers removes all the stored networkers.
func ClearNetworkers() {
networkersMx.Lock()
defer networkersMx.Unlock()

networkers = make(map[Type]Networker)
}

// Networker defines basic network operations, such as Dial/Listen.
type Networker interface {
Dial(addr Addr) (net.Conn, error)
Expand Down
24 changes: 8 additions & 16 deletions pkg/app2/network/networker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"net"
"testing"

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

"github.com/skycoin/dmsg/cipher"

"github.com/stretchr/testify/require"

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

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

nType := TypeDMSG
var n Networker
Expand All @@ -26,7 +25,7 @@ func TestAddNetworker(t *testing.T) {
}

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

nType := TypeDMSG
var n Networker
Expand All @@ -46,14 +45,14 @@ func TestDial(t *testing.T) {
addr := prepAddr()

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

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

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

dialCtx := context.Background()
var (
Expand All @@ -77,14 +76,14 @@ func TestListen(t *testing.T) {
addr := prepAddr()

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

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

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

listenCtx := context.Background()
var (
Expand Down Expand Up @@ -114,10 +113,3 @@ func prepAddr() Addr {
Port: addrPort,
}
}

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

networkers = make(map[Type]Networker)
}
Loading

0 comments on commit 07d5b80

Please sign in to comment.