Skip to content

Commit f19a7f5

Browse files
committed
PR comments 2
1 parent 6df4273 commit f19a7f5

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

testutil/retry.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"runtime"
7+
"slices"
78
"sync"
89
"testing"
910
"time"
@@ -83,6 +84,10 @@ func RunRetry(t *testing.T, count int, fn func(t testing.TB)) {
8384
// fakeT is a fake implementation of testing.TB that never fails and only logs
8485
// errors. Fatal errors will cause the goroutine to exit without failing the
8586
// test.
87+
//
88+
// The behavior of the fake implementation should be as close as possible to
89+
// the real implementation from the test function's perspective (minus
90+
// intentionally unimplemented methods).
8691
type fakeT struct {
8792
*testing.T
8893
ctx context.Context
@@ -97,13 +102,13 @@ var _ testing.TB = &fakeT{}
97102

98103
func (t *fakeT) runCleanupFns() {
99104
t.mu.Lock()
100-
cleanupFns := make([]func(), len(t.cleanupFns))
101-
copy(cleanupFns, t.cleanupFns)
105+
cleanupFns := slices.Clone(t.cleanupFns)
102106
t.mu.Unlock()
103107

104-
// Iterate in reverse order to match the behavior of *testing.T.
105-
for i := len(cleanupFns) - 1; i >= 0; i-- {
106-
cleanupFns[i]()
108+
// Execute in LIFO order to match the behavior of *testing.T.
109+
slices.Reverse(cleanupFns)
110+
for _, fn := range cleanupFns {
111+
fn()
107112
}
108113
}
109114

0 commit comments

Comments
 (0)