snapshot of https://github.com/ThreeDotsLabs/watermill.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
master / ci (push) Failing after 0s

This commit is contained in:
upstream-mirror
2026-09-15 21:27:46 +05:00
commit ce933d1277
538 changed files with 46504 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
package middleware_test
import (
"errors"
"testing"
"time"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
"github.com/sony/gobreaker"
"github.com/stretchr/testify/assert"
)
func TestCircuitBreaker(t *testing.T) {
t.Parallel()
count := 0
failing := true
h := middleware.NewCircuitBreaker(
gobreaker.Settings{
Name: "test",
Timeout: time.Millisecond * 50,
},
).Middleware(func(msg *message.Message) (messages []*message.Message, e error) {
count++
if failing {
return nil, errors.New("test error")
}
return nil, nil
})
msg := message.NewMessage("1", nil)
// The first 6 calls should fail and increment the count
for i := 0; i < 6; i++ {
_, err := h(msg)
assert.Error(t, err)
}
assert.Equal(t, 6, count)
// The next calls should fail and not increment the count (the circuit breaker is open)
for i := 0; i < 4; i++ {
_, err := h(msg)
assert.Error(t, err)
}
assert.Equal(t, 6, count)
time.Sleep(time.Millisecond * 100)
failing = false
// After a timeout, the Circuit Breaker is closed again
for i := 0; i < 4; i++ {
_, err := h(msg)
assert.NoError(t, err)
}
assert.Equal(t, 10, count)
}