mirror of
https://github.com/netbirdio/netbird.git
synced 2025-08-16 18:11:58 +02:00
Add healthcheck code
This commit is contained in:
66
relay/healthcheck/sender_test.go
Normal file
66
relay/healthcheck/sender_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
package healthcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewHealthPeriod(t *testing.T) {
|
||||
// override the health check interval to speed up the test
|
||||
healthCheckInterval = 1 * time.Second
|
||||
healthCheckTimeout = 100 * time.Millisecond
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
hc := NewSender(ctx)
|
||||
|
||||
iterations := 0
|
||||
for i := 0; i < 3; i++ {
|
||||
select {
|
||||
case <-hc.HealthCheck:
|
||||
iterations++
|
||||
hc.OnHCResponse()
|
||||
case <-hc.Timeout:
|
||||
t.Fatalf("health check is timed out")
|
||||
case <-time.After(healthCheckInterval + 100*time.Millisecond):
|
||||
t.Fatalf("health check not received")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHealthFailed(t *testing.T) {
|
||||
// override the health check interval to speed up the test
|
||||
healthCheckInterval = 1 * time.Second
|
||||
healthCheckTimeout = 500 * time.Millisecond
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
hc := NewSender(ctx)
|
||||
|
||||
select {
|
||||
case <-hc.Timeout:
|
||||
case <-time.After(healthCheckInterval + healthCheckTimeout + 100*time.Millisecond):
|
||||
t.Fatalf("health check is not timed out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewHealthcheckStop(t *testing.T) {
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
hc := NewSender(ctx)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
cancel()
|
||||
|
||||
select {
|
||||
case <-hc.HealthCheck:
|
||||
t.Fatalf("is not closed")
|
||||
case <-hc.Timeout:
|
||||
t.Fatalf("is not closed")
|
||||
case <-ctx.Done():
|
||||
// expected
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatalf("is not exited")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user