refactor(backend): standardize errors and simplify configuration and test setup
This commit is contained in:
1 parent
1535c19b8c
commit
138666a7bb
92 files changed
+3994
-210
No files matched your search
@@ -8,16 +8,21 @@ import (
|
||||
)
|
||||
|
||||
type Readiness struct {
|
||||
checker outbound.ReadinessChecker
|
||||
checkers []outbound.ReadinessChecker
|
||||
}
|
||||
|
||||
func NewReadiness(checker outbound.ReadinessChecker) *Readiness {
|
||||
return &Readiness{checker: checker}
|
||||
func NewReadiness(checkers ...outbound.ReadinessChecker) *Readiness {
|
||||
return &Readiness{checkers: checkers}
|
||||
}
|
||||
|
||||
func (readiness *Readiness) Check(ctx context.Context) error {
|
||||
if err := readiness.checker.Check(ctx); err != nil {
|
||||
return fmt.Errorf("readiness check failed: %w", err)
|
||||
if len(readiness.checkers) == 0 {
|
||||
return fmt.Errorf("Readiness.Check - no readiness checkers: %w", outbound.ErrNotReady)
|
||||
}
|
||||
for _, checker := range readiness.checkers {
|
||||
if err := checker.Check(ctx); err != nil {
|
||||
return fmt.Errorf("Readiness.Check - check dependency: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user