mirror of
https://github.com/netbirdio/netbird.git
synced 2026-05-03 15:46:38 +00:00
use setters and getters for healthCheckInterval and healthCheckTimeout
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var heartbeatTimeout = healthCheckInterval + 10*time.Second
|
||||
var heartbeatTimeout = getHealthCheckInterval() + 10*time.Second
|
||||
var mux sync.Mutex
|
||||
|
||||
func getHeartBeatTimeout() time.Duration {
|
||||
|
||||
@@ -59,12 +59,12 @@ func TestReceiverHealthCheckAttemptThreshold(t *testing.T) {
|
||||
|
||||
for _, tc := range testsCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
originalInterval := healthCheckInterval
|
||||
originalInterval := getHealthCheckInterval()
|
||||
originalTimeout := getHeartBeatTimeout()
|
||||
healthCheckInterval = 1 * time.Second
|
||||
setHeartBeatTimeout(healthCheckInterval + 500*time.Millisecond)
|
||||
setHealthCheckInterval(1 * time.Second)
|
||||
setHeartBeatTimeout(getHealthCheckInterval() + 500*time.Millisecond)
|
||||
defer func() {
|
||||
healthCheckInterval = originalInterval
|
||||
setHealthCheckInterval(originalInterval)
|
||||
setHeartBeatTimeout(originalTimeout)
|
||||
}()
|
||||
//nolint:tenv
|
||||
@@ -73,7 +73,7 @@ func TestReceiverHealthCheckAttemptThreshold(t *testing.T) {
|
||||
|
||||
receiver := NewReceiver(log.WithField("test_name", tc.name))
|
||||
|
||||
testTimeout := originalTimeout*time.Duration(tc.threshold) + healthCheckInterval
|
||||
testTimeout := originalTimeout*time.Duration(tc.threshold) + getHealthCheckInterval()
|
||||
|
||||
if tc.resetCounterOnce {
|
||||
receiver.Heartbeat()
|
||||
|
||||
@@ -19,6 +19,30 @@ var (
|
||||
healthCheckTimeout = 20 * time.Second
|
||||
)
|
||||
|
||||
func getHealthCheckInterval() time.Duration {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
return healthCheckInterval
|
||||
}
|
||||
|
||||
func setHealthCheckInterval(interval time.Duration) {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
healthCheckInterval = interval
|
||||
}
|
||||
|
||||
func getHealthCheckTimeout() time.Duration {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
return healthCheckTimeout
|
||||
}
|
||||
|
||||
func setHealthCheckTimeout(timeout time.Duration) {
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
healthCheckTimeout = timeout
|
||||
}
|
||||
|
||||
// Sender is a healthcheck sender
|
||||
// It will send healthcheck signal to the receiver
|
||||
// If the receiver does not receive the signal in a certain time, it will send a timeout signal and stop to work
|
||||
@@ -57,7 +81,7 @@ func (hc *Sender) OnHCResponse() {
|
||||
}
|
||||
|
||||
func (hc *Sender) StartHealthCheck(ctx context.Context) {
|
||||
ticker := time.NewTicker(healthCheckInterval)
|
||||
ticker := time.NewTicker(getHealthCheckInterval())
|
||||
defer ticker.Stop()
|
||||
|
||||
timeoutTicker := time.NewTicker(hc.getTimeoutTime())
|
||||
@@ -94,7 +118,7 @@ func (hc *Sender) StartHealthCheck(ctx context.Context) {
|
||||
}
|
||||
|
||||
func (hc *Sender) getTimeoutTime() time.Duration {
|
||||
return healthCheckInterval + healthCheckTimeout
|
||||
return getHealthCheckInterval() + getHealthCheckTimeout()
|
||||
}
|
||||
|
||||
func getAttemptThresholdFromEnv() int {
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// override the health check interval to speed up the test
|
||||
healthCheckInterval = 2 * time.Second
|
||||
healthCheckTimeout = 100 * time.Millisecond
|
||||
setHealthCheckInterval(2 * time.Second)
|
||||
setHealthCheckTimeout(100 * time.Millisecond)
|
||||
code := m.Run()
|
||||
os.Exit(code)
|
||||
}
|
||||
@@ -32,7 +32,7 @@ func TestNewHealthPeriod(t *testing.T) {
|
||||
hc.OnHCResponse()
|
||||
case <-hc.Timeout:
|
||||
t.Fatalf("health check is timed out")
|
||||
case <-time.After(healthCheckInterval + 100*time.Millisecond):
|
||||
case <-time.After(getHealthCheckInterval() + 100*time.Millisecond):
|
||||
t.Fatalf("health check not received")
|
||||
}
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func TestNewHealthFailed(t *testing.T) {
|
||||
|
||||
select {
|
||||
case <-hc.Timeout:
|
||||
case <-time.After(healthCheckInterval + healthCheckTimeout + 100*time.Millisecond):
|
||||
case <-time.After(getHealthCheckInterval() + getHealthCheckTimeout() + 100*time.Millisecond):
|
||||
t.Fatalf("health check is not timed out")
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func TestTimeoutReset(t *testing.T) {
|
||||
hc.OnHCResponse()
|
||||
case <-hc.Timeout:
|
||||
t.Fatalf("health check is timed out")
|
||||
case <-time.After(healthCheckInterval + 100*time.Millisecond):
|
||||
case <-time.After(getHealthCheckInterval() + 100*time.Millisecond):
|
||||
t.Fatalf("health check not received")
|
||||
}
|
||||
}
|
||||
@@ -118,13 +118,13 @@ func TestSenderHealthCheckAttemptThreshold(t *testing.T) {
|
||||
|
||||
for _, tc := range testsCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
originalInterval := healthCheckInterval
|
||||
originalTimeout := healthCheckTimeout
|
||||
healthCheckInterval = 1 * time.Second
|
||||
healthCheckTimeout = 500 * time.Millisecond
|
||||
originalInterval := getHealthCheckInterval()
|
||||
originalTimeout := getHealthCheckTimeout()
|
||||
setHealthCheckInterval(1 * time.Second)
|
||||
setHealthCheckTimeout(500 * time.Millisecond)
|
||||
defer func() {
|
||||
healthCheckInterval = originalInterval
|
||||
healthCheckTimeout = originalTimeout
|
||||
setHealthCheckInterval(originalInterval)
|
||||
setHealthCheckTimeout(originalTimeout)
|
||||
}()
|
||||
|
||||
//nolint:tenv
|
||||
@@ -155,7 +155,7 @@ func TestSenderHealthCheckAttemptThreshold(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
testTimeout := sender.getTimeoutTime()*time.Duration(tc.threshold) + healthCheckInterval
|
||||
testTimeout := sender.getTimeoutTime()*time.Duration(tc.threshold) + getHealthCheckInterval()
|
||||
|
||||
select {
|
||||
case <-sender.Timeout:
|
||||
|
||||
Reference in New Issue
Block a user