github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/checkers/proxy/check_test.go (about)

     1  // Copyright 2021 - 2023 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 proxy
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/hakeeper"
    22  	"github.com/matrixorigin/matrixone/pkg/hakeeper/operator"
    23  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  var expiredTick = uint64(hakeeper.DefaultProxyStoreTimeout / time.Second * hakeeper.DefaultTickPerSecond)
    28  
    29  func TestParseProxyStores(t *testing.T) {
    30  	cases := []struct {
    31  		infos       pb.ProxyState
    32  		currentTick uint64
    33  
    34  		working []string
    35  		expired []string
    36  	}{
    37  		{
    38  			infos: pb.ProxyState{
    39  				Stores: map[string]pb.ProxyStore{"a": {
    40  					Tick: 0,
    41  				}},
    42  			},
    43  			currentTick: 0,
    44  			working:     []string{"a"},
    45  			expired:     []string{},
    46  		},
    47  		{
    48  			infos: pb.ProxyState{
    49  				Stores: map[string]pb.ProxyStore{"a": {
    50  					Tick: 0,
    51  				}},
    52  			},
    53  			currentTick: expiredTick + 1,
    54  			working:     []string{},
    55  			expired:     []string{"a"},
    56  		},
    57  		{
    58  			infos: pb.ProxyState{
    59  				Stores: map[string]pb.ProxyStore{
    60  					"a": {
    61  						Tick: expiredTick,
    62  					},
    63  					"b": {
    64  						Tick: 0,
    65  					},
    66  				},
    67  			},
    68  			currentTick: expiredTick + 1,
    69  			working:     []string{"a"},
    70  			expired:     []string{"b"},
    71  		},
    72  	}
    73  
    74  	cfg := hakeeper.Config{}
    75  	cfg.Fill()
    76  
    77  	for _, c := range cases {
    78  		working, expired := parseProxyStores(cfg, c.infos, c.currentTick)
    79  		assert.Equal(t, c.working, working)
    80  		assert.Equal(t, c.expired, expired)
    81  	}
    82  }
    83  
    84  func TestCheck(t *testing.T) {
    85  	infos := pb.ProxyState{
    86  		Stores: map[string]pb.ProxyStore{"a": {
    87  			Tick: 0,
    88  		}},
    89  	}
    90  	currentTick := expiredTick + 1
    91  	cfg := hakeeper.Config{}
    92  	cfg.Fill()
    93  	ops := Check(cfg, infos, currentTick)
    94  	assert.Equal(t, 1, len(ops))
    95  	steps := ops[0].OpSteps()
    96  	assert.Equal(t, 1, len(steps))
    97  	s, ok := steps[0].(operator.DeleteProxyStore)
    98  	assert.True(t, ok)
    99  	assert.NotNil(t, s)
   100  	assert.Equal(t, "a", s.StoreID)
   101  }