Skip to content

Commit 11a247d

Browse files
authored
Remove some duplication & use slices package. (#90)
1 parent fe91b0d commit 11a247d

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

clockwork.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package clockwork
44
import (
55
"context"
66
"errors"
7-
"sort"
7+
"slices"
88
"sync"
99
"time"
1010
)
@@ -231,11 +231,7 @@ func (fc *FakeClock) Advance(d time.Duration) {
231231
//
232232
// Deprecated: New code should prefer BlockUntilContext.
233233
func (fc *FakeClock) BlockUntil(n int) {
234-
b := fc.newBlocker(n)
235-
if b == nil {
236-
return
237-
}
238-
<-b.ch
234+
fc.BlockUntilContext(context.TODO(), n)
239235
}
240236

241237
// BlockUntilContext blocks until the fakeClock has the given number of waiters
@@ -281,16 +277,16 @@ func (fc *FakeClock) stop(e expirer) bool {
281277
//
282278
// The caller must hold fc.l.
283279
func (fc *FakeClock) stopExpirer(e expirer) bool {
284-
for i, t := range fc.waiters {
285-
if t == e {
286-
// Remove element, maintaining order.
287-
copy(fc.waiters[i:], fc.waiters[i+1:])
288-
fc.waiters[len(fc.waiters)-1] = nil
289-
fc.waiters = fc.waiters[:len(fc.waiters)-1]
290-
return true
291-
}
280+
idx := slices.Index(fc.waiters, e)
281+
if idx == -1 {
282+
return false
292283
}
293-
return false
284+
// Remove element, maintaining order, setting inaccessible elements to nil so
285+
// they can be garbage collected.
286+
copy(fc.waiters[idx:], fc.waiters[idx+1:])
287+
fc.waiters[len(fc.waiters)-1] = nil
288+
fc.waiters = fc.waiters[:len(fc.waiters)-1]
289+
return true
294290
}
295291

296292
// set sets an expirer to expire at a future point in time.
@@ -315,8 +311,8 @@ func (fc *FakeClock) setExpirer(e expirer, d time.Duration) {
315311
// Add the expirer to the set of waiters and notify any blockers.
316312
e.setExpiry(fc.time.Add(d))
317313
fc.waiters = append(fc.waiters, e)
318-
sort.Slice(fc.waiters, func(i int, j int) bool {
319-
return fc.waiters[i].expiry().Before(fc.waiters[j].expiry())
314+
slices.SortFunc(fc.waiters, func(a, b expirer) int {
315+
return a.expiry().Compare(b.expiry())
320316
})
321317

322318
// Notify blockers of our new waiter.

0 commit comments

Comments
 (0)