github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/errors_test.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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 logservice
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/lni/dragonboat/v4"
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    26  )
    27  
    28  func TestTimeoutError(t *testing.T) {
    29  	code, _ := toErrorCode(dragonboat.ErrTimeout)
    30  	assert.Equal(t, moerr.ErrDragonboatTimeout, uint16(code))
    31  	code, _ = toErrorCode(dragonboat.ErrTimeoutTooSmall)
    32  	assert.Equal(t, moerr.ErrDragonboatTimeout, uint16(code))
    33  	code, _ = toErrorCode(dragonboat.ErrInvalidDeadline)
    34  	assert.Equal(t, moerr.ErrDragonboatTimeout, uint16(code))
    35  }
    36  
    37  func TestErrorConversion(t *testing.T) {
    38  	ctx := context.TODO()
    39  	mappings := getErrorToCodeMapping()
    40  	for _, rec := range mappings {
    41  		if rec.reverse {
    42  			code, msg := toErrorCode(rec.err)
    43  			resp := pb.Response{
    44  				ErrorCode:    uint32(code),
    45  				ErrorMessage: msg,
    46  			}
    47  			err := toError(ctx, resp)
    48  			assert.Equal(t, err, rec.err)
    49  		}
    50  	}
    51  }
    52  
    53  func TestUnknownErrorIsHandled(t *testing.T) {
    54  	ctx := context.TODO()
    55  	err := moerr.NewDragonboatOtherSystemError(ctx, "test error")
    56  	code, _ := toErrorCode(err)
    57  	assert.Equal(t, moerr.ErrDragonboatOtherSystemError, uint16(code))
    58  }
    59  
    60  func TestIsTempError(t *testing.T) {
    61  	ctx := context.TODO()
    62  	tests := []struct {
    63  		err  error
    64  		temp bool
    65  	}{
    66  		{dragonboat.ErrInvalidOperation, false},
    67  		{dragonboat.ErrInvalidAddress, false},
    68  		{dragonboat.ErrInvalidSession, false},
    69  		{dragonboat.ErrTimeoutTooSmall, false},
    70  		{dragonboat.ErrPayloadTooBig, false},
    71  		{dragonboat.ErrSystemBusy, true},
    72  		{dragonboat.ErrShardClosed, true},
    73  		{dragonboat.ErrShardNotInitialized, true},
    74  		{dragonboat.ErrTimeout, true},
    75  		{dragonboat.ErrClosed, true},
    76  		{dragonboat.ErrCanceled, false},
    77  		{dragonboat.ErrRejected, false},
    78  		{dragonboat.ErrShardNotReady, true},
    79  		{dragonboat.ErrInvalidTarget, false},
    80  		{dragonboat.ErrInvalidRange, false},
    81  		{dragonboat.ErrShardNotFound, true},
    82  
    83  		{moerr.NewNoHAKeeper(ctx), true},
    84  		{moerr.NewNoDB(ctx), false},
    85  	}
    86  	for _, tt := range tests {
    87  		assert.Equal(t, tt.temp, isTempError(tt.err))
    88  	}
    89  }