diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2018-03-23 14:28:41 +0100 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2018-03-23 14:28:41 +0100 |
commit | 40cdb18263ac2624c5a007ef1b0ac7d1b21974bd (patch) | |
tree | 2b793bc89d94ab5ebeb43a0bc5f81082583d7c69 /api | |
parent | 454ee4bc97c45298e7979f5e3427f7870a628e74 (diff) |
Support mocking reply for more multi requests at once
Change-Id: I3610fe1e0c04f4487f6b7139fc62ef4515fad640
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'api')
-rw-r--r-- | api/api.go | 6 | ||||
-rw-r--r-- | api/api_test.go | 94 |
2 files changed, 93 insertions, 7 deletions
@@ -233,15 +233,15 @@ func (ch *Channel) receiveReplyInternal(msg Message) (LastReplyReceived bool, er return false, err } if vppReply.MessageID != expMsgID { - err = fmt.Errorf("invalid message ID %d, expected %d "+ - "(also check if multiple goroutines are not sharing one GoVPP channel)", vppReply.MessageID, expMsgID) + err = fmt.Errorf("received invalid message ID, expected %d (%s), but got %d (check if multiple goroutines are not sharing single GoVPP channel)", + expMsgID, msg.GetMessageName(), vppReply.MessageID) return false, err } // decode the message err = ch.MsgDecoder.DecodeMsg(vppReply.Data, msg) case <-time.After(ch.replyTimeout): - err = fmt.Errorf("no reply received within the timeout period %ds", ch.replyTimeout/time.Second) + err = fmt.Errorf("no reply received within the timeout period %s", ch.replyTimeout) } return } diff --git a/api/api_test.go b/api/api_test.go index 3e77f48..9af6e71 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -49,6 +49,8 @@ func setupTest(t *testing.T) *testCtx { ctx.ch, err = ctx.conn.NewAPIChannel() Expect(err).ShouldNot(HaveOccurred()) + ctx.ch.SetReplyTimeout(time.Millisecond) + return ctx } @@ -197,10 +199,9 @@ func TestMultiRequestReplySwInterfaceTapDump(t *testing.T) { // mock reply for i := 1; i <= 10; i++ { - byteName := []byte("dev-name-test") ctx.mockVpp.MockReply(&tap.SwInterfaceTapDetails{ SwIfIndex: uint32(i), - DevName: byteName, + DevName: []byte("dev-name-test"), }) } ctx.mockVpp.MockReply(&vpe.ControlPingReply{}) @@ -327,8 +328,6 @@ func TestSetReplyTimeout(t *testing.T) { ctx := setupTest(t) defer ctx.teardownTest() - ctx.ch.SetReplyTimeout(time.Millisecond) - // first one request should work ctx.mockVpp.MockReply(&vpe.ControlPingReply{}) err := ctx.ch.SendRequest(&vpe.ControlPing{}).ReceiveReply(&vpe.ControlPingReply{}) @@ -340,6 +339,47 @@ func TestSetReplyTimeout(t *testing.T) { Expect(err.Error()).To(ContainSubstring("timeout")) } +func TestSetReplyTimeoutMultiRequest(t *testing.T) { + ctx := setupTest(t) + defer ctx.teardownTest() + + for i := 1; i <= 3; i++ { + ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{ + SwIfIndex: uint32(i), + InterfaceName: []byte("if-name-test"), + }) + } + ctx.mockVpp.MockReply(&vpe.ControlPingReply{}) + + cnt := 0 + sendMultiRequest := func() error { + reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{}) + for { + msg := &interfaces.SwInterfaceDetails{} + stop, err := reqCtx.ReceiveReply(msg) + if stop { + break // break out of the loop + } + if err != nil { + return err + } + cnt++ + } + return nil + } + + // first one request should work + err := sendMultiRequest() + Expect(err).ShouldNot(HaveOccurred()) + + // no other reply ready - expect timeout + err = sendMultiRequest() + Expect(err).Should(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("timeout")) + + Expect(cnt).To(BeEquivalentTo(3)) +} + func TestReceiveReplyNegative(t *testing.T) { ctx := setupTest(t) defer ctx.teardownTest() @@ -362,3 +402,49 @@ func TestReceiveReplyNegative(t *testing.T) { Expect(err).Should(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("invalid request context")) } + +func TestMultiRequestDouble(t *testing.T) { + ctx := setupTest(t) + defer ctx.teardownTest() + + // mock reply + for i := 1; i <= 3; i++ { + ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{ + SwIfIndex: uint32(i), + InterfaceName: []byte("if-name-test"), + }) + } + ctx.mockVpp.MockReply(&vpe.ControlPingReply{}) + for i := 1; i <= 3; i++ { + ctx.mockVpp.MockReply(&interfaces.SwInterfaceDetails{ + SwIfIndex: uint32(i), + InterfaceName: []byte("if-name-test"), + }) + } + ctx.mockVpp.MockReply(&vpe.ControlPingReply{}) + + cnt := 0 + sendMultiRequest := func() error { + reqCtx := ctx.ch.SendMultiRequest(&interfaces.SwInterfaceDump{}) + for { + msg := &interfaces.SwInterfaceDetails{} + stop, err := reqCtx.ReceiveReply(msg) + if stop { + break // break out of the loop + } + if err != nil { + return err + } + cnt++ + } + return nil + } + + err := sendMultiRequest() + Expect(err).ShouldNot(HaveOccurred()) + + err = sendMultiRequest() + Expect(err).ShouldNot(HaveOccurred()) + + Expect(cnt).To(BeEquivalentTo(6)) +} |