github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/client/errors_test.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package client
    22  
    23  import (
    24  	"fmt"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
    29  	"github.com/m3db/m3/src/dbnode/network/server/tchannelthrift/errors"
    30  	"github.com/m3db/m3/src/dbnode/topology"
    31  	xerrors "github.com/m3db/m3/src/x/errors"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  	"github.com/uber/tchannel-go"
    35  )
    36  
    37  func TestConsistencyResultError(t *testing.T) {
    38  	badReqErr := xerrors.NewRenamedError(&rpc.Error{
    39  		Type: rpc.ErrorType_BAD_REQUEST,
    40  	}, fmt.Errorf("renamed error"))
    41  	timeoutErr := errors.NewTimeoutError(fmt.Errorf("timeout"))
    42  
    43  	level := topology.ReadConsistencyLevelMajority
    44  	enqueued := 4
    45  	responded := 4
    46  	errs := []error{fmt.Errorf("another error"), timeoutErr, badReqErr} // badReqErr takes precedence
    47  
    48  	err := error(newConsistencyResultError(level, enqueued, responded, errs))
    49  
    50  	assert.True(t, strings.HasPrefix(err.Error(),
    51  		"failed to meet consistency level majority with 1/4 success, 4 nodes responded, errors:"))
    52  	assert.Equal(t, badReqErr, xerrors.InnerError(err))
    53  	assert.True(t, IsBadRequestError(err))
    54  	assert.Equal(t, 4, NumResponded(err))
    55  	assert.Equal(t, 1, NumSuccess(err))
    56  	assert.Equal(t, 3, NumError(err))
    57  }
    58  
    59  func TestConsistencyResultTimeoutError(t *testing.T) {
    60  	timeoutErr := errors.NewTimeoutError(fmt.Errorf("timeout"))
    61  
    62  	level := topology.ReadConsistencyLevelMajority
    63  	enqueued := 3
    64  	responded := 3
    65  	errs := []error{fmt.Errorf("another error"), timeoutErr}
    66  
    67  	err := error(newConsistencyResultError(level, enqueued, responded, errs))
    68  
    69  	assert.True(t, strings.HasPrefix(err.Error(),
    70  		"failed to meet consistency level majority with 1/3 success, 3 nodes responded, errors:"))
    71  	assert.Equal(t, timeoutErr, xerrors.InnerError(err))
    72  	assert.True(t, IsTimeoutError(err))
    73  	assert.Equal(t, 3, NumResponded(err))
    74  	assert.Equal(t, 1, NumSuccess(err))
    75  	assert.Equal(t, 2, NumError(err))
    76  }
    77  
    78  func TestConsistencyResultTchannelTimeoutError(t *testing.T) {
    79  	timeoutErr := xerrors.NewRenamedError(tchannel.ErrTimeout, fmt.Errorf("error"))
    80  
    81  	level := topology.ReadConsistencyLevelMajority
    82  	enqueued := 3
    83  	responded := 3
    84  	errs := []error{fmt.Errorf("another error"), timeoutErr}
    85  
    86  	err := error(newConsistencyResultError(level, enqueued, responded, errs))
    87  
    88  	assert.True(t, strings.HasPrefix(err.Error(),
    89  		"failed to meet consistency level majority with 1/3 success, 3 nodes responded, errors:"))
    90  	assert.Equal(t, timeoutErr, xerrors.InnerError(err))
    91  	assert.True(t, IsTimeoutError(err))
    92  	assert.Equal(t, 3, NumResponded(err))
    93  	assert.Equal(t, 1, NumSuccess(err))
    94  	assert.Equal(t, 2, NumError(err))
    95  }