diff --git a/circuitbreaker_test.go b/circuitbreaker_test.go index 79aa143..96821bd 100644 --- a/circuitbreaker_test.go +++ b/circuitbreaker_test.go @@ -323,15 +323,12 @@ func TestThresholdBreakerResets(t *testing.T) { } func TestTimeoutBreaker(t *testing.T) { - wait := make(chan struct{}) - c := clock.NewMock() - called := int32(0) + var called int32 circuit := func() error { - wait <- struct{}{} atomic.AddInt32(&called, 1) - <-wait + c.Add(time.Millisecond * 3) return nil } @@ -341,19 +338,16 @@ func TestTimeoutBreaker(t *testing.T) { errc := make(chan error) go func() { errc <- cb.Call(circuit, time.Millisecond) }() - <-wait - c.Add(time.Millisecond * 3) - wait <- struct{}{} - err := <-errc if err == nil { t.Fatal("expected timeout breaker to return an error") } + if atomic.LoadInt32(&called) != 1 { + t.Fatal("expected circuit be called once") + } + go cb.Call(circuit, time.Millisecond) - <-wait - c.Add(time.Millisecond * 3) - wait <- struct{}{} if !cb.Tripped() { t.Fatal("expected timeout breaker to be open") diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d3d6d94 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/rubyist/circuitbreaker + +go 1.21.0 + +require ( + github.com/cenkalti/backoff v2.2.1+incompatible + github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a + github.com/peterbourgon/g2s v0.0.0-20170223122336-d4e7ad98afea +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..28e688b --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw= +github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA= +github.com/peterbourgon/g2s v0.0.0-20170223122336-d4e7ad98afea h1:sKwxy1H95npauwu8vtF95vG/syrL0p8fSZo/XlDg5gk= +github.com/peterbourgon/g2s v0.0.0-20170223122336-d4e7ad98afea/go.mod h1:1VcHEd3ro4QMoHfiNl/j7Jkln9+KQuorp0PItHMJYNg=