Skip to content

Commit

Permalink
Add tests for router RPC client
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Nov 17, 2019
1 parent 04aa311 commit 89cd66a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 8 deletions.
6 changes: 0 additions & 6 deletions pkg/router/routerclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const rpcName = "RPCGateway"

// Client is an RPC client for router.
type Client struct {
s *dmsg.Stream
rpc *rpc.Client
}

Expand All @@ -30,7 +29,6 @@ func NewClient(ctx context.Context, dmsgC *dmsg.Client, pk cipher.PubKey) (*Clie
fmt.Printf("Dialed to %s\n", pk) // TODO: remove debug logging

client := &Client{
s: s,
rpc: rpc.NewClient(s.Conn),
}
return client, nil
Expand All @@ -42,10 +40,6 @@ func (c *Client) Close() error {
return nil
}

if err := c.s.Close(); err != nil {
return err
}

if err := c.rpc.Close(); err != nil {
return err
}
Expand Down
118 changes: 118 additions & 0 deletions pkg/router/routerclient/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package routerclient

import (
"context"
"net"
"net/rpc"
"testing"

"github.com/SkycoinProject/skywire-mainnet/internal/testhelpers"

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

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

"github.com/stretchr/testify/require"
"golang.org/x/net/nettest"
)

func TestClient_Close(t *testing.T) {
t.Run("ok", func(t *testing.T) {
r := &router.MockRouter{}
_, _, cleanup := prepRPCServerAndClient(t, r)
cleanup()
})

t.Run("ok - no panic on nil client", func(t *testing.T) {
var cl *Client
err := cl.Close()
require.NoError(t, err)
})
}

func TestClient_AddEdgeRules(t *testing.T) {
srcPK, _ := cipher.GenerateKeyPair()
dstPK, _ := cipher.GenerateKeyPair()
var srcPort, dstPort routing.Port = 100, 110

desc := routing.NewRouteDescriptor(srcPK, dstPK, srcPort, dstPort)

rules := routing.EdgeRules{
Desc: desc,
Forward: routing.Rule{0, 0, 0},
Reverse: routing.Rule{1, 1, 1},
}

r := &router.MockRouter{}
r.On("IntroduceRules", rules).Return(testhelpers.NoErr)
r.On("SaveRoutingRules", rules.Forward, rules.Reverse).Return(testhelpers.NoErr)

_, cl, cleanup := prepRPCServerAndClient(t, r)
defer cleanup()

ok, err := cl.AddEdgeRules(context.Background(), rules)
require.NoError(t, err)
require.True(t, ok)
}

func TestClient_AddIntermediaryRules(t *testing.T) {
rule1 := routing.Rule{0, 0, 0}
rule2 := routing.Rule{1, 1, 1}
rulesIfc := []interface{}{rule1, rule2}
rules := []routing.Rule{rule1, rule2}

r := &router.MockRouter{}
r.On("SaveRoutingRules", rulesIfc...).Return(testhelpers.NoErr)

_, cl, cleanup := prepRPCServerAndClient(t, r)
defer cleanup()

ok, err := cl.AddIntermediaryRules(context.Background(), rules)
require.NoError(t, err)
require.True(t, ok)
}

func TestClient_ReserveIDs(t *testing.T) {
var n uint8 = 5
ids := []routing.RouteID{1, 2, 3, 4, 5}

r := &router.MockRouter{}
r.On("ReserveKeys", int(n)).Return(ids, testhelpers.NoErr)

_, cl, cleanup := prepRPCServerAndClient(t, r)
defer cleanup()

gotIDs, err := cl.ReserveIDs(context.Background(), n)
require.NoError(t, err)
require.Equal(t, ids, gotIDs)
}

func prepRPCServerAndClient(t *testing.T, r router.Router) (s *rpc.Server, cl *Client, cleanup func()) {
l, err := nettest.NewLocalListener("tcp")
require.NoError(t, err)

gateway := router.NewRPCGateway(r)

s = rpc.NewServer()
err = s.Register(gateway)
require.NoError(t, err)
go s.Accept(l)

conn, err := net.Dial("tcp", l.Addr().String())
require.NoError(t, err)

cl = &Client{
rpc: rpc.NewClient(conn),
}

cleanup = func() {
err := cl.Close()
require.NoError(t, err)

err = l.Close()
require.NoError(t, err)
}

return s, cl, cleanup
}
4 changes: 2 additions & 2 deletions pkg/router/rpc_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestRPCGateway_AddEdgeRules(t *testing.T) {
rules := routing.EdgeRules{
Desc: desc,
Forward: routing.Rule{0, 0, 0},
Reverse: routing.Rule{0, 0, 0},
Reverse: routing.Rule{1, 1, 1},
}

t.Run("ok", func(t *testing.T) {
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestRPCGateway_AddEdgeRules(t *testing.T) {

func TestRPCGateway_AddIntermediaryRules(t *testing.T) {
rule1 := routing.Rule{0, 0, 0}
rule2 := routing.Rule{0, 0, 0}
rule2 := routing.Rule{1, 1, 1}
rulesIfc := []interface{}{rule1, rule2}
rules := []routing.Rule{rule1, rule2}

Expand Down

0 comments on commit 89cd66a

Please sign in to comment.