use setters and getters for healthCheckInterval and healthCheckTimeout

This commit is contained in:
Maycon Santos
2025-07-20 22:09:03 +02:00
parent 4e737a482b
commit 09d0fea5ca
4 changed files with 44 additions and 20 deletions

View File

@@ -8,7 +8,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var heartbeatTimeout = healthCheckInterval + 10*time.Second var heartbeatTimeout = getHealthCheckInterval() + 10*time.Second
var mux sync.Mutex var mux sync.Mutex
func getHeartBeatTimeout() time.Duration { func getHeartBeatTimeout() time.Duration {

View File

@@ -59,12 +59,12 @@ func TestReceiverHealthCheckAttemptThreshold(t *testing.T) {
for _, tc := range testsCases { for _, tc := range testsCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
originalInterval := healthCheckInterval originalInterval := getHealthCheckInterval()
originalTimeout := getHeartBeatTimeout() originalTimeout := getHeartBeatTimeout()
healthCheckInterval = 1 * time.Second setHealthCheckInterval(1 * time.Second)
setHeartBeatTimeout(healthCheckInterval + 500*time.Millisecond) setHeartBeatTimeout(getHealthCheckInterval() + 500*time.Millisecond)
defer func() { defer func() {
healthCheckInterval = originalInterval setHealthCheckInterval(originalInterval)
setHeartBeatTimeout(originalTimeout) setHeartBeatTimeout(originalTimeout)
}() }()
//nolint:tenv //nolint:tenv
@@ -73,7 +73,7 @@ func TestReceiverHealthCheckAttemptThreshold(t *testing.T) {
receiver := NewReceiver(log.WithField("test_name", tc.name)) 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 { if tc.resetCounterOnce {
receiver.Heartbeat() receiver.Heartbeat()

View File

@@ -19,6 +19,30 @@ var (
healthCheckTimeout = 20 * time.Second 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 // Sender is a healthcheck sender
// It will send healthcheck signal to the receiver // 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 // 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) { func (hc *Sender) StartHealthCheck(ctx context.Context) {
ticker := time.NewTicker(healthCheckInterval) ticker := time.NewTicker(getHealthCheckInterval())
defer ticker.Stop() defer ticker.Stop()
timeoutTicker := time.NewTicker(hc.getTimeoutTime()) timeoutTicker := time.NewTicker(hc.getTimeoutTime())
@@ -94,7 +118,7 @@ func (hc *Sender) StartHealthCheck(ctx context.Context) {
} }
func (hc *Sender) getTimeoutTime() time.Duration { func (hc *Sender) getTimeoutTime() time.Duration {
return healthCheckInterval + healthCheckTimeout return getHealthCheckInterval() + getHealthCheckTimeout()
} }
func getAttemptThresholdFromEnv() int { func getAttemptThresholdFromEnv() int {

View File

@@ -12,8 +12,8 @@ import (
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// override the health check interval to speed up the test // override the health check interval to speed up the test
healthCheckInterval = 2 * time.Second setHealthCheckInterval(2 * time.Second)
healthCheckTimeout = 100 * time.Millisecond setHealthCheckTimeout(100 * time.Millisecond)
code := m.Run() code := m.Run()
os.Exit(code) os.Exit(code)
} }
@@ -32,7 +32,7 @@ func TestNewHealthPeriod(t *testing.T) {
hc.OnHCResponse() hc.OnHCResponse()
case <-hc.Timeout: case <-hc.Timeout:
t.Fatalf("health check is timed out") 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") t.Fatalf("health check not received")
} }
} }
@@ -46,7 +46,7 @@ func TestNewHealthFailed(t *testing.T) {
select { select {
case <-hc.Timeout: 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") t.Fatalf("health check is not timed out")
} }
} }
@@ -89,7 +89,7 @@ func TestTimeoutReset(t *testing.T) {
hc.OnHCResponse() hc.OnHCResponse()
case <-hc.Timeout: case <-hc.Timeout:
t.Fatalf("health check is timed out") 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") t.Fatalf("health check not received")
} }
} }
@@ -118,13 +118,13 @@ func TestSenderHealthCheckAttemptThreshold(t *testing.T) {
for _, tc := range testsCases { for _, tc := range testsCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
originalInterval := healthCheckInterval originalInterval := getHealthCheckInterval()
originalTimeout := healthCheckTimeout originalTimeout := getHealthCheckTimeout()
healthCheckInterval = 1 * time.Second setHealthCheckInterval(1 * time.Second)
healthCheckTimeout = 500 * time.Millisecond setHealthCheckTimeout(500 * time.Millisecond)
defer func() { defer func() {
healthCheckInterval = originalInterval setHealthCheckInterval(originalInterval)
healthCheckTimeout = originalTimeout setHealthCheckTimeout(originalTimeout)
}() }()
//nolint:tenv //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 { select {
case <-sender.Timeout: case <-sender.Timeout: