github.com/polarismesh/polaris@v1.17.8/service/healthcheck/test_export.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 healthcheck
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  
    24  	"github.com/polarismesh/polaris/plugin"
    25  	"github.com/polarismesh/polaris/service/batch"
    26  	"github.com/polarismesh/polaris/store"
    27  )
    28  
    29  func TestInitialize(ctx context.Context, hcOpt *Config, cacheOpen bool, bc *batch.Controller,
    30  	storage store.Store) (*Server, error) {
    31  
    32  	testServer := new(Server)
    33  	testServer.hcOpt = hcOpt
    34  
    35  	if !cacheOpen {
    36  		return nil, fmt.Errorf("[healthcheck]cache not open")
    37  	}
    38  	hcOpt.SetDefault()
    39  	if hcOpt.Open {
    40  		if len(hcOpt.Checkers) > 0 {
    41  			testServer.checkers = make(map[int32]plugin.HealthChecker, len(hcOpt.Checkers))
    42  			for _, entry := range hcOpt.Checkers {
    43  				checker := plugin.GetHealthChecker(entry.Name, &entry)
    44  				if checker == nil {
    45  					return nil, fmt.Errorf("[healthcheck]unknown healthchecker %s", entry.Name)
    46  				}
    47  				// The same health type check plugin can only exist in one
    48  				_, exist := testServer.checkers[int32(checker.Type())]
    49  				if exist {
    50  					return nil, fmt.Errorf("[healthcheck]duplicate healthchecker %s, checkType %d",
    51  						entry.Name, checker.Type())
    52  				}
    53  				testServer.checkers[int32(checker.Type())] = checker
    54  				if nil == testServer.defaultChecker {
    55  					testServer.defaultChecker = checker
    56  				}
    57  			}
    58  		} else {
    59  			return nil, fmt.Errorf("[healthcheck]no checker config")
    60  		}
    61  	}
    62  	testServer.storage = storage
    63  	testServer.bc = bc
    64  
    65  	testServer.localHost = hcOpt.LocalHost
    66  	testServer.history = plugin.GetHistory()
    67  	testServer.discoverEvent = plugin.GetDiscoverEvent()
    68  
    69  	testServer.cacheProvider = newCacheProvider(hcOpt.Service, testServer)
    70  	testServer.timeAdjuster = newTimeAdjuster(ctx, testServer.storage)
    71  	testServer.checkScheduler = newCheckScheduler(ctx, hcOpt.SlotNum, hcOpt.MinCheckInterval,
    72  		hcOpt.MaxCheckInterval, hcOpt.ClientCheckInterval, hcOpt.ClientCheckTtl)
    73  	testServer.dispatcher = newDispatcher(ctx, testServer)
    74  	finishInit = true
    75  	return testServer, testServer.run(ctx)
    76  }