Change module name to go.fd.io/govpp
[govpp.git] / core / connection_test.go
index b7c3aa0..fe2f191 100644 (file)
@@ -16,16 +16,18 @@ package core_test
 
 import (
        "testing"
+       "time"
 
-       "git.fd.io/govpp.git/adapter/mock"
-       "git.fd.io/govpp.git/api"
-       "git.fd.io/govpp.git/core"
-       "git.fd.io/govpp.git/core/bin_api/vpe"
-       "git.fd.io/govpp.git/examples/bin_api/interfaces"
-       "git.fd.io/govpp.git/examples/bin_api/stats"
-
-       "git.fd.io/govpp.git/codec"
        . "github.com/onsi/gomega"
+
+       "go.fd.io/govpp/adapter/mock"
+       "go.fd.io/govpp/api"
+       "go.fd.io/govpp/binapi/ethernet_types"
+       interfaces "go.fd.io/govpp/binapi/interface"
+       "go.fd.io/govpp/binapi/interface_types"
+       memclnt "go.fd.io/govpp/binapi/memclnt"
+       "go.fd.io/govpp/codec"
+       "go.fd.io/govpp/core"
 )
 
 type testCtx struct {
@@ -37,8 +39,9 @@ type testCtx struct {
 func setupTest(t *testing.T, bufferedChan bool) *testCtx {
        RegisterTestingT(t)
 
-       ctx := &testCtx{}
-       ctx.mockVpp = &mock.VppAdapter{}
+       ctx := &testCtx{
+               mockVpp: mock.NewVppAdapter(),
+       }
 
        var err error
        ctx.conn, err = core.Connect(ctx.mockVpp)
@@ -59,100 +62,6 @@ func (ctx *testCtx) teardownTest() {
        ctx.conn.Disconnect()
 }
 
-func TestSimpleRequest(t *testing.T) {
-       ctx := setupTest(t, false)
-       defer ctx.teardownTest()
-
-       ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: -5})
-
-       req := &vpe.ControlPing{}
-       reply := &vpe.ControlPingReply{}
-
-       // send the request and receive a reply
-       ctx.ch.GetRequestChannel() <- &api.VppRequest{Message: req}
-       vppReply := <-ctx.ch.GetReplyChannel()
-
-       Expect(vppReply).ShouldNot(BeNil())
-       Expect(vppReply.Error).ShouldNot(HaveOccurred())
-
-       // decode the message
-       err := ctx.ch.GetMessageDecoder().DecodeMsg(vppReply.Data, reply)
-       Expect(err).ShouldNot(HaveOccurred())
-
-       Expect(reply.Retval).To(BeEquivalentTo(-5))
-}
-
-func TestMultiRequest(t *testing.T) {
-       ctx := setupTest(t, false)
-       defer ctx.teardownTest()
-
-       msgs := []api.Message{}
-       for m := 0; m < 10; m++ {
-               msgs = append(msgs, &interfaces.SwInterfaceDetails{})
-       }
-       ctx.mockVpp.MockReply(msgs...)
-       ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
-
-       // send multipart request
-       ctx.ch.GetRequestChannel() <- &api.VppRequest{Message: &interfaces.SwInterfaceDump{}, Multipart: true}
-
-       cnt := 0
-       for {
-               // receive a reply
-               vppReply := <-ctx.ch.GetReplyChannel()
-               if vppReply.LastReplyReceived {
-                       break // break out of the loop
-               }
-               Expect(vppReply.Error).ShouldNot(HaveOccurred())
-
-               // decode the message
-               reply := &interfaces.SwInterfaceDetails{}
-               err := ctx.ch.GetMessageDecoder().DecodeMsg(vppReply.Data, reply)
-               Expect(err).ShouldNot(HaveOccurred())
-               cnt++
-       }
-
-       Expect(cnt).To(BeEquivalentTo(10))
-}
-
-func TestNotifications(t *testing.T) {
-       ctx := setupTest(t, false)
-       defer ctx.teardownTest()
-
-       // subscribe for notification
-       notifChan := make(chan api.Message, 1)
-       subscription := &api.NotifSubscription{
-               NotifChan:  notifChan,
-               MsgFactory: interfaces.NewSwInterfaceSetFlags,
-       }
-       ctx.ch.GetNotificationChannel() <- &api.NotifSubscribeRequest{
-               Subscription: subscription,
-               Subscribe:    true,
-       }
-       err := <-ctx.ch.GetNotificationReplyChannel()
-       Expect(err).ShouldNot(HaveOccurred())
-
-       // mock the notification and force its delivery
-       ctx.mockVpp.MockReply(&interfaces.SwInterfaceSetFlags{
-               SwIfIndex:   3,
-               AdminUpDown: 1,
-       })
-       ctx.mockVpp.SendMsg(0, []byte{0})
-
-       // receive the notification
-       notif := (<-notifChan).(*interfaces.SwInterfaceSetFlags)
-
-       Expect(notif.SwIfIndex).To(BeEquivalentTo(3))
-
-       // unsubscribe notification
-       ctx.ch.GetNotificationChannel() <- &api.NotifSubscribeRequest{
-               Subscription: subscription,
-               Subscribe:    false,
-       }
-       err = <-ctx.ch.GetNotificationReplyChannel()
-       Expect(err).ShouldNot(HaveOccurred())
-}
-
 func TestNilConnection(t *testing.T) {
        RegisterTestingT(t)
        var conn *core.Connection
@@ -168,102 +77,73 @@ func TestNilConnection(t *testing.T) {
        Expect(err.Error()).To(ContainSubstring("nil"))
 }
 
-func TestDoubleConnection(t *testing.T) {
-       ctx := setupTest(t, false)
-       defer ctx.teardownTest()
-
-       conn, err := core.Connect(ctx.mockVpp)
-       Expect(err).Should(HaveOccurred())
-       Expect(err.Error()).To(ContainSubstring("only one connection per process"))
-       Expect(conn).Should(BeNil())
-}
-
 func TestAsyncConnection(t *testing.T) {
        ctx := setupTest(t, false)
        defer ctx.teardownTest()
 
        ctx.conn.Disconnect()
-       conn, ch, err := core.AsyncConnect(ctx.mockVpp)
+       conn, statusChan, err := core.AsyncConnect(ctx.mockVpp, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
        ctx.conn = conn
 
        Expect(err).ShouldNot(HaveOccurred())
        Expect(conn).ShouldNot(BeNil())
 
-       ev := <-ch
+       ev := <-statusChan
        Expect(ev.State).Should(BeEquivalentTo(core.Connected))
 }
 
-func TestFullBuffer(t *testing.T) {
+func TestAsyncConnectionProcessesVppTimeout(t *testing.T) {
        ctx := setupTest(t, false)
        defer ctx.teardownTest()
 
-       // close the default API channel
-       ctx.ch.Close()
+       ctx.conn.Disconnect()
+       conn, statusChan, err := core.AsyncConnect(ctx.mockVpp, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
+       ctx.conn = conn
 
-       // create a new channel with limited buffer sizes
-       var err error
-       ctx.ch, err = ctx.conn.NewAPIChannelBuffered(10, 1)
        Expect(err).ShouldNot(HaveOccurred())
+       Expect(conn).ShouldNot(BeNil())
 
-       // send multiple requests, only one reply should be read
-       for i := 0; i < 20; i++ {
-               ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
-               ctx.ch.GetRequestChannel() <- &api.VppRequest{Message: &vpe.ControlPing{}}
-       }
-
-       vppReply := <-ctx.ch.GetReplyChannel()
-       Expect(vppReply).ShouldNot(BeNil())
+       ev := <-statusChan
+       Expect(ev.State).Should(BeEquivalentTo(core.Connected))
 
-       var received bool
-       select {
-       case <-ctx.ch.GetReplyChannel():
-               received = true // this should not happen
-       default:
-               received = false // no reply to be received
-       }
-       Expect(received).Should(BeFalse(), "A reply has been recieved, should had been ignored.")
+       // make control ping reply fail so that connection.healthCheckLoop()
+       // initiates reconnection.
+       ctx.mockVpp.MockReply(&memclnt.ControlPingReply{
+               Retval: -1,
+       })
+       time.Sleep(time.Duration(1+core.HealthCheckThreshold) * (core.HealthCheckInterval + 2*core.HealthCheckReplyTimeout))
 }
 
 func TestCodec(t *testing.T) {
        RegisterTestingT(t)
 
-       msgCodec := &codec.MsgCodec{}
+       var msgCodec = codec.DefaultCodec
 
        // request
-       data, err := msgCodec.EncodeMsg(&interfaces.CreateLoopback{MacAddress: []byte{1, 2, 3, 4, 5, 6}}, 11)
+       data, err := msgCodec.EncodeMsg(&interfaces.CreateLoopback{MacAddress: ethernet_types.MacAddress{1, 2, 3, 4, 5, 6}}, 11)
        Expect(err).ShouldNot(HaveOccurred())
        Expect(data).ShouldNot(BeEmpty())
 
        msg1 := &interfaces.CreateLoopback{}
        err = msgCodec.DecodeMsg(data, msg1)
        Expect(err).ShouldNot(HaveOccurred())
-       Expect(msg1.MacAddress).To(BeEquivalentTo([]byte{1, 2, 3, 4, 5, 6}))
+       Expect(msg1.MacAddress).To(BeEquivalentTo(ethernet_types.MacAddress{1, 2, 3, 4, 5, 6}))
 
        // reply
-       data, err = msgCodec.EncodeMsg(&vpe.ControlPingReply{Retval: 55}, 22)
+       data, err = msgCodec.EncodeMsg(&memclnt.ControlPingReply{Retval: 55}, 22)
        Expect(err).ShouldNot(HaveOccurred())
        Expect(data).ShouldNot(BeEmpty())
 
-       msg2 := &vpe.ControlPingReply{}
+       msg2 := &memclnt.ControlPingReply{}
        err = msgCodec.DecodeMsg(data, msg2)
        Expect(err).ShouldNot(HaveOccurred())
        Expect(msg2.Retval).To(BeEquivalentTo(55))
-
-       // other
-       data, err = msgCodec.EncodeMsg(&stats.VnetIP4FibCounters{VrfID: 77}, 33)
-       Expect(err).ShouldNot(HaveOccurred())
-       Expect(data).ShouldNot(BeEmpty())
-
-       msg3 := &stats.VnetIP4FibCounters{}
-       err = msgCodec.DecodeMsg(data, msg3)
-       Expect(err).ShouldNot(HaveOccurred())
-       Expect(msg3.VrfID).To(BeEquivalentTo(77))
 }
 
 func TestCodecNegative(t *testing.T) {
        RegisterTestingT(t)
 
-       msgCodec := &codec.MsgCodec{}
+       var msgCodec = codec.DefaultCodec
 
        // nil message for encoding
        data, err := msgCodec.EncodeMsg(nil, 15)
@@ -277,9 +157,9 @@ func TestCodecNegative(t *testing.T) {
        Expect(err.Error()).To(ContainSubstring("nil message"))
 
        // nil data for decoding
-       err = msgCodec.DecodeMsg(nil, &vpe.ControlPingReply{})
+       err = msgCodec.DecodeMsg(nil, &memclnt.ControlPingReply{})
        Expect(err).Should(HaveOccurred())
-       Expect(err.Error()).To(ContainSubstring("EOF"))
+       Expect(err.Error()).To(ContainSubstring("panic"))
 }
 
 func TestSimpleRequestsWithSequenceNumbers(t *testing.T) {
@@ -288,16 +168,15 @@ func TestSimpleRequestsWithSequenceNumbers(t *testing.T) {
 
        var reqCtx []api.RequestCtx
        for i := 0; i < 10; i++ {
-               ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: int32(i)})
-               req := &vpe.ControlPing{}
+               ctx.mockVpp.MockReply(&memclnt.ControlPingReply{})
+               req := &memclnt.ControlPing{}
                reqCtx = append(reqCtx, ctx.ch.SendRequest(req))
        }
 
        for i := 0; i < 10; i++ {
-               reply := &vpe.ControlPingReply{}
+               reply := &memclnt.ControlPingReply{}
                err := reqCtx[i].ReceiveReply(reply)
                Expect(err).ShouldNot(HaveOccurred())
-               Expect(reply.Retval).To(BeEquivalentTo(i))
        }
 }
 
@@ -305,12 +184,12 @@ func TestMultiRequestsWithSequenceNumbers(t *testing.T) {
        ctx := setupTest(t, false)
        defer ctx.teardownTest()
 
-       msgs := []api.Message{}
+       var msgs []api.Message
        for i := 0; i < 10; i++ {
-               msgs = append(msgs, &interfaces.SwInterfaceDetails{SwIfIndex: uint32(i)})
+               msgs = append(msgs, &interfaces.SwInterfaceDetails{SwIfIndex: interface_types.InterfaceIndex(i)})
        }
        ctx.mockVpp.MockReply(msgs...)
-       ctx.mockVpp.MockReply(&vpe.ControlPingReply{})
+       ctx.mockVpp.MockReply(&memclnt.ControlPingReply{})
 
        // send multipart request
        reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
@@ -324,7 +203,7 @@ func TestMultiRequestsWithSequenceNumbers(t *testing.T) {
                lastReplyReceived, err := reqCtx.ReceiveReply(reply)
 
                if lastReplyReceived {
-                       break // break out of the loop
+                       break
                }
 
                Expect(err).ShouldNot(HaveOccurred())
@@ -342,15 +221,15 @@ func TestSimpleRequestWithTimeout(t *testing.T) {
 
        // reply for a previous timeouted requests to be ignored
        ctx.mockVpp.MockReplyWithContext(mock.MsgWithContext{
-               Msg:    &vpe.ControlPingReply{Retval: 1},
+               Msg:    &memclnt.ControlPingReply{},
                SeqNum: 0,
        })
 
        // send reply later
-       req1 := &vpe.ControlPing{}
+       req1 := &memclnt.ControlPing{}
        reqCtx1 := ctx.ch.SendRequest(req1)
 
-       reply := &vpe.ControlPingReply{}
+       reply := &memclnt.ControlPingReply{}
        err := reqCtx1.ReceiveReply(reply)
        Expect(err).ToNot(BeNil())
        Expect(err.Error()).To(HavePrefix("no reply received within the timeout period"))
@@ -358,24 +237,23 @@ func TestSimpleRequestWithTimeout(t *testing.T) {
        ctx.mockVpp.MockReplyWithContext(
                // reply for the previous request
                mock.MsgWithContext{
-                       Msg:    &vpe.ControlPingReply{Retval: 1},
+                       Msg:    &memclnt.ControlPingReply{},
                        SeqNum: 1,
                },
                // reply for the next request
                mock.MsgWithContext{
-                       Msg:    &vpe.ControlPingReply{Retval: 2},
+                       Msg:    &memclnt.ControlPingReply{},
                        SeqNum: 2,
                })
 
        // next request
-       req2 := &vpe.ControlPing{}
+       req2 := &memclnt.ControlPing{}
        reqCtx2 := ctx.ch.SendRequest(req2)
 
        // second request should ignore the first reply and return the second one
-       reply = &vpe.ControlPingReply{}
+       reply = &memclnt.ControlPingReply{}
        err = reqCtx2.ReceiveReply(reply)
        Expect(err).To(BeNil())
-       Expect(reply.Retval).To(BeEquivalentTo(2))
 }
 
 func TestSimpleRequestsWithMissingReply(t *testing.T) {
@@ -383,37 +261,36 @@ func TestSimpleRequestsWithMissingReply(t *testing.T) {
        defer ctx.teardownTest()
 
        // request without reply
-       req1 := &vpe.ControlPing{}
+       req1 := &memclnt.ControlPing{}
        reqCtx1 := ctx.ch.SendRequest(req1)
 
        // another request without reply
-       req2 := &vpe.ControlPing{}
+       req2 := &memclnt.ControlPing{}
        reqCtx2 := ctx.ch.SendRequest(req2)
 
        // third request with reply
        ctx.mockVpp.MockReplyWithContext(mock.MsgWithContext{
-               Msg:    &vpe.ControlPingReply{Retval: 3},
+               Msg:    &memclnt.ControlPingReply{},
                SeqNum: 3,
        })
-       req3 := &vpe.ControlPing{}
+       req3 := &memclnt.ControlPing{}
        reqCtx3 := ctx.ch.SendRequest(req3)
 
        // the first two should fail, but not consume reply for the 3rd
-       reply := &vpe.ControlPingReply{}
+       reply := &memclnt.ControlPingReply{}
        err := reqCtx1.ReceiveReply(reply)
        Expect(err).ToNot(BeNil())
        Expect(err.Error()).To(Equal("missing binary API reply with sequence number: 1"))
 
-       reply = &vpe.ControlPingReply{}
+       reply = &memclnt.ControlPingReply{}
        err = reqCtx2.ReceiveReply(reply)
        Expect(err).ToNot(BeNil())
        Expect(err.Error()).To(Equal("missing binary API reply with sequence number: 2"))
 
        // the second request should succeed
-       reply = &vpe.ControlPingReply{}
+       reply = &memclnt.ControlPingReply{}
        err = reqCtx3.ReceiveReply(reply)
        Expect(err).To(BeNil())
-       Expect(reply.Retval).To(BeEquivalentTo(3))
 }
 
 func TestMultiRequestsWithErrors(t *testing.T) {
@@ -421,38 +298,25 @@ func TestMultiRequestsWithErrors(t *testing.T) {
        defer ctx.teardownTest()
 
        // replies for a previous timeouted requests to be ignored
-       msgs := []mock.MsgWithContext{}
-       msgs = append(msgs,
-               mock.MsgWithContext{
-                       Msg:    &vpe.ControlPingReply{Retval: 1},
-                       SeqNum: 0xffff - 1,
-               },
-               mock.MsgWithContext{
-                       Msg:    &vpe.ControlPingReply{Retval: 1},
-                       SeqNum: 0xffff,
-               },
-               mock.MsgWithContext{
-                       Msg:    &vpe.ControlPingReply{Retval: 1},
-                       SeqNum: 0,
-               })
-
+       msgs := []mock.MsgWithContext{
+               {Msg: &memclnt.ControlPingReply{}, SeqNum: 0xffff - 1},
+               {Msg: &memclnt.ControlPingReply{}, SeqNum: 0xffff},
+               {Msg: &memclnt.ControlPingReply{}, SeqNum: 0},
+       }
        for i := 0; i < 10; i++ {
-               msgs = append(msgs,
-                       mock.MsgWithContext{
-                               Msg:       &interfaces.SwInterfaceDetails{SwIfIndex: uint32(i)},
-                               SeqNum:    1,
-                               Multipart: true,
-                       })
+               msgs = append(msgs, mock.MsgWithContext{
+                       Msg:       &interfaces.SwInterfaceDetails{SwIfIndex: interface_types.InterfaceIndex(i)},
+                       SeqNum:    1,
+                       Multipart: true,
+               })
        }
        // missing finalizing control ping
 
        // reply for a next request
-       msgs = append(msgs,
-               mock.MsgWithContext{
-                       Msg:       &vpe.ControlPingReply{Retval: 2},
-                       SeqNum:    2,
-                       Multipart: false,
-               })
+       msgs = append(msgs, mock.MsgWithContext{
+               Msg:    &memclnt.ControlPingReply{},
+               SeqNum: 2,
+       })
 
        // queue replies
        ctx.mockVpp.MockReplyWithContext(msgs...)
@@ -482,11 +346,10 @@ func TestMultiRequestsWithErrors(t *testing.T) {
        Expect(err.Error()).To(Equal("missing binary API reply with sequence number: 1"))
 
        // reply for the second request has not been consumed
-       reqCtx2 := ctx.ch.SendRequest(&vpe.ControlPing{})
-       reply2 := &vpe.ControlPingReply{}
+       reqCtx2 := ctx.ch.SendRequest(&memclnt.ControlPing{})
+       reply2 := &memclnt.ControlPingReply{}
        err = reqCtx2.ReceiveReply(reply2)
        Expect(err).To(BeNil())
-       Expect(reply2.Retval).To(BeEquivalentTo(2))
 }
 
 func TestRequestsOrdering(t *testing.T) {
@@ -497,47 +360,45 @@ func TestRequestsOrdering(t *testing.T) {
        // some replies will get thrown away
 
        // first request
-       ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: 1})
-       req1 := &vpe.ControlPing{}
+       ctx.mockVpp.MockReply(&memclnt.ControlPingReply{})
+       req1 := &memclnt.ControlPing{}
        reqCtx1 := ctx.ch.SendRequest(req1)
 
        // second request
-       ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: 2})
-       req2 := &vpe.ControlPing{}
+       ctx.mockVpp.MockReply(&memclnt.ControlPingReply{})
+       req2 := &memclnt.ControlPing{}
        reqCtx2 := ctx.ch.SendRequest(req2)
 
        // if reply for the second request is read first, the reply for the first
        // request gets thrown away.
-       reply2 := &vpe.ControlPingReply{}
+       reply2 := &memclnt.ControlPingReply{}
        err := reqCtx2.ReceiveReply(reply2)
        Expect(err).To(BeNil())
-       Expect(reply2.Retval).To(BeEquivalentTo(2))
 
        // first request has already been considered closed
-       reply1 := &vpe.ControlPingReply{}
+       reply1 := &memclnt.ControlPingReply{}
        err = reqCtx1.ReceiveReply(reply1)
        Expect(err).ToNot(BeNil())
        Expect(err.Error()).To(HavePrefix("no reply received within the timeout period"))
 }
 
 func TestCycleOverSetOfSequenceNumbers(t *testing.T) {
-       ctx := setupTest(t, true)
+       ctx := setupTest(t, false)
        defer ctx.teardownTest()
 
        numIters := 0xffff + 100
        reqCtx := make(map[int]api.RequestCtx)
 
-       for i := 0; i < numIters+30; /* receiver is 30 reqs behind */ i++ {
+       for i := 0; i < numIters+30; i++ {
                if i < numIters {
-                       ctx.mockVpp.MockReply(&vpe.ControlPingReply{Retval: int32(i)})
-                       req := &vpe.ControlPing{}
+                       ctx.mockVpp.MockReply(&memclnt.ControlPingReply{})
+                       req := &memclnt.ControlPing{}
                        reqCtx[i] = ctx.ch.SendRequest(req)
                }
                if i > 30 {
-                       reply := &vpe.ControlPingReply{}
+                       reply := &memclnt.ControlPingReply{}
                        err := reqCtx[i-30].ReceiveReply(reply)
                        Expect(err).ShouldNot(HaveOccurred())
-                       Expect(reply.Retval).To(BeEquivalentTo(i - 30))
                }
        }
 }