Skip to content

Commit

Permalink
Simplify and fix Call() for rate breaker.
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
Melraidin authored and rubyist committed Apr 3, 2015
1 parent 2e1dba2 commit e454f25
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Changelog
All notable changes to this project will be documented in this file.

## 2.1.2 - 2015-04-03

### Added
- Nothing

### Deprecated
- Nothing

### Removed
- Nothing

### Fixed
- Simplify Call() for rate breaker, fixing a reset bug

## 2.1.1 - 2014-10-29

### Added
Expand Down
6 changes: 1 addition & 5 deletions circuitbreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,8 @@ func (cb *Breaker) Ready() bool {
// than timeout to run, a failure will be recorded.
func (cb *Breaker) Call(circuit func() error, timeout time.Duration) error {
var err error
state := cb.state()

if state == open {
if !cb.Ready() {
return ErrBreakerOpen
}

Expand All @@ -294,9 +293,6 @@ func (cb *Breaker) Call(circuit func() error, timeout time.Duration) error {
}

if err != nil {
if state == halfopen {
atomic.StoreInt64(&cb.halfOpens, 0)
}
cb.Fail()
return err
}
Expand Down
57 changes: 51 additions & 6 deletions circuitbreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ func TestThresholdBreakerResets(t *testing.T) {
}

time.Sleep(cb.nextBackOff)
err = cb.Call(circuit, 0)
if err != nil {
t.Fatal("Expected cb to be successful")
}
for i := 0; i < 4; i++ {
err = cb.Call(circuit, 0)
if err != nil {
t.Fatal("Expected cb to be successful")
}

if !success {
t.Fatal("Expected cb to have been reset")
if !success {
t.Fatal("Expected cb to have been reset")
}
}
}

Expand Down Expand Up @@ -278,3 +280,46 @@ func TestRateBreakerSampleSize(t *testing.T) {
t.Fatal("expected rate breaker to not be tripped yet")
}
}

func TestRateBreakerResets(t *testing.T) {
serviceError := fmt.Errorf("service error")

called := 0
success := false
circuit := func() error {
if called < 4 {
called++
return serviceError
}
success = true
return nil
}

cb := NewRateBreaker(0.5, 4)
var err error
for i := 0; i < 4; i++ {
err = cb.Call(circuit, 0)
if err == nil {
t.Fatal("Expected cb to return an error (closed breaker, service failure)")
} else if err != serviceError {
t.Fatal("Expected cb to return error from service (closed breaker, service failure)")
}
}

err = cb.Call(circuit, 0)
if err == nil {
t.Fatal("Expected cb to return an error (open breaker)")
} else if err != ErrBreakerOpen {
t.Fatal("Expected cb to return open open breaker error (open breaker)")
}

time.Sleep(cb.nextBackOff)
err = cb.Call(circuit, 0)
if err != nil {
t.Fatal("Expected cb to be successful")
}

if !success {
t.Fatal("Expected cb to have been reset")
}
}

0 comments on commit e454f25

Please sign in to comment.