github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/operator/controller_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 some tests. 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 TestDispatchAndRemoveOperator(t *testing.T) { 31 c := NewController() 32 operator1 := &Operator{shardID: 1} 33 operator2 := &Operator{shardID: 1} 34 operator3 := &Operator{shardID: 2} 35 36 c.Dispatch([]*Operator{operator1}, pb.LogState{}, pb.TNState{}, pb.CNState{}, pb.ProxyState{}) 37 assert.Equal(t, []*Operator{operator1}, c.operators[1]) 38 39 c.Dispatch([]*Operator{operator2}, pb.LogState{}, pb.TNState{}, pb.CNState{}, pb.ProxyState{}) 40 assert.Equal(t, []*Operator{operator1, operator2}, c.operators[1]) 41 42 c.Dispatch([]*Operator{operator3}, pb.LogState{}, pb.TNState{}, pb.CNState{}, pb.ProxyState{}) 43 assert.Equal(t, []*Operator{operator3}, c.operators[2]) 44 45 c.RemoveOperator(operator1) 46 assert.Equal(t, []*Operator{operator2}, c.operators[1]) 47 48 c.RemoveOperator(operator2) 49 assert.Equal(t, []*Operator(nil), c.operators[1]) 50 51 c.RemoveOperator(operator3) 52 assert.Equal(t, []*Operator(nil), c.operators[2]) 53 } 54 55 func TestRemoveFinishedOperator(t *testing.T) { 56 c := NewController() 57 op1 := NewOperator("", 1, 1, AddLogService{ 58 Target: "a", 59 Replica: Replica{ 60 UUID: "d", 61 ShardID: 1, 62 ReplicaID: 4}, 63 }) 64 logState := pb.LogState{ 65 Shards: map[uint64]pb.LogShardInfo{1: { 66 ShardID: 1, 67 Replicas: map[uint64]string{1: "a", 2: "b", 3: "c"}, 68 Epoch: 0, 69 }}, 70 } 71 72 c.Dispatch([]*Operator{op1}, logState, pb.TNState{}, pb.CNState{}, pb.ProxyState{}) 73 assert.Equal(t, []*Operator{op1}, c.GetOperators(1)) 74 75 logState = pb.LogState{ 76 Shards: map[uint64]pb.LogShardInfo{1: { 77 ShardID: 1, 78 Replicas: map[uint64]string{1: "a", 2: "b", 3: "c", 4: "d"}, 79 Epoch: 0, 80 }}, 81 } 82 c.RemoveFinishedOperator(logState, pb.TNState{}, pb.CNState{}, pb.ProxyState{}) 83 assert.Equal(t, []*Operator(nil), c.GetOperators(1)) 84 }