Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Aug 22, 2019
1 parent 54b5095 commit b6d0f62
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
10 changes: 7 additions & 3 deletions pkg/router/managed_routing_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import (
func TestManagedRoutingTableCleanup(t *testing.T) {
rt := manageRoutingTable(routing.InMemoryRoutingTable())

_, err := rt.AddRule(routing.ForwardRule(time.Now().Add(time.Hour), 3, uuid.New(), 1))
_, err := rt.AddRule(routing.ForwardRule(1*time.Hour, 3, uuid.New(), 1))
require.NoError(t, err)

id, err := rt.AddRule(routing.ForwardRule(time.Now().Add(-time.Hour), 3, uuid.New(), 2))
id, err := rt.AddRule(routing.ForwardRule(1*time.Hour, 3, uuid.New(), 2))
require.NoError(t, err)

id2, err := rt.AddRule(routing.ForwardRule(time.Now().Add(-time.Hour), 3, uuid.New(), 3))
id2, err := rt.AddRule(routing.ForwardRule(-1*time.Hour, 3, uuid.New(), 3))
require.NoError(t, err)

// rule should already be expired at this point due to the execution time.
// However, we'll just a bit to be sure
time.Sleep(1 * time.Millisecond)

assert.Equal(t, 3, rt.Count())

_, err = rt.Rule(id)
Expand Down
22 changes: 13 additions & 9 deletions pkg/router/route_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ func TestNewRouteManager(t *testing.T) {
t.Run("GetRule", func(t *testing.T) {
defer clearRules()

expiredRule := routing.ForwardRule(time.Now().Add(-10*time.Minute), 3, uuid.New(), 1)
expiredRule := routing.ForwardRule(-10*time.Minute, 3, uuid.New(), 1)
expiredID, err := rt.AddRule(expiredRule)
require.NoError(t, err)

rule := routing.ForwardRule(time.Now().Add(10*time.Minute), 3, uuid.New(), 2)
rule := routing.ForwardRule(10*time.Minute, 3, uuid.New(), 2)
id, err := rt.AddRule(rule)
require.NoError(t, err)

// rule should already be expired at this point due to the execution time.
// However, we'll just a bit to be sure
time.Sleep(1 * time.Millisecond)

_, err = rm.GetRule(expiredID)
require.Error(t, err)

Expand All @@ -67,7 +71,7 @@ func TestNewRouteManager(t *testing.T) {
defer clearRules()

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

Expand Down Expand Up @@ -112,7 +116,7 @@ func TestNewRouteManager(t *testing.T) {
require.NoError(t, err)

// Emulate SetupNode sending AddRule request.
rule := routing.ForwardRule(time.Now(), 3, uuid.New(), id)
rule := routing.ForwardRule(10*time.Minute, 3, uuid.New(), id)
err = setup.AddRule(context.TODO(), setup.NewSetupProtocol(addIn), rule)
require.NoError(t, err)

Expand Down Expand Up @@ -150,7 +154,7 @@ func TestNewRouteManager(t *testing.T) {

proto := setup.NewSetupProtocol(in)

rule := routing.ForwardRule(time.Now(), 3, uuid.New(), 1)
rule := routing.ForwardRule(10*time.Minute, 3, uuid.New(), 1)
id, err := rt.AddRule(rule)
require.NoError(t, err)
assert.Equal(t, 1, rt.Count())
Expand Down Expand Up @@ -186,10 +190,10 @@ func TestNewRouteManager(t *testing.T) {

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

rule = routing.ForwardRule(time.Now(), 3, uuid.New(), 1)
rule = routing.ForwardRule(10*time.Minute, 3, uuid.New(), 1)
require.NoError(t, rt.SetRule(1, rule))

ld := routing.LoopData{
Expand Down Expand Up @@ -238,10 +242,10 @@ func TestNewRouteManager(t *testing.T) {
proto := setup.NewSetupProtocol(in)
pk, _ := cipher.GenerateKeyPair()

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

rule = routing.ForwardRule(time.Now(), 3, uuid.New(), 1)
rule = routing.ForwardRule(10*time.Minute, 3, uuid.New(), 1)
require.NoError(t, rt.SetRule(1, rule))

ld := routing.LoopData{
Expand Down
10 changes: 5 additions & 5 deletions pkg/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

const (
// RouteTTL is the default expiration interval for routes
RouteTTL = 2 * time.Hour
// RouteKeepAlive is the default expiration interval for routes
RouteKeepAlive = 2 * time.Hour

// DefaultGarbageCollectDuration is the default duration for garbage collection of routing rules.
DefaultGarbageCollectDuration = time.Second * 5
Expand Down Expand Up @@ -298,9 +298,9 @@ func (r *Router) requestLoop(ctx context.Context, appConn *app.Protocol, raddr r
Local: laddr,
Remote: raddr,
},
Expiry: time.Now().Add(RouteTTL),
Forward: forwardRoute,
Reverse: reverseRoute,
KeepAlive: RouteKeepAlive,
Forward: forwardRoute,
Reverse: reverseRoute,
}

sConn, err := r.rm.dialSetupConn(ctx)
Expand Down
2 changes: 1 addition & 1 deletion pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestRouter_Serve(t *testing.T) {
defer clearRules(r0, r1)

// Add a FWD rule for r0.
fwdRule := routing.ForwardRule(time.Now().Add(time.Hour), routing.RouteID(5), tp1.Entry.ID, routing.RouteID(0))
fwdRule := routing.ForwardRule(1*time.Hour, routing.RouteID(5), tp1.Entry.ID, routing.RouteID(0))
fwdRtID, err := r0.rm.rt.AddRule(fwdRule)
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions pkg/routing/routing_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
func RoutingTableSuite(t *testing.T, tbl Table) {
t.Helper()

rule := ForwardRule(time.Now(), 2, uuid.New(), 1)
rule := ForwardRule(15*time.Minute, 2, uuid.New(), 1)
id, err := tbl.AddRule(rule)
require.NoError(t, err)

Expand All @@ -39,7 +39,7 @@ func RoutingTableSuite(t *testing.T, tbl Table) {
require.NoError(t, err)
assert.Equal(t, rule, r)

rule2 := ForwardRule(time.Now(), 3, uuid.New(), 2)
rule2 := ForwardRule(15*time.Minute, 3, uuid.New(), 2)
id2, err := tbl.AddRule(rule2)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion pkg/setup/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestNode(t *testing.T) {
&routing.Hop{From: clients[3].Addr.PK, To: clients[2].Addr.PK, Transport: uuid.New()},
&routing.Hop{From: clients[2].Addr.PK, To: clients[1].Addr.PK, Transport: uuid.New()},
},
Expiry: time.Now().Add(time.Hour),
KeepAlive: 1 * time.Hour,
}

// client_1 initiates loop creation with setup node.
Expand Down
6 changes: 3 additions & 3 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP
log.Infof("tp[%2d]: %v", i, tps[i])
}
rt := routing.InMemoryRoutingTable()
ruleExp := time.Now().Add(time.Hour * 24)
ruleKeepAlive := 24 * time.Hour
for i := 0; i < r.Intn(maxRules+1); i++ {
remotePK, _ := cipher.GenerateKeyPair()
var lpRaw, rpRaw [2]byte
Expand All @@ -215,15 +215,15 @@ func NewMockRPCClient(r *rand.Rand, maxTps int, maxRules int) (cipher.PubKey, RP
if err != nil {
panic(err)
}
fwdRule := routing.ForwardRule(ruleExp, routing.RouteID(r.Uint32()), uuid.New(), fwdRID)
fwdRule := routing.ForwardRule(ruleKeepAlive, routing.RouteID(r.Uint32()), uuid.New(), fwdRID)
if err := rt.SetRule(fwdRID, fwdRule); err != nil {
panic(err)
}
appRID, err := rt.AddRule(nil)
if err != nil {
panic(err)
}
appRule := routing.AppRule(ruleExp, fwdRID, remotePK, rp, lp, appRID)
appRule := routing.AppRule(ruleKeepAlive, fwdRID, remotePK, rp, lp, appRID)
if err := rt.SetRule(appRID, appRule); err != nil {
panic(err)
}
Expand Down

0 comments on commit b6d0f62

Please sign in to comment.