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)
ctx, cancel := context.WithTimeout(context.Background(), time.Second) for i := 0; i < 100; i++ {
cancel() ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, err := b.AllowCtx(ctx) cancel()
assert.ErrorIs(t, err, context.Canceled) _, err := b.AllowCtx(ctx)
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)
ctx, cancel := context.WithTimeout(context.Background(), time.Second) for i := 0; i < 100; i++ {
cancel() ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err := b.DoCtx(ctx, func() error { cancel()
err := b.DoCtx(ctx, func() error {
return nil
})
assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoCtx(context.Background(), func() error {
return nil return nil
}) }))
assert.ErrorIs(t, err, context.Canceled)
}) })
} }
@@ -135,14 +144,21 @@ 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)
ctx, cancel := context.WithTimeout(context.Background(), time.Second) for i := 0; i < 100; i++ {
cancel() ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err := b.DoWithAcceptableCtx(ctx, func() error { cancel()
err := b.DoWithAcceptableCtx(ctx, func() error {
return nil
}, func(err error) bool {
return true
})
assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoWithAcceptableCtx(context.Background(), func() error {
return nil return nil
}, func(err error) bool { }, func(err error) bool {
return true return true
}) }))
assert.ErrorIs(t, err, context.Canceled)
}) })
} }
@@ -186,14 +202,21 @@ 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)
ctx, cancel := context.WithTimeout(context.Background(), time.Second) for i := 0; i < 100; i++ {
cancel() ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err := b.DoWithFallbackCtx(ctx, func() error { cancel()
err := b.DoWithFallbackCtx(ctx, func() error {
return nil
}, func(err error) error {
return err
})
assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoWithFallbackCtx(context.Background(), func() error {
return nil return nil
}, func(err error) error { }, func(err error) error {
return err return err
}) }))
assert.ErrorIs(t, err, context.Canceled)
}) })
} }
@@ -243,16 +266,25 @@ 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)
ctx, cancel := context.WithTimeout(context.Background(), time.Second) for i := 0; i < 100; i++ {
cancel() ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err := b.DoWithFallbackAcceptableCtx(ctx, func() error { cancel()
err := b.DoWithFallbackAcceptableCtx(ctx, func() error {
return nil
}, func(err error) error {
return err
}, func(err error) bool {
return true
})
assert.ErrorIs(t, err, context.Canceled)
}
assert.NoError(t, b.DoWithFallbackAcceptableCtx(context.Background(), func() error {
return nil return nil
}, func(err error) error { }, func(err error) error {
return err return err
}, func(err error) bool { }, func(err error) bool {
return true return true
}) }))
assert.ErrorIs(t, err, context.Canceled)
}) })
} }