47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package health
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
type checkFunc func(context.Context) error
|
|
|
|
func (check checkFunc) Check(ctx context.Context) error { return check(ctx) }
|
|
|
|
func TestReadinessRequiresEveryDependency(t *testing.T) {
|
|
failure := errors.New("dependency unavailable")
|
|
for _, failed := range []int{-1, 0, 1} {
|
|
calls := 0
|
|
checkers := make([]outbound.ReadinessChecker, 0, 2)
|
|
for index := range 2 {
|
|
checkers = append(checkers, checkFunc(func(ctx context.Context) error {
|
|
calls++
|
|
if ctx != t.Context() {
|
|
t.Error("readiness replaced the caller context")
|
|
}
|
|
if index == failed {
|
|
return failure
|
|
}
|
|
return nil
|
|
}))
|
|
}
|
|
err := NewReadiness(checkers...).Check(t.Context())
|
|
if failed < 0 {
|
|
if err != nil || calls != 2 {
|
|
t.Fatalf("healthy dependencies: calls=%d error=%v", calls, err)
|
|
}
|
|
continue
|
|
}
|
|
if !errors.Is(err, failure) {
|
|
t.Fatalf("failed dependency %d: error=%v", failed, err)
|
|
}
|
|
}
|
|
if err := NewReadiness().Check(t.Context()); !errors.Is(err, outbound.ErrNotReady) {
|
|
t.Fatalf("no dependencies: error=%v", err)
|
|
}
|
|
}
|