github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/checkers/cnservice/check_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 cnservice
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/hakeeper"
    19  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    20  	"github.com/stretchr/testify/assert"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  var expiredTick = uint64(hakeeper.DefaultCNStoreTimeout / time.Second * hakeeper.DefaultTickPerSecond)
    26  
    27  func TestParseCNStores(t *testing.T) {
    28  	cases := []struct {
    29  		infos       pb.CNState
    30  		currentTick uint64
    31  
    32  		working []string
    33  		expired []string
    34  	}{
    35  		{
    36  			infos: pb.CNState{
    37  				Stores: map[string]pb.CNStoreInfo{"a": {
    38  					Tick: 0,
    39  				}},
    40  			},
    41  			currentTick: 0,
    42  			working:     []string{"a"},
    43  			expired:     []string{},
    44  		},
    45  		{
    46  			infos: pb.CNState{
    47  				Stores: map[string]pb.CNStoreInfo{"a": {
    48  					Tick: 0,
    49  				}},
    50  			},
    51  			currentTick: expiredTick + 1,
    52  			working:     []string{},
    53  			expired:     []string{"a"},
    54  		},
    55  		{
    56  			infos: pb.CNState{
    57  				Stores: map[string]pb.CNStoreInfo{
    58  					"a": {
    59  						Tick: expiredTick,
    60  					},
    61  					"b": {
    62  						Tick: 0,
    63  					},
    64  				},
    65  			},
    66  			currentTick: expiredTick + 1,
    67  			working:     []string{"a"},
    68  			expired:     []string{"b"},
    69  		},
    70  	}
    71  
    72  	cfg := hakeeper.Config{}
    73  	cfg.Fill()
    74  
    75  	for _, c := range cases {
    76  		working, expired := parseCNStores(cfg, c.infos, c.currentTick)
    77  		assert.Equal(t, c.working, working)
    78  		assert.Equal(t, c.expired, expired)
    79  	}
    80  }