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
@@ -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