github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/framework/model/worker_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  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestTerminateState(t *testing.T) {
    23  	t.Parallel()
    24  	testCases := []struct {
    25  		code     WorkerState
    26  		expected bool
    27  	}{
    28  		{WorkerStateNormal, false},
    29  		{WorkerStateCreated, false},
    30  		{WorkerStateInit, false},
    31  		{WorkerStateError, true},
    32  		{WorkerStateFinished, true},
    33  		{WorkerStateStopped, true},
    34  	}
    35  	s := &WorkerStatus{}
    36  	for _, tc := range testCases {
    37  		s.State = tc.code
    38  		require.Equal(t, tc.expected, s.InTerminateState())
    39  	}
    40  }
    41  
    42  func TestHasSignificantChange(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	testCases := []struct {
    46  		prev, next *WorkerStatus
    47  		changed    bool
    48  	}{
    49  		{
    50  			prev:    &WorkerStatus{State: WorkerStateInit, ErrorMsg: "1"},
    51  			next:    &WorkerStatus{State: WorkerStateInit, ErrorMsg: "2"},
    52  			changed: true,
    53  		},
    54  		{
    55  			prev:    &WorkerStatus{State: WorkerStateFinished, ErrorMsg: "1"},
    56  			next:    &WorkerStatus{State: WorkerStateFinished, ErrorMsg: "1"},
    57  			changed: false,
    58  		},
    59  		{
    60  			prev:    &WorkerStatus{State: WorkerStateFinished, ErrorMsg: "1", ExtBytes: []byte("1")},
    61  			next:    &WorkerStatus{State: WorkerStateFinished, ErrorMsg: "1", ExtBytes: []byte("2")},
    62  			changed: false,
    63  		},
    64  		{
    65  			prev:    &WorkerStatus{State: WorkerStateFinished, ErrorMsg: "1", ExtBytes: []byte("1")},
    66  			next:    &WorkerStatus{State: WorkerStateFinished, ErrorMsg: "2", ExtBytes: []byte("2")},
    67  			changed: true,
    68  		},
    69  		{
    70  			prev:    &WorkerStatus{State: WorkerStateInit, ErrorMsg: "1"},
    71  			next:    &WorkerStatus{State: WorkerStateNormal, ErrorMsg: "1"},
    72  			changed: true,
    73  		},
    74  	}
    75  
    76  	for _, tc := range testCases {
    77  		changed := tc.prev.HasSignificantChange(tc.next)
    78  		require.Equal(t, tc.changed, changed)
    79  	}
    80  }