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

     1  // Copyright 2021 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  	"encoding/json"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/pingcap/tiflow/pkg/errors"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestChangefeedCommonInfoMarshalJSON(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	runningErr := &RunningError{
    29  		time.Now(),
    30  		"",
    31  		string(errors.ErrProcessorUnknown.RFCCode()),
    32  		errors.ErrProcessorUnknown.GetMsg(),
    33  	}
    34  	cfInfo := &ChangefeedCommonInfo{
    35  		ID:           "test",
    36  		FeedState:    StateNormal,
    37  		RunningError: runningErr,
    38  	}
    39  	// when state is normal, the error code is not exist
    40  	cfInfoJSON, err := json.Marshal(cfInfo)
    41  	require.Nil(t, err)
    42  	require.NotContains(t, string(cfInfoJSON), string(errors.ErrProcessorUnknown.RFCCode()))
    43  
    44  	// when state is not normal, the error code is exist
    45  	cfInfo.FeedState = StateWarning
    46  	cfInfoJSON, err = json.Marshal(cfInfo)
    47  	require.Nil(t, err)
    48  	require.Contains(t, string(cfInfoJSON), string(errors.ErrProcessorUnknown.RFCCode()))
    49  }
    50  
    51  func TestChangefeedDetailMarshalJSON(t *testing.T) {
    52  	t.Parallel()
    53  
    54  	runningErr := &RunningError{
    55  		time.Now(),
    56  		"",
    57  		string(errors.ErrProcessorUnknown.RFCCode()),
    58  		errors.ErrProcessorUnknown.GetMsg(),
    59  	}
    60  	cfDetail := &ChangefeedDetail{
    61  		ID:           "test",
    62  		FeedState:    StateNormal,
    63  		RunningError: runningErr,
    64  	}
    65  	// when state is normal, the error code is not exist
    66  	cfInfoJSON, err := json.Marshal(cfDetail)
    67  	require.Nil(t, err)
    68  	require.NotContains(t, string(cfInfoJSON), string(errors.ErrProcessorUnknown.RFCCode()))
    69  
    70  	// when state is not normal, the error code is exist
    71  	cfDetail.FeedState = StateWarning
    72  	cfInfoJSON, err = json.Marshal(cfDetail)
    73  	require.Nil(t, err)
    74  	require.Contains(t, string(cfInfoJSON), string(errors.ErrProcessorUnknown.RFCCode()))
    75  }
    76  
    77  func TestLiveness(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	liveness := LivenessCaptureAlive
    81  
    82  	require.True(t, liveness.Store(LivenessCaptureAlive))
    83  	require.Equal(t, LivenessCaptureAlive, liveness.Load())
    84  
    85  	require.True(t, liveness.Store(LivenessCaptureStopping))
    86  	require.Equal(t, LivenessCaptureStopping, liveness.Load())
    87  
    88  	require.False(t, liveness.Store(LivenessCaptureAlive))
    89  	require.Equal(t, LivenessCaptureStopping, liveness.Load())
    90  }