chore: add more tests (#4094)

This commit is contained in:
Kevin Wan
2024-04-19 11:13:23 +08:00
committed by GitHub
parent d371ab5479
commit 5e5123caa3

View File

@@ -45,10 +45,14 @@ func TestCircuitBreaker_Allow(t *testing.T) {
t.Run("allow with ctx cancel", func(t *testing.T) { t.Run("allow with ctx cancel", func(t *testing.T) {
b := NewBreaker() b := NewBreaker()
assert.True(t, len(b.Name()) > 0) assert.True(t, len(b.Name()) > 0)
for i := 0; i < 100; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
cancel() cancel()
_, err := b.AllowCtx(ctx) _, err := b.AllowCtx(ctx)
assert.ErrorIs(t, err, context.Canceled) assert.ErrorIs(t, err, context.Canceled)
}
_, err := b.AllowCtx(context.Background())
assert.NoError(t, err)
}) })
} }
@@ -86,12 +90,17 @@ func TestCircuitBreaker_Do(t *testing.T) {
t.Run("do with ctx cancel", func(t *testing.T) { t.Run("do with ctx cancel", func(t *testing.T) {
b := NewBreaker() b := NewBreaker()
assert.True(t, len(b.Name()) > 0) assert.True(t, len(b.Name()) > 0)
for i := 0; i < 100; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
cancel() cancel()
err := b.DoCtx(ctx, func() error { err := b.DoCtx(ctx, func() error {
return nil return nil
}) })
assert.ErrorIs(t, err, context.Canceled) assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoCtx(context.Background(), func() error {
return nil
}))
}) })
} }
@@ -135,6 +144,7 @@ func TestCircuitBreaker_DoWithAcceptable(t *testing.T) {
t.Run("doWithAcceptable with ctx cancel", func(t *testing.T) { t.Run("doWithAcceptable with ctx cancel", func(t *testing.T) {
b := NewBreaker() b := NewBreaker()
assert.True(t, len(b.Name()) > 0) assert.True(t, len(b.Name()) > 0)
for i := 0; i < 100; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
cancel() cancel()
err := b.DoWithAcceptableCtx(ctx, func() error { err := b.DoWithAcceptableCtx(ctx, func() error {
@@ -143,6 +153,12 @@ func TestCircuitBreaker_DoWithAcceptable(t *testing.T) {
return true return true
}) })
assert.ErrorIs(t, err, context.Canceled) assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoWithAcceptableCtx(context.Background(), func() error {
return nil
}, func(err error) bool {
return true
}))
}) })
} }
@@ -186,6 +202,7 @@ func TestCircuitBreaker_DoWithFallback(t *testing.T) {
t.Run("doWithFallback with ctx cancel", func(t *testing.T) { t.Run("doWithFallback with ctx cancel", func(t *testing.T) {
b := NewBreaker() b := NewBreaker()
assert.True(t, len(b.Name()) > 0) assert.True(t, len(b.Name()) > 0)
for i := 0; i < 100; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
cancel() cancel()
err := b.DoWithFallbackCtx(ctx, func() error { err := b.DoWithFallbackCtx(ctx, func() error {
@@ -194,6 +211,12 @@ func TestCircuitBreaker_DoWithFallback(t *testing.T) {
return err return err
}) })
assert.ErrorIs(t, err, context.Canceled) assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoWithFallbackCtx(context.Background(), func() error {
return nil
}, func(err error) error {
return err
}))
}) })
} }
@@ -243,6 +266,7 @@ func TestCircuitBreaker_DoWithFallbackAcceptable(t *testing.T) {
t.Run("doWithFallbackAcceptable with ctx cancel", func(t *testing.T) { t.Run("doWithFallbackAcceptable with ctx cancel", func(t *testing.T) {
b := NewBreaker() b := NewBreaker()
assert.True(t, len(b.Name()) > 0) assert.True(t, len(b.Name()) > 0)
for i := 0; i < 100; i++ {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
cancel() cancel()
err := b.DoWithFallbackAcceptableCtx(ctx, func() error { err := b.DoWithFallbackAcceptableCtx(ctx, func() error {
@@ -253,6 +277,14 @@ func TestCircuitBreaker_DoWithFallbackAcceptable(t *testing.T) {
return true return true
}) })
assert.ErrorIs(t, err, context.Canceled) assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoWithFallbackAcceptableCtx(context.Background(), func() error {
return nil
}, func(err error) error {
return err
}, func(err error) bool {
return true
}))
}) })
} }