go.ligato.io/vpp-agent/v3@v3.5.0/plugins/govppmux/client_binapi_test.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at:
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package govppmux
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	. "github.com/onsi/gomega"
    22  	"go.fd.io/govpp/core"
    23  
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    25  )
    26  
    27  func TestRequestRetry(t *testing.T) {
    28  	tests := []struct {
    29  		name        string
    30  		attempts    int
    31  		timeout     time.Duration
    32  		retErrs     []error
    33  		expErr      error
    34  		expAttempts int
    35  	}{
    36  		{name: "no retry fail",
    37  			attempts:    0,
    38  			timeout:     time.Millisecond,
    39  			retErrs:     []error{core.ErrNotConnected},
    40  			expErr:      core.ErrNotConnected,
    41  			expAttempts: 0,
    42  		},
    43  		{name: "1 retry ok",
    44  			attempts:    1,
    45  			timeout:     time.Millisecond,
    46  			retErrs:     []error{core.ErrNotConnected},
    47  			expErr:      nil,
    48  			expAttempts: 1,
    49  		},
    50  		{name: "3 retries fail",
    51  			attempts:    3,
    52  			timeout:     time.Millisecond,
    53  			retErrs:     []error{core.ErrNotConnected, core.ErrNotConnected, core.ErrNotConnected, core.ErrNotConnected},
    54  			expErr:      core.ErrNotConnected,
    55  			expAttempts: 3,
    56  		},
    57  		{name: "no retry attempt",
    58  			attempts:    3,
    59  			timeout:     time.Millisecond,
    60  			retErrs:     []error{core.ErrInvalidRequestCtx},
    61  			expErr:      core.ErrInvalidRequestCtx,
    62  			expAttempts: 0,
    63  		},
    64  
    65  		{name: "3 retries ok",
    66  			attempts:    3,
    67  			timeout:     time.Millisecond,
    68  			retErrs:     []error{core.ErrNotConnected, core.ErrNotConnected, core.ErrNotConnected},
    69  			expErr:      nil,
    70  			expAttempts: 3,
    71  		},
    72  	}
    73  	for _, test := range tests {
    74  		t.Run(test.name, func(t *testing.T) {
    75  			ctx := vppmock.SetupTestCtx(t)
    76  			defer ctx.TeardownTestCtx()
    77  
    78  			retryCfg := retryConfig{test.attempts, test.timeout}
    79  			ch := newGovppChan(ctx.MockChannel, retryCfg)
    80  
    81  			ctx.MockChannel.RetErrs = test.retErrs
    82  
    83  			ctx.MockVpp.MockReply(&core.ControlPingReply{})
    84  			for i := 0; i < test.attempts; i++ {
    85  				ctx.MockVpp.MockReply(&core.ControlPingReply{})
    86  			}
    87  
    88  			req := &core.ControlPing{}
    89  			reply := &core.ControlPingReply{}
    90  			reqCtx := ch.SendRequest(req)
    91  			err := reqCtx.ReceiveReply(reply)
    92  
    93  			if test.expErr == nil {
    94  				Expect(err).Should(Succeed())
    95  			} else {
    96  				Expect(err).Should(HaveOccurred())
    97  			}
    98  			Expect(ctx.MockChannel.Msgs).To(HaveLen(test.expAttempts + 1))
    99  		})
   100  	}
   101  }