netbird/relay/healthcheck/sender_test.go

67 lines
1.4 KiB
Go
Raw Normal View History

2024-06-27 18:40:12 +02:00
package healthcheck
import (
"context"
"os"
2024-06-27 18:40:12 +02:00
"testing"
"time"
)
2024-07-08 21:34:39 +02:00
func TestMain(m *testing.M) {
2024-06-27 18:40:12 +02:00
// override the health check interval to speed up the test
healthCheckInterval = 1 * time.Second
healthCheckTimeout = 100 * time.Millisecond
code := m.Run()
os.Exit(code)
2024-07-08 21:34:39 +02:00
}
2024-06-27 18:40:12 +02:00
2024-07-08 21:34:39 +02:00
func TestNewHealthPeriod(t *testing.T) {
2024-06-27 18:40:12 +02:00
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) {
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)
2024-07-08 21:34:39 +02:00
time.Sleep(300 * time.Millisecond)
2024-06-27 18:40:12 +02:00
cancel()
select {
case <-hc.HealthCheck:
2024-07-08 21:34:39 +02:00
t.Fatalf("health check on received")
2024-06-27 18:40:12 +02:00
case <-hc.Timeout:
2024-07-08 21:34:39 +02:00
t.Fatalf("health check timedout")
2024-06-27 18:40:12 +02:00
case <-ctx.Done():
// expected
case <-time.After(1 * time.Second):
t.Fatalf("is not exited")
}
}