Skip to content

Commit

Permalink
Modify router interface
Browse files Browse the repository at this point in the history
- `IntroduceRules` added to the interface
- `SaveRoutingRules` got exported and added to the interface
- Router in the RPCGateway is of interface type now to be more testable
  • Loading branch information
Darkren committed Nov 13, 2019
1 parent a227415 commit 5aabda1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
11 changes: 8 additions & 3 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type Router interface {
// - Return the RoutingGroup.
AcceptRoutes(context.Context) (*RouteGroup, error)

SaveRoutingRules(rules ...routing.Rule) error

IntroduceRules(rules routing.EdgeRules) error

Serve(context.Context) error

SetupIsTrusted(cipher.PubKey) bool
Expand Down Expand Up @@ -178,7 +182,7 @@ func (r *router) DialRoutes(ctx context.Context, rPK cipher.PubKey, lPort, rPort
return nil, err
}

if err := r.saveRoutingRules(rules.Forward, rules.Reverse); err != nil {
if err := r.SaveRoutingRules(rules.Forward, rules.Reverse); err != nil {
return nil, err
}

Expand All @@ -201,7 +205,7 @@ func (r *router) AcceptRoutes(ctx context.Context) (*RouteGroup, error) {
break
}

if err := r.saveRoutingRules(rules.Forward, rules.Reverse); err != nil {
if err := r.SaveRoutingRules(rules.Forward, rules.Reverse); err != nil {
return nil, err
}

Expand Down Expand Up @@ -453,7 +457,8 @@ func (r *router) SetupIsTrusted(sPK cipher.PubKey) bool {
return ok
}

func (r *router) saveRoutingRules(rules ...routing.Rule) error {
// Saves `rules` to the routing table.
func (r *router) SaveRoutingRules(rules ...routing.Rule) error {
for _, rule := range rules {
if err := r.rt.SaveRule(rule); err != nil {
return fmt.Errorf("routing table: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/router/rpc_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

type RPCGateway struct {
logger *logging.Logger
router *router
router Router
}

func NewRPCGateway(router *router) *RPCGateway {
func NewRPCGateway(router Router) *RPCGateway {
return &RPCGateway{
logger: logging.MustGetLogger("router-gateway"),
router: router,
Expand Down
43 changes: 36 additions & 7 deletions pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@
package setup

import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/rpc"
"os"
"sync"
"sync/atomic"
"testing"
"time"

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

"github.com/SkycoinProject/skywire-mainnet/internal/skyenv"
"github.com/SkycoinProject/skywire-mainnet/pkg/metrics"
"github.com/SkycoinProject/skywire-mainnet/pkg/routing"
"github.com/google/uuid"

"github.com/SkycoinProject/dmsg"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/dmsg/disc"
Expand Down Expand Up @@ -48,7 +61,7 @@ func TestMain(m *testing.M) {
// 3. Hanging may be not the problem of the DMSG. Probably some of the communication part here is wrong.
// The reason I think so is that - if we ensure read timeouts, why doesn't this test constantly fail?
// Maybe some wrapper for DMSG is wrong, or some internal operations before the actual communication behave bad
/*func TestNode(t *testing.T) {
func TestNode(t *testing.T) {
// Prepare mock dmsg discovery.
discovery := disc.NewMock()

Expand All @@ -61,14 +74,22 @@ func TestMain(m *testing.M) {

type clientWithDMSGAddrAndListener struct {
*dmsg.Client
Addr dmsg.Addr
Listener *dmsg.Listener
Addr dmsg.Addr
Listener *dmsg.Listener
RPCServer *rpc.Server
Router router.Router
}

// CLOSURE: sets up dmsg clients.
prepClients := func(n int) ([]clientWithDMSGAddrAndListener, func()) {
prepClients := func(n, dstIdx int) ([]clientWithDMSGAddrAndListener, func()) {
if dstIdx >= n {
dstIdx = n - 1
}

clients := make([]clientWithDMSGAddrAndListener, n)
for i := 0; i < n; i++ {
r := &router.MockRouter{}

var port uint16
// setup node
if i == 0 {
Expand Down Expand Up @@ -170,6 +191,14 @@ func TestMain(m *testing.M) {

var addRuleDone sync.WaitGroup
var nextRouteID uint32
expectAddIntermediaryRules := func(client int, expRule routing.RuleType) {
conn, err := clients[client].Listener.Accept()
require.NoError(t, err)

fmt.Printf("client %v:%v accepted\n", client, clients[client].Addr)

go clients[client].RPCServer.ServeConn(conn)
}
// CLOSURE: emulates how a visor node should react when expecting an AddRules packet.
expectAddRules := func(client int, expRule routing.RuleType) {
conn, err := clients[client].Listener.Accept()
Expand Down Expand Up @@ -280,7 +309,7 @@ func TestMain(m *testing.M) {
// TEST: Emulates the communication between 2 visor nodes and a setup nodes,
// where a route is already established,
// and the first client attempts to tear it down.
t.Run("CloseLoop", func(t *testing.T) {
/*t.Run("CloseLoop", func(t *testing.T) {
// client index 0 is for setup node.
// clients index 1 and 2 are for visor nodes.
clients, closeClients := prepClients(3)
Expand Down Expand Up @@ -343,8 +372,8 @@ func TestMain(m *testing.M) {
// TODO: This error is not checked due to a bug in dmsg.
err = proto.WritePacket(RespSuccess, nil)
_ = err
})
}*/
})*/
}

func createServer(t *testing.T, dc disc.APIClient) (srv *dmsg.Server, srvErr <-chan error) {
pk, sk, err := cipher.GenerateDeterministicKeyPair([]byte("s"))
Expand Down

0 comments on commit 5aabda1

Please sign in to comment.