github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/model/errors_test.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package model
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	cerror "github.com/pingcap/tiflow/pkg/errors"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestIsChangefeedNotRetryError(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	cases := []struct {
    28  		err    RunningError
    29  		result bool
    30  	}{
    31  		{
    32  			RunningError{
    33  				Addr:    "",
    34  				Code:    string(cerror.ErrAPIGetPDClientFailed.RFCCode()),
    35  				Message: cerror.ErrAPIGetPDClientFailed.Error(),
    36  			},
    37  			false,
    38  		},
    39  		{
    40  			RunningError{
    41  				Addr:    "",
    42  				Code:    string(cerror.ErrExpressionColumnNotFound.RFCCode()),
    43  				Message: cerror.ErrExpressionColumnNotFound.Error(),
    44  			},
    45  			true,
    46  		},
    47  		{
    48  			RunningError{
    49  				Addr:    "",
    50  				Code:    string(cerror.ErrExpressionParseFailed.RFCCode()),
    51  				Message: cerror.ErrExpressionParseFailed.Error(),
    52  			},
    53  			true,
    54  		},
    55  	}
    56  
    57  	for _, c := range cases {
    58  		require.Equal(t, c.result, c.err.ShouldFailChangefeed())
    59  	}
    60  }
    61  
    62  func TestRunningErrorScan(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	timeNow := time.Now()
    66  	timeNowJSON, err := timeNow.MarshalJSON()
    67  	require.Nil(t, err)
    68  
    69  	newTime := time.Time{}
    70  	err = newTime.UnmarshalJSON(timeNowJSON)
    71  	require.Nil(t, err)
    72  	// timeNow:  2023-10-13 16:48:08.345614 +0800 CST m=+0.027639459
    73  	// newTime:  2023-10-13 16:48:08.345614 +0800 CST
    74  	require.NotEqual(t, timeNow, newTime)
    75  
    76  	cases := []struct {
    77  		err    RunningError
    78  		result string
    79  	}{
    80  		{
    81  			RunningError{
    82  				Time:    timeNow,
    83  				Addr:    "",
    84  				Code:    string(cerror.ErrAPIGetPDClientFailed.RFCCode()),
    85  				Message: cerror.ErrAPIGetPDClientFailed.Error(),
    86  			},
    87  			`{"time":` + string(timeNowJSON) +
    88  				`,"addr":"","code":"CDC:ErrAPIGetPDClientFailed","message":"` +
    89  				cerror.ErrAPIGetPDClientFailed.Error() + `"}`,
    90  		},
    91  	}
    92  
    93  	for _, c := range cases {
    94  		v, err := c.err.Value()
    95  		b, ok := v.([]byte)
    96  		require.True(t, ok)
    97  		require.Nil(t, err)
    98  		require.Equal(t, c.result, string(b))
    99  
   100  		var err2 RunningError
   101  		err = err2.Scan(b)
   102  		require.Nil(t, err)
   103  		require.Equal(t, c.err.Addr, err2.Addr)
   104  		require.Equal(t, c.err.Code, err2.Code)
   105  		require.Equal(t, c.err.Message, err2.Message)
   106  	}
   107  }