Skip to content

Commit

Permalink
feat(docs/tests): clarify WithFailureCondition behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Nov 14, 2023
1 parent 0d3bc35 commit 4565e1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit 4565e1b

Please sign in to comment.