Skip to content

Commit

Permalink
Implement tests for Close and KeepAlive packet types
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Dec 12, 2019
1 parent e4bae52 commit 6612878
Showing 1 changed file with 44 additions and 65 deletions.
109 changes: 44 additions & 65 deletions pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,71 +239,50 @@ func TestRouter_Serve(t *testing.T) {
assert.Equal(t, dstRtIDs[1], recvPacket.RouteID())
})

// 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
// localPort := routing.Port(9)
// cConn, sConn := net.Pipe()
//
// // mock-app config
// // appConf := &app.Config{
// // AppName: "test_app",
// // AppVersion: "1.0",
// // ProtocolVersion: supportedProtocolVersion,
// // }
//
// // serve mock-app
// // sErrCh := make(chan error, 1)
// go func() {
// // 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)
// go func() {
// conn, err := a.Accept()
// if err == nil {
// fmt.Println("ACCEPTED:", conn.RemoteAddr())
// }
// fmt.Println("FAILED TO ACCEPT")
// // cErrCh <- err
// // close(cErrCh)
// }()
// a.Dial(a.Addr().(routing.Addr))
// // defer func() {
// // assert.NoError(t, <-cErrCh)
// // }()
//
// // Add a APP rule for r0.
// // port8 := routing.Port(8)
// appRtID := routing.RouteID(7)
// appRule := routing.ConsumeRule(10*time.Minute, appRtID, keys[1].PK, localPort, localPort)
// require.NoError(t, r0.rt.SaveRule(appRule))
//
// // Call handleTransportPacket for r0.
//
// // payload is prepended with two bytes to satisfy app.Proto.
// // 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...)
// packet := routing.MakeDataPacket(appRtID, rawRAddr)
// require.NoError(t, r0.handleTransportPacket(context.TODO(), packet))
// })
t.Run("handlePacket_close", func(t *testing.T) {
defer clearRules(r0, r1)
defer clearRouteGroups(r0, r1)

rtIDs, err := r0.ReserveKeys(1)
require.NoError(t, err)

cnsmRule := routing.ConsumeRule(1*time.Hour, rtIDs[0], keys[1].PK, keys[0].PK, 0, 0)
err = r0.rt.SaveRule(cnsmRule)
require.NoError(t, err)
require.Len(t, r0.rt.AllRules(), 1)

packet := routing.MakeClosePacket(rtIDs[0], routing.CloseRequested)
require.NoError(t, r0.handleTransportPacket(context.TODO(), packet))

require.Len(t, r0.rt.AllRules(), 0)
})

t.Run("handlePacket_keepalive", func(t *testing.T) {
defer clearRules(r0, r1)
defer clearRouteGroups(r0, r1)

rtIDs, err := r0.ReserveKeys(1)
require.NoError(t, err)

rtID := rtIDs[0]

cnsmRule := routing.ConsumeRule(100*time.Millisecond, rtID, keys[1].PK, keys[0].PK, 0, 0)
err = r0.rt.SaveRule(cnsmRule)
require.NoError(t, err)
require.Len(t, r0.rt.AllRules(), 1)

time.Sleep(50 * time.Millisecond)

packet := routing.MakeKeepAlivePacket(rtIDs[0])
require.NoError(t, r0.handleTransportPacket(context.TODO(), packet))

require.Len(t, r0.rt.AllRules(), 1)
time.Sleep(50 * time.Millisecond)
require.Len(t, r0.rt.AllRules(), 1)

time.Sleep(100 * time.Millisecond)
require.Len(t, r0.rt.AllRules(), 0)
})
}

// TODO (Darkren): fix tests
Expand Down

0 comments on commit 6612878

Please sign in to comment.