Skip to content

Commit

Permalink
Merge pull request #558 from evanlinjin/bug/fix-tests-uncomment
Browse files Browse the repository at this point in the history
Fix tests that can be fixed.
  • Loading branch information
志宇 authored Sep 6, 2019
2 parents 93efd86 + e785983 commit 958128d
Show file tree
Hide file tree
Showing 14 changed files with 619 additions and 589 deletions.
2 changes: 1 addition & 1 deletion cmd/skywire-cli/commands/node/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var addRuleCmd = &cobra.Command{
remotePort = routing.Port(parseUint("remote-port", args[3], 16))
localPort = routing.Port(parseUint("local-port", args[4], 16))
)
rule = routing.AppRule(keepAlive, routeID, remotePK, remotePort, localPort, 0)
rule = routing.AppRule(keepAlive, 0, routeID, remotePK, localPort, remotePort)
case "fwd":
var (
nextRouteID = routing.RouteID(parseUint("next-route-id", args[1], 32))
Expand Down
7 changes: 7 additions & 0 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func (app *App) Close() error {
// Accept awaits for incoming loop confirmation request from a Node and
// returns net.Conn for received loop.
func (app *App) Accept() (net.Conn, error) {
fmt.Println("!!! [ACCEPT] start !!!")
addrs := <-app.acceptChan
fmt.Println("!!! [ACCEPT] read from ch !!!")
laddr := addrs[0]
raddr := addrs[1]

Expand Down Expand Up @@ -187,6 +189,7 @@ func (app *App) Addr() net.Addr {

func (app *App) handleProto() {
err := app.proto.Serve(func(frame Frame, payload []byte) (res interface{}, err error) {
fmt.Printf("!!! app received frame: %s\n", frame)
switch frame {
case FrameConfirmLoop:
err = app.confirmLoop(payload)
Expand Down Expand Up @@ -242,6 +245,8 @@ func (app *App) forwardPacket(data []byte) error {
return err
}

fmt.Printf("!!! packet loop: %s\n", packet.Loop)

app.mu.Lock()
conn := app.conns[packet.Loop]
app.mu.Unlock()
Expand Down Expand Up @@ -272,6 +277,7 @@ func (app *App) closeConn(data []byte) error {
}

func (app *App) confirmLoop(data []byte) error {
fmt.Println("!!! [confirmLoop] !!!")
var addrs [2]routing.Addr
if err := json.Unmarshal(data, &addrs); err != nil {
return err
Expand All @@ -288,6 +294,7 @@ func (app *App) confirmLoop(data []byte) error {
return errors.New("loop is already created")
}

fmt.Println("!!! [confirmLoop] selecting !!!")
select {
case app.acceptChan <- addrs:
default:
Expand Down
8 changes: 5 additions & 3 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestNewRouteManager(t *testing.T) {
defer clearRules()

pk, _ := cipher.GenerateKeyPair()
rule := routing.AppRule(10*time.Minute, 3, pk, 3, 2, 1)
rule := routing.AppRule(10*time.Minute, 1, 3, pk, 2, 3)
_, err := rt.AddRule(rule)
require.NoError(t, err)

Expand Down Expand Up @@ -102,6 +102,8 @@ func TestNewRouteManager(t *testing.T) {
errCh <- rm.handleSetupConn(delOut) // Receive DeleteRule request.
close(errCh)
}()

// TODO: remove defer from for loop
defer func() {
require.NoError(t, requestIDIn.Close())
require.NoError(t, addIn.Close())
Expand Down Expand Up @@ -190,7 +192,7 @@ func TestNewRouteManager(t *testing.T) {

proto := setup.NewSetupProtocol(in)
pk, _ := cipher.GenerateKeyPair()
rule := routing.AppRule(10*time.Minute, 3, pk, 3, 2, 2)
rule := routing.AppRule(10*time.Minute, 2, 3, pk, 2, 3)
require.NoError(t, rt.SetRule(2, rule))

rule = routing.ForwardRule(10*time.Minute, 3, uuid.New(), 1)
Expand Down Expand Up @@ -242,7 +244,7 @@ func TestNewRouteManager(t *testing.T) {
proto := setup.NewSetupProtocol(in)
pk, _ := cipher.GenerateKeyPair()

rule := routing.AppRule(10*time.Minute, 3, pk, 3, 2, 0)
rule := routing.AppRule(10*time.Minute, 0, 3, pk, 2, 3)
require.NoError(t, rt.SetRule(2, rule))

rule = routing.ForwardRule(10*time.Minute, 3, uuid.New(), 1)
Expand Down
4 changes: 3 additions & 1 deletion pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ func (r *Router) handlePacket(ctx context.Context, packet routing.Packet) error

// ServeApp handles App packets from the App connection on provided port.
func (r *Router) ServeApp(conn net.Conn, port routing.Port, appConf *app.Config) error {
fmt.Println("!!! [ServeApp] start !!!")

r.wg.Add(1)
defer r.wg.Done()

Expand Down Expand Up @@ -229,7 +231,7 @@ func (r *Router) consumePacket(payload []byte, rule routing.Rule) error {
}
fmt.Println("got it!")
if err := b.conn.Send(app.FrameSend, p, nil); err != nil { // TODO: Stuck here.
fmt.Println("err:", err)
fmt.Println("!!! Send err:", err)
return err
}
fmt.Println("done")
Expand Down
46 changes: 27 additions & 19 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

routeFinder "github.com/skycoin/skywire/pkg/route-finder/client"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/snet"
"github.com/skycoin/skywire/pkg/snet/snettest"
Expand All @@ -36,7 +37,6 @@ func TestMain(m *testing.M) {

// Ensure that received packets are handled properly in `(*Router).Serve()`.
func TestRouter_Serve(t *testing.T) {

// We are generating two key pairs - one for the a `Router`, the other to send packets to `Router`.
keys := snettest.GenKeyPairs(2)

Expand All @@ -49,10 +49,10 @@ func TestRouter_Serve(t *testing.T) {
// Create routers
r0, err := New(nEnv.Nets[0], rEnv.GenRouterConfig(0))
require.NoError(t, err)
//go r0.Serve(context.TODO())
// go r0.Serve(context.TODO())
r1, err := New(nEnv.Nets[1], rEnv.GenRouterConfig(1))
require.NoError(t, err)
//go r1.Serve(context.TODO())
// go r1.Serve(context.TODO())

// Create dmsg transport between two `snet.Network` entities.
tp1, err := rEnv.TpMngrs[1].SaveTransport(context.TODO(), keys[0].PK, dmsg.Type)
Expand Down Expand Up @@ -95,6 +95,11 @@ func TestRouter_Serve(t *testing.T) {

// TODO(evanlinjin): I'm having so much trouble with this I officially give up.
//t.Run("handlePacket_appRule", func(t *testing.T) {
// const duration = 10 * time.Second
// // time.AfterFunc(duration, func() {
// // panic("timeout")
// // })
//
// defer clearRules(r0, r1)
//
// // prepare mock-app
Expand All @@ -109,34 +114,37 @@ func TestRouter_Serve(t *testing.T) {
// }
//
// // serve mock-app
// sErrCh := make(chan error, 1)
// // sErrCh := make(chan error, 1)
// go func() {
// sErrCh <- r0.ServeApp(sConn, localPort, appConf)
// close(sErrCh)
// }()
// defer func() {
// assert.NoError(t, cConn.Close())
// assert.NoError(t, <-sErrCh)
// // sErrCh <- r0.ServeApp(sConn, localPort, appConf)
// _ = r0.ServeApp(sConn, localPort, appConf)
// // close(sErrCh)
// }()
// // defer func() {
// // assert.NoError(t, cConn.Close())
// // assert.NoError(t, <-sErrCh)
// // }()
//
// a, err := app.New(cConn, appConf)
// require.NoError(t, err)
// cErrCh := make(chan error, 1)
// // cErrCh := make(chan error, 1)
// go func() {
// conn, err := a.Accept()
// if err == nil {
// fmt.Println("ACCEPTED:", conn.RemoteAddr())
// }
// fmt.Println("FAILED TO ACCEPT")
// cErrCh <- err
// close(cErrCh)
// }()
// defer func() {
// assert.NoError(t, <-cErrCh)
// // cErrCh <- err
// // close(cErrCh)
// }()
// a.Dial(a.Addr().(routing.Addr))
// // defer func() {
// // assert.NoError(t, <-cErrCh)
// // }()
//
// // Add a APP rule for r0.
// appRule := routing.AppRule(time.Now().Add(time.Hour), routing.RouteID(7), keys[1].PK, routing.Port(8), localPort)
// // port8 := routing.Port(8)
// appRule := routing.AppRule(10*time.Minute, 0, routing.RouteID(7), keys[1].PK, localPort, localPort)
// appRtID, err := r0.rm.rt.AddRule(appRule)
// require.NoError(t, err)
//
Expand All @@ -146,7 +154,7 @@ func TestRouter_Serve(t *testing.T) {
// // payload[0] = frame type, payload[1] = id
// rAddr := routing.Addr{PubKey: keys[1].PK, Port: localPort}
// rawRAddr, _ := json.Marshal(rAddr)
// //payload := append([]byte{byte(app.FrameClose), 0}, rawRAddr...)
// // payload := append([]byte{byte(app.FrameClose), 0}, rawRAddr...)
// packet := routing.MakePacket(appRtID, rawRAddr)
// require.NoError(t, r0.handlePacket(context.TODO(), packet))
//})
Expand Down Expand Up @@ -204,7 +212,7 @@ func (e *TestEnv) GenRouterConfig(i int) *Config {
SecKey: e.TpMngrConfs[i].SecKey,
TransportManager: e.TpMngrs[i],
RoutingTable: routing.InMemoryRoutingTable(),
RouteFinder: nil, // TODO
RouteFinder: routeFinder.NewMock(),
SetupNodes: nil, // TODO
GarbageCollectDuration: DefaultGarbageCollectDuration,
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/routing/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ type RuleSummary struct {
func (rs *RuleSummary) ToRule() (Rule, error) {
if rs.Type == RuleApp && rs.AppFields != nil && rs.ForwardFields == nil {
f := rs.AppFields
return AppRule(rs.KeepAlive, f.RespRID, f.RemotePK, f.RemotePort, f.LocalPort, rs.RequestRouteID), nil
return AppRule(rs.KeepAlive, rs.RequestRouteID, f.RespRID, f.RemotePK, f.LocalPort, f.RemotePort), nil
}
if rs.Type == RuleForward && rs.AppFields == nil && rs.ForwardFields != nil {
f := rs.ForwardFields
Expand Down Expand Up @@ -182,8 +182,7 @@ func (r Rule) Summary() *RuleSummary {
}

// AppRule constructs a new consume RoutingRule.
func AppRule(keepAlive time.Duration, respRoute RouteID, remotePK cipher.PubKey, remotePort, localPort Port,
requestRouteID RouteID) Rule {
func AppRule(keepAlive time.Duration, reqRoute, respRoute RouteID, remotePK cipher.PubKey, localPort, remotePort Port) Rule {
rule := make([]byte, RuleHeaderSize)

if keepAlive < 0 {
Expand All @@ -198,7 +197,7 @@ func AppRule(keepAlive time.Duration, respRoute RouteID, remotePK cipher.PubKey,
rule = append(rule, bytes.Repeat([]byte{0}, 8)...)
binary.BigEndian.PutUint16(rule[46:], uint16(remotePort))
binary.BigEndian.PutUint16(rule[48:], uint16(localPort))
binary.BigEndian.PutUint32(rule[50:], uint32(requestRouteID))
binary.BigEndian.PutUint32(rule[50:], uint32(reqRoute))
return rule
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/routing/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestAppRule(t *testing.T) {
keepAlive := 2 * time.Minute
pk, _ := cipher.GenerateKeyPair()
rule := AppRule(keepAlive, 2, pk, 3, 4, 1)
rule := AppRule(keepAlive, 1, 2, pk, 4, 3)

assert.Equal(t, keepAlive, rule.KeepAlive())
assert.Equal(t, RuleApp, rule.Type())
Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (sn *Node) createRoute(ctx context.Context, keepAlive time.Duration, route
nextTpID = r[i+1].Transport
rule = routing.ForwardRule(keepAlive, 0, nextTpID, 0)
} else {
rule = routing.AppRule(keepAlive, 0, init, lport, rport, 0)
rule = routing.AppRule(keepAlive, 0, 0, init, lport, rport)
}

go func(i int, pk cipher.PubKey, rule routing.Rule, reqIDChIn <-chan routing.RouteID,
Expand Down
Loading

0 comments on commit 958128d

Please sign in to comment.