Skip to content

Commit 5643445

Browse files
authored
Revert "Make maxSleep and maxRetires configurable when building options (#94)" (#116)
This reverts commit 751da59. Per discussion in #93, we don't want to release this change as-is.
1 parent 7b4998f commit 5643445

File tree

4 files changed

+11
-64
lines changed

4 files changed

+11
-64
lines changed

leaks.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ func Find(options ...Option) error {
5656
cur := stack.Current().ID()
5757

5858
opts := buildOpts(options...)
59-
if err := opts.validate(); err != nil {
60-
return err
61-
}
6259
if opts.cleanup != nil {
6360
return errors.New("Cleanup can only be passed to VerifyNone or VerifyTestMain")
6461
}

leaks_test.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var _ = TestingT(testing.TB(nil))
3636
// testOptions passes a shorter max sleep time, used so tests don't wait
3737
// ~1 second in cases where we expect Find to error out.
3838
func testOptions() Option {
39-
return MaxSleepInterval(time.Millisecond)
39+
return maxSleep(time.Millisecond)
4040
}
4141

4242
func TestFind(t *testing.T) {
@@ -60,16 +60,6 @@ func TestFind(t *testing.T) {
6060
err := Find(Cleanup(func(int) { assert.Fail(t, "this should not be called") }))
6161
require.Error(t, err, "Should exit with invalid option")
6262
})
63-
64-
t.Run("Find should return error when maxRetries is less than 0", func(t *testing.T) {
65-
err := Find(MaxRetryAttempts(-1))
66-
require.Error(t, err, "maxRetries should be greater than 0")
67-
})
68-
69-
t.Run("Find should return error when maxSleep is less than 0s", func(t *testing.T) {
70-
err := Find(MaxSleepInterval(time.Duration(-1)))
71-
require.Error(t, err, "maxSleep should be greater than 0s")
72-
})
7363
}
7464

7565
func TestFindRetry(t *testing.T) {

options.go

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package goleak
2222

2323
import (
24-
"errors"
2524
"strings"
2625
"time"
2726

@@ -33,14 +32,10 @@ type Option interface {
3332
apply(*opts)
3433
}
3534

36-
const (
37-
// We retry up to default 20 times if we can't find the goroutine that
38-
// we are looking for.
39-
_defaultRetryAttempts = 20
40-
// In between each retry attempt, sleep for up to default 100 microseconds
41-
// to let any running goroutine completes.
42-
_defaultSleepInterval = 100 * time.Microsecond
43-
)
35+
// We retry up to 20 times if we can't find the goroutine that
36+
// we are looking for. In between each attempt, we will sleep for
37+
// a short while to let any running goroutines complete.
38+
const _defaultRetries = 20
4439

4540
type opts struct {
4641
filters []func(stack.Stack) bool
@@ -58,17 +53,6 @@ func (o *opts) apply(opts *opts) {
5853
opts.cleanup = o.cleanup
5954
}
6055

61-
// validate the options.
62-
func (o *opts) validate() error {
63-
if o.maxRetries < 0 {
64-
return errors.New("maxRetryAttempts should be greater than 0")
65-
}
66-
if o.maxSleep <= 0 {
67-
return errors.New("maxSleepInterval should be greater than 0s")
68-
}
69-
return nil
70-
}
71-
7256
// optionFunc lets us easily write options without a custom type.
7357
type optionFunc func(*opts)
7458

@@ -123,25 +107,12 @@ func IgnoreCurrent() Option {
123107
})
124108
}
125109

126-
// MaxSleepInterval sets the maximum sleep time in-between each retry attempt.
127-
// The sleep duration grows in an exponential backoff, to a maximum of the value specified here.
128-
// If not configured, default to 100 microseconds.
129-
func MaxSleepInterval(d time.Duration) Option {
110+
func maxSleep(d time.Duration) Option {
130111
return optionFunc(func(opts *opts) {
131112
opts.maxSleep = d
132113
})
133114
}
134115

135-
// MaxRetryAttempts sets the retry upper limit.
136-
// When finding extra goroutines, we'll retry until all goroutines complete
137-
// or end up with the maximum retry attempts.
138-
// If not configured, default to 20 times.
139-
func MaxRetryAttempts(num int) Option {
140-
return optionFunc(func(opts *opts) {
141-
opts.maxRetries = num
142-
})
143-
}
144-
145116
func addFilter(f func(stack.Stack) bool) Option {
146117
return optionFunc(func(opts *opts) {
147118
opts.filters = append(opts.filters, f)
@@ -150,8 +121,8 @@ func addFilter(f func(stack.Stack) bool) Option {
150121

151122
func buildOpts(options ...Option) *opts {
152123
opts := &opts{
153-
maxRetries: _defaultRetryAttempts,
154-
maxSleep: _defaultSleepInterval,
124+
maxRetries: _defaultRetries,
125+
maxSleep: 100 * time.Millisecond,
155126
}
156127
opts.filters = append(opts.filters,
157128
isTestStack,
@@ -162,7 +133,6 @@ func buildOpts(options ...Option) *opts {
162133
for _, option := range options {
163134
option.apply(opts)
164135
}
165-
166136
return opts
167137
}
168138

options_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,10 @@ func TestOptionsIgnoreAnyFunction(t *testing.T) {
8888
}
8989
}
9090

91-
func TestBuildOptions(t *testing.T) {
92-
// With default options.
93-
opts := buildOpts()
94-
assert.Equal(t, _defaultSleepInterval, opts.maxSleep, "value of maxSleep not right")
95-
assert.Equal(t, _defaultRetryAttempts, opts.maxRetries, "value of maxRetries not right")
96-
97-
// With customized options.
98-
opts = buildOpts(MaxRetryAttempts(50), MaxSleepInterval(time.Microsecond))
99-
assert.Equal(t, time.Microsecond, opts.maxSleep, "value of maxSleep not right")
100-
assert.Equal(t, 50, opts.maxRetries, "value of maxRetries not right")
101-
}
102-
10391
func TestOptionsRetry(t *testing.T) {
104-
opts := buildOpts(MaxSleepInterval(time.Millisecond), MaxRetryAttempts(50)) // initial attempt + 50 retries = 51
92+
opts := buildOpts()
93+
opts.maxRetries = 50 // initial attempt + 50 retries = 11
94+
opts.maxSleep = time.Millisecond
10595

10696
for i := 0; i < 50; i++ {
10797
assert.True(t, opts.retry(i), "Attempt %v/51 should allow retrying", i)

0 commit comments

Comments
 (0)