Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs/tests): clarify WithFailureCondition behavior #9

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions hoglet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ func TestBreaker_ctx_parameter_not_cancelled(t *testing.T) {
assert.NoError(t, ctx.Err())
}

func TestCircuit_ignored_context_cancellation_still_returned(t *testing.T) {
b, err := NewCircuit(
func(ctx context.Context, _ any) (string, error) {
return "expected", ctx.Err()
},
nil,
WithFailureCondition(IgnoreContextCancelation))
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
cancel()

out, err := b.Call(ctx, nil)
assert.ErrorIs(t, err, context.Canceled)
assert.Equal(t, "expected", out)
}

// mockBreaker is a mock implementation of the [Breaker] interface that opens or closes depending on the last observed
// failure.
type mockBreaker struct{}
Expand Down
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func WithHalfOpenDelay(delay time.Duration) Option {
// If the provided function returns true, the error is considered a failure and the breaker may open (depending on the
// breaker logic).
// The default filter considers all non-nil errors as failures (err != nil).
//
// This does not modify the error returned by [Circuit.Call]. It only affects the circuit itself.
func WithFailureCondition(condition func(error) bool) Option {
return optionFunc(func(o *options) error {
o.isFailure = condition
Expand Down