Skip to content

Commit

Permalink
Finish app RPC client tests for deadline methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Dec 17, 2019
1 parent 075ddc6 commit e0dda82
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 165 deletions.
25 changes: 19 additions & 6 deletions pkg/app/appserver/rpc_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appserver
import (
"context"
"errors"
"fmt"
"math"
"net"
"strings"
Expand Down Expand Up @@ -580,7 +581,9 @@ func testRPCGatewaySetWriteDeadlineOK(t *testing.T, l *logging.Logger, deadline

conn := &appcommon.MockConn{}
conn.On("SetWriteDeadline", mock.Anything).Return(func(d time.Time) error {
require.Equal(t, deadline, d)
if !deadline.Equal(d) {
return fmt.Errorf("expected deadline %v, got %v", deadline, d)
}

return nil
})
Expand Down Expand Up @@ -628,7 +631,9 @@ func testRPCGatewaySetWriteDeadlineError(t *testing.T, l *logging.Logger, deadli

conn := &appcommon.MockConn{}
conn.On("SetWriteDeadline", mock.Anything).Return(func(d time.Time) error {
require.Equal(t, deadline, d)
if !deadline.Equal(d) {
return fmt.Errorf("expected deadline %v, got %v", deadline, d)
}

return testhelpers.Err
})
Expand Down Expand Up @@ -670,7 +675,9 @@ func testRPCGatewaySetReadDeadlineOK(t *testing.T, l *logging.Logger, deadline t

conn := &appcommon.MockConn{}
conn.On("SetReadDeadline", mock.Anything).Return(func(d time.Time) error {
require.Equal(t, deadline, d)
if !deadline.Equal(d) {
return fmt.Errorf("expected deadline %v, got %v", deadline, d)
}

return nil
})
Expand Down Expand Up @@ -718,7 +725,9 @@ func testRPCGatewaySetReadDeadlineError(t *testing.T, l *logging.Logger, deadlin

conn := &appcommon.MockConn{}
conn.On("SetReadDeadline", mock.Anything).Return(func(d time.Time) error {
require.Equal(t, deadline, d)
if !deadline.Equal(d) {
return fmt.Errorf("expected deadline %v, got %v", deadline, d)
}

return testhelpers.Err
})
Expand Down Expand Up @@ -760,7 +769,9 @@ func testRPCGatewaySetDeadlineOK(t *testing.T, l *logging.Logger, deadline time.

conn := &appcommon.MockConn{}
conn.On("SetDeadline", mock.Anything).Return(func(d time.Time) error {
require.Equal(t, deadline, d)
if !deadline.Equal(d) {
return fmt.Errorf("expected deadline %v, got %v", deadline, d)
}

return nil
})
Expand Down Expand Up @@ -808,7 +819,9 @@ func testRPCGatewaySetDeadlineError(t *testing.T, l *logging.Logger, deadline ti

conn := &appcommon.MockConn{}
conn.On("SetDeadline", mock.Anything).Return(func(d time.Time) error {
require.Equal(t, deadline, d)
if !deadline.Equal(d) {
return fmt.Errorf("expected deadline %v, got %v", deadline, d)
}

return testhelpers.Err
})
Expand Down
282 changes: 135 additions & 147 deletions pkg/app/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,11 @@ package app

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

"github.com/SkycoinProject/skycoin/src/util/logging"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"golang.org/x/net/nettest"

"github.com/SkycoinProject/skywire-mainnet/internal/testhelpers"
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"
"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/SkycoinProject/skywire-mainnet/pkg/routing"
"github.com/SkycoinProject/skywire-mainnet/pkg/snet/snettest"
"github.com/stretchr/testify/require"
)

func TestConn_Read(t *testing.T) {
Expand Down Expand Up @@ -172,157 +160,157 @@ 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,
}
/*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,
}
wc1 := wrapConn(p1, ra1, ra2)
wc2 := wrapConn(p2, ra2, ra1)
ra1 := routing.Addr{
PubKey: a1.PubKey,
Port: a1.Port,
}
ra2 := routing.Addr{
PubKey: a2.PubKey,
Port: a2.Port,
}
n := &appnet.MockNetworker{}
n.On("DialContext", mock.Anything, a1).Return(wc1, testhelpers.NoErr)
n.On("DialContext", mock.Anything, a2).Return(wc2, testhelpers.NoErr)
wc1 := wrapConn(p1, ra1, ra2)
wc2 := wrapConn(p2, ra2, ra1)
appnet.ClearNetworkers()
err := appnet.AddNetworker(appnet.Type(netType), n)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
n := &appnet.MockNetworker{}
n.On("DialContext", mock.Anything, a1).Return(wc1, testhelpers.NoErr)
n.On("DialContext", mock.Anything, a2).Return(wc2, testhelpers.NoErr)
rpcL, err := nettest.NewLocalListener("tcp")
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
appnet.ClearNetworkers()
err := appnet.AddNetworker(appnet.Type(netType), n)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
rpcS := rpc.NewServer()
rpcL, err := nettest.NewLocalListener("tcp")
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
appKeys := snettest.GenKeyPairs(2)
rpcS := rpc.NewServer()
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)
appKeys := snettest.GenKeyPairs(2)
go rpcS.Accept(rpcL)
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)
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(),
}
go rpcS.Accept(rpcL)
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(),
}
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(),
}
c1, err := cl1.Dial(a2)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
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(),
}
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)*/
}
c1, err := cl1.Dial(a2)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
return c1, c2, stop, nil
c2, err := cl2.Dial(a1)
if err != nil {
return nil, nil, nil, err
}
//require.NoError(t, err)
/*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
}
stop := func() {
/*err := c1.Close()
require.NoError(t, err)
err = c2.Close()
require.NoError(t, err)
err = rpcL.Close()
require.NoError(t, err)*/
/*}
nC <- n
bufC <- buf[:n]
}()
return c1, c2, stop, nil
}
written, err := c1.Write(payload)
require.NoError(t, err)
require.Equal(t, len(payload), written)
/*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
}
require.NoError(t, <-errC)
close(errC)
require.Equal(t, len(payload), <-nC)
close(nC)
require.Equal(t, payload, <-bufC)
close(bufC)
nC <- n
bufC <- buf[:n]
}()
err = c1.Close()
require.NoError(t, err)
err = c2.Close()
require.NoError(t, err)*/
written, err := c1.Write(payload)
require.NoError(t, err)
require.Equal(t, len(payload), written)
nettest.TestConn(t, mp)
}
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)
}*/
Loading

0 comments on commit e0dda82

Please sign in to comment.