2016-12-08 12:32:12 +00:00
|
|
|
package safe
|
|
|
|
|
|
|
|
import (
|
2017-04-30 12:39:49 +00:00
|
|
|
"context"
|
2024-02-07 16:14:07 +00:00
|
|
|
"errors"
|
2017-04-30 12:39:49 +00:00
|
|
|
"sync"
|
2016-12-08 12:32:12 +00:00
|
|
|
"testing"
|
2017-04-30 12:39:49 +00:00
|
|
|
"time"
|
|
|
|
|
2020-02-26 09:36:05 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2016-12-08 12:32:12 +00:00
|
|
|
)
|
|
|
|
|
2017-04-30 12:39:49 +00:00
|
|
|
func TestNewPoolContext(t *testing.T) {
|
|
|
|
type testKeyType string
|
2019-10-09 09:48:04 +00:00
|
|
|
|
2017-04-30 12:39:49 +00:00
|
|
|
testKey := testKeyType("test")
|
2018-10-12 13:40:03 +00:00
|
|
|
|
2017-04-30 12:39:49 +00:00
|
|
|
ctx := context.WithValue(context.Background(), testKey, "test")
|
|
|
|
p := NewPool(ctx)
|
2018-10-12 13:40:03 +00:00
|
|
|
|
2020-02-03 16:56:04 +00:00
|
|
|
p.GoCtx(func(ctx context.Context) {
|
|
|
|
retCtxVal, ok := ctx.Value(testKey).(string)
|
|
|
|
if !ok || retCtxVal != "test" {
|
|
|
|
t.Errorf("Pool.Ctx() did not return a derived context, got %#v, expected context with test value", ctx)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
p.Stop()
|
2017-04-30 12:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fakeRoutine struct {
|
|
|
|
sync.Mutex
|
|
|
|
started bool
|
|
|
|
startSig chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFakeRoutine() *fakeRoutine {
|
|
|
|
return &fakeRoutine{
|
|
|
|
startSig: make(chan bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *fakeRoutine) routineCtx(ctx context.Context) {
|
|
|
|
tr.Lock()
|
|
|
|
tr.started = true
|
|
|
|
tr.Unlock()
|
|
|
|
tr.startSig <- true
|
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPoolWithCtx(t *testing.T) {
|
|
|
|
testRoutine := newFakeRoutine()
|
2018-10-12 13:40:03 +00:00
|
|
|
|
|
|
|
testCases := []struct {
|
2017-04-30 12:39:49 +00:00
|
|
|
desc string
|
|
|
|
fn func(*Pool)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "GoCtx()",
|
|
|
|
fn: func(p *Pool) {
|
|
|
|
p.GoCtx(testRoutine.routineCtx)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-10-12 13:40:03 +00:00
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2017-04-30 12:39:49 +00:00
|
|
|
// These subtests cannot be run in parallel, since the testRoutine
|
|
|
|
// is shared across the subtests.
|
|
|
|
p := NewPool(context.Background())
|
|
|
|
timer := time.NewTimer(500 * time.Millisecond)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
2018-10-12 13:40:03 +00:00
|
|
|
test.fn(p)
|
2020-02-03 16:56:04 +00:00
|
|
|
defer p.Stop()
|
2017-04-30 12:39:49 +00:00
|
|
|
|
|
|
|
testDone := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
<-testRoutine.startSig
|
2020-02-03 16:56:04 +00:00
|
|
|
p.Stop()
|
2017-04-30 12:39:49 +00:00
|
|
|
testDone <- true
|
|
|
|
}()
|
2018-10-12 13:40:03 +00:00
|
|
|
|
2017-04-30 12:39:49 +00:00
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
testRoutine.Lock()
|
|
|
|
defer testRoutine.Unlock()
|
|
|
|
t.Fatalf("Pool test did not complete in time, goroutine started equals '%t'", testRoutine.started)
|
|
|
|
case <-testDone:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:56:04 +00:00
|
|
|
func TestPoolCleanupWithGoPanicking(t *testing.T) {
|
2018-10-12 13:40:03 +00:00
|
|
|
p := NewPool(context.Background())
|
|
|
|
|
2017-04-30 12:39:49 +00:00
|
|
|
timer := time.NewTimer(500 * time.Millisecond)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
2020-02-03 16:56:04 +00:00
|
|
|
p.GoCtx(func(ctx context.Context) {
|
|
|
|
panic("BOOM")
|
|
|
|
})
|
2017-04-30 12:39:49 +00:00
|
|
|
|
|
|
|
testDone := make(chan bool, 1)
|
|
|
|
go func() {
|
2020-02-03 16:56:04 +00:00
|
|
|
p.Stop()
|
2017-04-30 12:39:49 +00:00
|
|
|
testDone <- true
|
|
|
|
}()
|
2018-10-12 13:40:03 +00:00
|
|
|
|
2017-04-30 12:39:49 +00:00
|
|
|
select {
|
|
|
|
case <-timer.C:
|
2020-02-03 16:56:04 +00:00
|
|
|
t.Fatalf("Pool.Cleanup() did not complete in time with a panicking goroutine")
|
2017-04-30 12:39:49 +00:00
|
|
|
case <-testDone:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoroutineRecover(t *testing.T) {
|
|
|
|
// if recover fails the test will panic
|
|
|
|
Go(func() {
|
|
|
|
panic("BOOM")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-08 12:32:12 +00:00
|
|
|
func TestOperationWithRecover(t *testing.T) {
|
2016-12-08 12:32:52 +00:00
|
|
|
operation := func() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := backoff.Retry(OperationWithRecover(operation), &backoff.StopBackOff{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in OperationWithRecover: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperationWithRecoverPanic(t *testing.T) {
|
2016-12-08 12:32:12 +00:00
|
|
|
operation := func() error {
|
|
|
|
panic("BOOM")
|
|
|
|
}
|
|
|
|
err := backoff.Retry(OperationWithRecover(operation), &backoff.StopBackOff{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Error in OperationWithRecover: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 12:32:52 +00:00
|
|
|
|
|
|
|
func TestOperationWithRecoverError(t *testing.T) {
|
|
|
|
operation := func() error {
|
2024-02-07 16:14:07 +00:00
|
|
|
return errors.New("ERROR")
|
2016-12-08 12:32:52 +00:00
|
|
|
}
|
|
|
|
err := backoff.Retry(OperationWithRecover(operation), &backoff.StopBackOff{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Error in OperationWithRecover: %s", err)
|
|
|
|
}
|
|
|
|
}
|