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

     1  // Copyright 2020 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  	"strings"
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestAdminJobType(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	names := map[AdminJobType]string{
    27  		AdminNone:         "noop",
    28  		AdminStop:         "stop changefeed",
    29  		AdminResume:       "resume changefeed",
    30  		AdminRemove:       "remove changefeed",
    31  		AdminFinish:       "finish changefeed",
    32  		AdminJobType(100): "unknown",
    33  	}
    34  	for job, name := range names {
    35  		require.Equal(t, name, job.String())
    36  	}
    37  
    38  	isStopped := map[AdminJobType]bool{
    39  		AdminNone:   false,
    40  		AdminStop:   true,
    41  		AdminResume: false,
    42  		AdminRemove: true,
    43  		AdminFinish: true,
    44  	}
    45  	for job, stopped := range isStopped {
    46  		require.Equal(t, stopped, job.IsStopState())
    47  	}
    48  }
    49  
    50  func TestTaskPositionMarshal(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	pos := &TaskPosition{
    54  		ResolvedTs:   420875942036766723,
    55  		CheckPointTs: 420875940070686721,
    56  	}
    57  	expected := `{"checkpoint-ts":420875940070686721,"resolved-ts":420875942036766723,"count":0,"error":null,"warning":null}`
    58  
    59  	data, err := pos.Marshal()
    60  	require.Nil(t, err)
    61  	require.Equal(t, expected, data)
    62  	require.Equal(t, expected, pos.String())
    63  
    64  	newPos := &TaskPosition{}
    65  	err = newPos.Unmarshal([]byte(data))
    66  	require.Nil(t, err)
    67  	require.Equal(t, pos, newPos)
    68  }
    69  
    70  func TestChangeFeedStatusMarshal(t *testing.T) {
    71  	t.Parallel()
    72  
    73  	status := &ChangeFeedStatus{
    74  		CheckpointTs: 420875940070686721,
    75  	}
    76  	expected := `{"checkpoint-ts":420875940070686721,
    77  "min-table-barrier-ts":0,"admin-job-type":0}`
    78  	expected = strings.ReplaceAll(expected, "\n", "")
    79  
    80  	data, err := status.Marshal()
    81  	require.Nil(t, err)
    82  	require.Equal(t, expected, data)
    83  
    84  	newStatus := &ChangeFeedStatus{}
    85  	err = newStatus.Unmarshal([]byte(data))
    86  	require.Nil(t, err)
    87  	require.Equal(t, status, newStatus)
    88  }
    89  
    90  func TestTableOperationState(t *testing.T) {
    91  	t.Parallel()
    92  
    93  	processedMap := map[uint64]bool{
    94  		OperDispatched: false,
    95  		OperProcessed:  true,
    96  		OperFinished:   true,
    97  	}
    98  	appliedMap := map[uint64]bool{
    99  		OperDispatched: false,
   100  		OperProcessed:  false,
   101  		OperFinished:   true,
   102  	}
   103  	o := &TableOperation{}
   104  
   105  	for status, processed := range processedMap {
   106  		o.Status = status
   107  		require.Equal(t, processed, o.TableProcessed())
   108  	}
   109  	for status, applied := range appliedMap {
   110  		o.Status = status
   111  		require.Equal(t, applied, o.TableApplied())
   112  	}
   113  
   114  	// test clone nil operation. no-nil clone will be tested in `TestShouldBeDeepCopy`
   115  	var nilTableOper *TableOperation
   116  	require.Nil(t, nilTableOper.Clone())
   117  }
   118  
   119  func TestShouldBeDeepCopy(t *testing.T) {
   120  	t.Parallel()
   121  
   122  	info := TaskStatus{
   123  		Tables: map[TableID]*TableReplicaInfo{
   124  			1: {StartTs: 100},
   125  			2: {StartTs: 100},
   126  			3: {StartTs: 100},
   127  			4: {StartTs: 100},
   128  		},
   129  		Operation: map[TableID]*TableOperation{
   130  			5: {
   131  				Delete: true, BoundaryTs: 6,
   132  			},
   133  			6: {
   134  				Delete: false, BoundaryTs: 7,
   135  			},
   136  		},
   137  		AdminJobType: AdminStop,
   138  	}
   139  
   140  	clone := info.Clone()
   141  	assertIsSnapshot := func() {
   142  		require.Equal(t, map[TableID]*TableReplicaInfo{
   143  			1: {StartTs: 100},
   144  			2: {StartTs: 100},
   145  			3: {StartTs: 100},
   146  			4: {StartTs: 100},
   147  		}, clone.Tables)
   148  		require.Equal(t, map[TableID]*TableOperation{
   149  			5: {
   150  				Delete: true, BoundaryTs: 6,
   151  			},
   152  			6: {
   153  				Delete: false, BoundaryTs: 7,
   154  			},
   155  		}, clone.Operation)
   156  		require.Equal(t, AdminStop, clone.AdminJobType)
   157  	}
   158  
   159  	assertIsSnapshot()
   160  
   161  	info.Tables[7] = &TableReplicaInfo{StartTs: 100}
   162  	info.Operation[7] = &TableOperation{Delete: true, BoundaryTs: 7}
   163  
   164  	info.Operation[5].BoundaryTs = 8
   165  	info.Tables[1].StartTs = 200
   166  
   167  	assertIsSnapshot()
   168  }
   169  
   170  func TestTaskStatusMarshal(t *testing.T) {
   171  	t.Parallel()
   172  
   173  	status := &TaskStatus{
   174  		Tables: map[TableID]*TableReplicaInfo{
   175  			1: {StartTs: 420875942036766723},
   176  		},
   177  	}
   178  	expected := `{"tables":{"1":{"start-ts":420875942036766723}},"operation":null,"admin-job-type":0}`
   179  
   180  	data, err := status.Marshal()
   181  	require.Nil(t, err)
   182  	require.Equal(t, expected, data)
   183  	require.Equal(t, expected, status.String())
   184  
   185  	newStatus := &TaskStatus{}
   186  	err = newStatus.Unmarshal([]byte(data))
   187  	require.Nil(t, err)
   188  	require.Equal(t, status, newStatus)
   189  }