github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/operator/operator_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  // Portions of this file are additionally subject to the following
    15  // copyright.
    16  //
    17  // Copyright (C) 2021 Matrix Origin.
    18  //
    19  // Modified the behavior of the operator.
    20  
    21  package operator
    22  
    23  import (
    24  	"testing"
    25  
    26  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestHasStarted(t *testing.T) {
    31  	op := NewOperator("", 1, 1)
    32  	assert.Equal(t, true, op.HasStarted())
    33  }
    34  
    35  func TestCheckSuccess(t *testing.T) {
    36  	op := NewOperator("", 1, 1, AddLogService{}, RemoveLogService{})
    37  	assert.Equal(t, false, op.CheckSuccess())
    38  
    39  	op.currentStep = 1
    40  	assert.Equal(t, false, op.CheckSuccess())
    41  
    42  	op.currentStep = 2
    43  	assert.Equal(t, true, op.CheckSuccess())
    44  }
    45  
    46  func TestCheck(t *testing.T) {
    47  	op := NewOperator("", 1, 1,
    48  		AddLogService{"a", Replica{"d", 1, 4, 1}},
    49  		RemoveLogService{"a", Replica{"c", 1, 3, 1}})
    50  
    51  	logState := pb.LogState{
    52  		Shards: map[uint64]pb.LogShardInfo{1: {
    53  			ShardID:  1,
    54  			Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"},
    55  			Epoch:    0,
    56  		}},
    57  	}
    58  	currentStep := op.Check(logState, pb.TNState{}, pb.CNState{}, pb.ProxyState{})
    59  
    60  	assert.Equal(t,
    61  		AddLogService{"a", Replica{"d", 1, 4, 1}},
    62  		currentStep)
    63  	assert.NotEqual(t, SUCCESS, op.Status())
    64  
    65  	logState = pb.LogState{
    66  		Shards: map[uint64]pb.LogShardInfo{1: {
    67  			ShardID:  1,
    68  			Replicas: map[uint64]string{1: "a", 2: "b", 3: "c", 4: "d"},
    69  			Epoch:    0,
    70  		}},
    71  	}
    72  	currentStep = op.Check(logState, pb.TNState{}, pb.CNState{}, pb.ProxyState{})
    73  
    74  	assert.Equal(t,
    75  		RemoveLogService{"a", Replica{"c", 1, 3, 1}},
    76  		currentStep)
    77  	assert.NotEqual(t, SUCCESS, op.Status())
    78  
    79  	logState = pb.LogState{
    80  		Shards: map[uint64]pb.LogShardInfo{1: {
    81  			ShardID:  1,
    82  			Replicas: map[uint64]string{1: "a", 2: "b", 4: "d"},
    83  			Epoch:    0,
    84  		}},
    85  	}
    86  	currentStep = op.Check(logState, pb.TNState{}, pb.CNState{}, pb.ProxyState{})
    87  
    88  	assert.Equal(t, nil, currentStep)
    89  	assert.Equal(t, SUCCESS, op.Status())
    90  
    91  	assert.Equal(t, nil, op.Check(pb.LogState{}, pb.TNState{}, pb.CNState{}, pb.ProxyState{}))
    92  }