github.com/polarismesh/polaris@v1.17.8/plugin/healthchecker/leader/checker_leader_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package leader
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/golang/mock/gomock"
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	"github.com/polarismesh/polaris/common/batchjob"
    29  	"github.com/polarismesh/polaris/common/eventhub"
    30  	"github.com/polarismesh/polaris/common/utils"
    31  	"github.com/polarismesh/polaris/plugin"
    32  	"github.com/polarismesh/polaris/store"
    33  	"github.com/polarismesh/polaris/store/mock"
    34  )
    35  
    36  func TestLeaderHealthChecker_OnEvent(t *testing.T) {
    37  	ctrl := gomock.NewController(t)
    38  	eventhub.InitEventHub()
    39  	t.Cleanup(func() {
    40  		ctrl.Finish()
    41  	})
    42  	mockStore := mock.NewMockStore(ctrl)
    43  	mockStore.EXPECT().StartLeaderElection(gomock.Any()).Return(nil).AnyTimes()
    44  
    45  	checker := &LeaderHealthChecker{
    46  		self: NewLocalPeerFunc(),
    47  		s:    mockStore,
    48  		conf: &Config{
    49  			SoltNum: 0,
    50  			Batch: batchjob.CtrlConfig{
    51  				QueueSize:     16,
    52  				WaitTime:      32 * time.Millisecond,
    53  				MaxBatchCount: 32,
    54  				Concurrency:   1,
    55  			},
    56  		},
    57  	}
    58  	err := checker.Initialize(&plugin.ConfigEntry{
    59  		Option: map[string]interface{}{},
    60  	})
    61  	assert.NoError(t, err)
    62  
    63  	mockPort := uint32(28888)
    64  	_, err = newMockPolarisGRPCSever(t, mockPort)
    65  	assert.NoError(t, err)
    66  
    67  	utils.LocalHost = "127.0.0.2"
    68  	utils.LocalPort = int(mockPort)
    69  	t.Cleanup(func() {
    70  		utils.LocalPort = 8091
    71  		utils.LocalHost = "127.0.0.1"
    72  	})
    73  
    74  	t.Run("initialize-self-is-follower", func(t *testing.T) {
    75  		checker.OnEvent(context.Background(), store.LeaderChangeEvent{
    76  			Key:        electionKey,
    77  			Leader:     false,
    78  			LeaderHost: "127.0.0.1",
    79  		})
    80  
    81  		assert.True(t, checker.isInitialize())
    82  		assert.False(t, checker.isLeader())
    83  
    84  		skipRet := checker.skipCheck(utils.NewUUID(), 15)
    85  		assert.True(t, skipRet)
    86  		time.Sleep(15 * time.Second)
    87  		skipRet = checker.skipCheck(utils.NewUUID(), 15)
    88  		assert.False(t, skipRet)
    89  
    90  		peer := checker.findLeaderPeer()
    91  		assert.NotNil(t, peer)
    92  		_, ok := peer.(*RemotePeer)
    93  		assert.True(t, ok)
    94  	})
    95  
    96  	t.Run("initialize-self-become-leader", func(t *testing.T) {
    97  		checker.OnEvent(context.Background(), store.LeaderChangeEvent{
    98  			Key:        electionKey,
    99  			Leader:     true,
   100  			LeaderHost: "127.0.0.2",
   101  		})
   102  
   103  		assert.True(t, checker.isInitialize())
   104  		assert.True(t, checker.isLeader())
   105  		assert.Nil(t, checker.remote)
   106  
   107  		skipRet := checker.skipCheck(utils.NewUUID(), 15)
   108  		assert.True(t, skipRet)
   109  		time.Sleep(15 * time.Second)
   110  		skipRet = checker.skipCheck(utils.NewUUID(), 15)
   111  		assert.False(t, skipRet)
   112  
   113  		peer := checker.findLeaderPeer()
   114  		assert.NotNil(t, peer)
   115  		_, ok := peer.(*LocalPeer)
   116  		assert.True(t, ok)
   117  	})
   118  }