github.com/matrixorigin/matrixone@v0.7.0/pkg/hakeeper/task/parse_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package task
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/hakeeper"
    22  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  var expiredTick = uint64(hakeeper.DefaultCNStoreTimeout / time.Second * hakeeper.DefaultTickPerSecond)
    27  
    28  func TestParseCNStores(t *testing.T) {
    29  	cases := []struct {
    30  		infos           pb.CNState
    31  		currentTick     uint64
    32  		expectedWorking []string
    33  		expectedExpired []string
    34  	}{
    35  		{
    36  			infos:       pb.CNState{},
    37  			currentTick: 0,
    38  
    39  			expectedWorking: []string{},
    40  			expectedExpired: []string{},
    41  		},
    42  		{
    43  			infos:       pb.CNState{Stores: map[string]pb.CNStoreInfo{"a": {Tick: 0}}},
    44  			currentTick: 0,
    45  
    46  			expectedWorking: []string{"a"},
    47  			expectedExpired: []string{},
    48  		},
    49  		{
    50  			infos: pb.CNState{Stores: map[string]pb.CNStoreInfo{
    51  				"a": {Tick: 0},
    52  				"b": {Tick: expiredTick}}},
    53  			currentTick: expiredTick + 1,
    54  
    55  			expectedWorking: []string{"b"},
    56  			expectedExpired: []string{"a"},
    57  		},
    58  	}
    59  
    60  	for _, c := range cases {
    61  		cfg := hakeeper.Config{}
    62  		cfg.Fill()
    63  		working, expired := parseCNStores(cfg, c.infos, c.currentTick)
    64  		assert.Equal(t, c.expectedWorking, working)
    65  		assert.Equal(t, c.expectedExpired, expired)
    66  	}
    67  }
    68  
    69  func TestContains(t *testing.T) {
    70  	assert.True(t, contains([]string{"a"}, "a"))
    71  	assert.False(t, contains([]string{"a"}, "b"))
    72  }