Skip to content

Commit

Permalink
Start doing TestConn for app
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Dec 16, 2019
1 parent 01b7a70 commit e69a1b3
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 39 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ require (
golang.org/x/net v0.0.0-20191204025024-5ee1b9f4859a
)

// replace github.com/SkycoinProject/dmsg => ../dmsg
replace github.com/SkycoinProject/dmsg => ../dmsg
2 changes: 1 addition & 1 deletion internal/therealproxy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Client) ListenAndServe(addr string) error {
return fmt.Errorf("listen: %s", err)
}

Log.Println("Listening therealproxy client")
Log.Printf("Listening therealproxy client on %s", addr)

c.listener = l
for {
Expand Down
197 changes: 197 additions & 0 deletions pkg/app/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@ package app

import (
"errors"
"fmt"
"net"
"net/rpc"
"testing"

"github.com/SkycoinProject/skywire-mainnet/pkg/routing"

"github.com/stretchr/testify/mock"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"

"golang.org/x/net/nettest"

"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/SkycoinProject/skywire-mainnet/internal/testhelpers"

"github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest"

"github.com/SkycoinProject/skywire-mainnet/pkg/app/appnet"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appserver"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/idmanager"

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

Expand Down Expand Up @@ -135,3 +155,180 @@ func TestConn_Close(t *testing.T) {
require.Equal(t, "conn is already closed", err.Error())
})
}

type wrappedConn struct {
net.Conn
local routing.Addr
remote routing.Addr
}

func wrapConn(conn net.Conn, local, remote routing.Addr) *wrappedConn {
return &wrappedConn{
Conn: conn,
local: local,
remote: remote,
}
}

func (p *wrappedConn) LocalAddr() net.Addr {
return p.local
}

func (p *wrappedConn) RemoteAddr() net.Addr {
return p.remote
}

func TestConn_TestConn(t *testing.T) {
mp := func() (net.Conn, net.Conn, func(), error) {
netType := appnet.TypeSkynet
keys := snettest.GenKeyPairs(2)
fmt.Printf("C1 Local: %s\n", keys[0].PK)
fmt.Printf("C2 Local: %s\n", keys[1].PK)
p1, p2 := net.Pipe()
a1 := appnet.Addr{
Net: appnet.Type(netType),
PubKey: keys[0].PK,
Port: 0,
}
a2 := appnet.Addr{
Net: appnet.Type(netType),
PubKey: keys[1].PK,
Port: 0,
}

ra1 := routing.Addr{
PubKey: a1.PubKey,
Port: a1.Port,
}
ra2 := routing.Addr{
PubKey: a2.PubKey,
Port: a2.Port,
}

wc1 := wrapConn(p1, ra1, ra2)
wc2 := wrapConn(p2, ra2, ra1)

n := &appnet.MockNetworker{}
n.On("DialContext", mock.Anything, a1).Return(wc1, testhelpers.NoErr)
n.On("DialContext", mock.Anything, a2).Return(wc2, testhelpers.NoErr)

appnet.ClearNetworkers()
err := appnet.AddNetworker(appnet.Type(netType), n)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)

rpcL, err := nettest.NewLocalListener("tcp")
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)

rpcS := rpc.NewServer()

appKeys := snettest.GenKeyPairs(2)

gateway1 := appserver.NewRPCGateway(logging.MustGetLogger("test_app_rpc_gateway1"))
gateway2 := appserver.NewRPCGateway(logging.MustGetLogger("test_app_rpc_gateway2"))
err = rpcS.RegisterName(appKeys[0].PK.Hex(), gateway1)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
err = rpcS.RegisterName(appKeys[1].PK.Hex(), gateway2)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)

go rpcS.Accept(rpcL)

rpcCl1, err := rpc.Dial(rpcL.Addr().Network(), rpcL.Addr().String())
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)

cl1 := Client{
log: logging.MustGetLogger("test_client_1"),
visorPK: keys[0].PK,
rpc: NewRPCClient(rpcCl1, appcommon.Key(appKeys[0].PK.Hex())),
lm: idmanager.New(),
cm: idmanager.New(),
}

rpcCl2, err := rpc.Dial(rpcL.Addr().Network(), rpcL.Addr().String())
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)

cl2 := Client{
log: logging.MustGetLogger("test_client_2"),
visorPK: keys[1].PK,
rpc: NewRPCClient(rpcCl2, appcommon.Key(appKeys[1].PK.Hex())),
lm: idmanager.New(),
cm: idmanager.New(),
}

c1, err := cl1.Dial(a2)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)

c2, err := cl2.Dial(a1)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)

stop := func() {
/*err := c1.Close()
require.NoError(t, err)
err = c2.Close()
require.NoError(t, err)
err = rpcL.Close()
require.NoError(t, err)*/
}

return c1, c2, stop, nil
}

/*payload := []byte("hello")
errC := make(chan error)
nC := make(chan int)
bufC := make(chan []byte)
go func() {
buf := make([]byte, 10)
n, err := c2.Read(buf)
fmt.Printf("Err is %v\n", err)
errC <- err
if err != nil {
return
}
nC <- n
bufC <- buf[:n]
}()
written, err := c1.Write(payload)
require.NoError(t, err)
require.Equal(t, len(payload), written)
require.NoError(t, <-errC)
close(errC)
require.Equal(t, len(payload), <-nC)
close(nC)
require.Equal(t, payload, <-bufC)
close(bufC)
err = c1.Close()
require.NoError(t, err)
err = c2.Close()
require.NoError(t, err)*/

nettest.TestConn(t, mp)
}
2 changes: 1 addition & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (r *router) handleDataPacket(ctx context.Context, packet routing.Packet) er
}

r.logger.Infof("Got new remote packet with route ID %d. Using rule: %s", packet.RouteID(), rule)
r.logger.Infof("Packet contents: %s", packet.Payload())
r.logger.Infof("Packet contents (len = %d): %v", len(packet.Payload()), packet.Payload())

if rg.isClosed() {
r.logger.Infoln("RG IS CLOSED")
Expand Down
4 changes: 3 additions & 1 deletion vendor/github.com/SkycoinProject/dmsg/go.mod

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

5 changes: 3 additions & 2 deletions vendor/github.com/SkycoinProject/dmsg/go.sum

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

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

This file was deleted.

6 changes: 3 additions & 3 deletions vendor/github.com/mattn/go-colorable/colorable_appengine.go

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

6 changes: 3 additions & 3 deletions vendor/github.com/mattn/go-colorable/colorable_others.go

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

Loading

0 comments on commit e69a1b3

Please sign in to comment.