github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/jobmaster/dm/runtime/worker_status_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 runtime
    15  
    16  import (
    17  	"encoding/json"
    18  	"testing"
    19  	"time"
    20  
    21  	frameModel "github.com/pingcap/tiflow/engine/framework/model"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestWorkerStatus(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	task := "worker_status_test"
    29  	workerID := "worker-id"
    30  	workerStatus := NewWorkerStatus(task, frameModel.WorkerDMDump, workerID, WorkerOffline, 1)
    31  	require.Equal(t, workerStatus.TaskID, task)
    32  	require.Equal(t, workerStatus.ID, workerID)
    33  	require.Equal(t, workerStatus.Unit, frameModel.WorkerDMDump)
    34  	require.Equal(t, workerStatus.Stage, WorkerOffline)
    35  	require.Equal(t, workerStatus.CfgModRevision, uint64(1))
    36  	require.True(t, workerStatus.IsOffline())
    37  	require.True(t, workerStatus.IsTombStone())
    38  	require.False(t, workerStatus.RunAsExpected())
    39  
    40  	workerStatus = InitWorkerStatus(task, frameModel.WorkerDMLoad, workerID)
    41  	require.Equal(t, workerStatus.Unit, frameModel.WorkerDMLoad)
    42  	require.Equal(t, workerStatus.Stage, WorkerCreating)
    43  	require.False(t, workerStatus.IsOffline())
    44  	require.False(t, workerStatus.IsTombStone())
    45  	require.True(t, workerStatus.RunAsExpected())
    46  
    47  	workerStatus = NewWorkerStatus(task, frameModel.WorkerDMSync, workerID, WorkerOnline, 0)
    48  	require.Equal(t, workerStatus.Unit, frameModel.WorkerDMSync)
    49  	require.Equal(t, workerStatus.Stage, WorkerOnline)
    50  	require.False(t, workerStatus.IsOffline())
    51  	require.False(t, workerStatus.IsTombStone())
    52  	require.True(t, workerStatus.RunAsExpected())
    53  
    54  	workerStatus = NewWorkerStatus(task, frameModel.WorkerDMLoad, workerID, WorkerFinished, 0)
    55  	require.Equal(t, workerStatus.Unit, frameModel.WorkerDMLoad)
    56  	require.Equal(t, workerStatus.Stage, WorkerFinished)
    57  	require.False(t, workerStatus.IsOffline())
    58  	require.True(t, workerStatus.IsTombStone())
    59  	require.True(t, workerStatus.RunAsExpected())
    60  
    61  	workerStatus = InitWorkerStatus(task, frameModel.WorkerDMLoad, workerID)
    62  	require.False(t, workerStatus.CreateFailed())
    63  	require.False(t, workerStatus.IsTombStone())
    64  	workerStatus.createdTime = time.Now().Add(-2*HeartbeatInterval - 1)
    65  	require.True(t, workerStatus.CreateFailed())
    66  	require.True(t, workerStatus.IsTombStone())
    67  
    68  	for i, s := range typesStringify {
    69  		ws, ok := toWorkerStage[s]
    70  		require.True(t, ok)
    71  		bs, err := json.Marshal(ws)
    72  		require.NoError(t, err)
    73  		var ws2 WorkerStage
    74  		require.NoError(t, json.Unmarshal(bs, &ws2))
    75  		require.Equal(t, ws, ws2)
    76  		require.Equal(t, ws, WorkerStage(i))
    77  	}
    78  
    79  	ws := WorkerStage(-1)
    80  	require.Equal(t, "Unknown WorkerStage -1", ws.String())
    81  	ws = WorkerStage(1000)
    82  	require.Equal(t, "Unknown WorkerStage 1000", ws.String())
    83  	bs, err := json.Marshal(ws)
    84  	require.NoError(t, err)
    85  	var ws2 WorkerStage
    86  	require.EqualError(t, json.Unmarshal(bs, &ws2), "Unknown WorkerStage Unknown WorkerStage 1000")
    87  	require.Equal(t, WorkerStage(0), ws2)
    88  }