Skip to content

Commit b17d68f

Browse files
committed
Extract fast clock to xtest
1 parent 637deba commit b17d68f

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

internal/topic/topicwriterinternal/writer_reconnector_test.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"testing"
1515
"time"
1616

17-
"github.com/jonboulle/clockwork"
1817
"github.com/stretchr/testify/require"
1918
"go.uber.org/mock/gomock"
2019

@@ -428,19 +427,7 @@ func TestWriterImpl_Reconnect(t *testing.T) {
428427
xtest.TestManyTimesWithName(t, "ReconnectOnErrors", func(t testing.TB) {
429428
ctx := xtest.Context(t)
430429

431-
clock := clockwork.NewFakeClock()
432-
433-
go func() {
434-
for {
435-
if ctx.Err() != nil {
436-
return
437-
}
438-
clock.Advance(time.Second)
439-
time.Sleep(time.Microsecond)
440-
}
441-
}()
442-
443-
w := newTestWriterStopped(WithClock(clock), WithTokenUpdateInterval(time.Duration(math.MaxInt64)))
430+
w := newTestWriterStopped(WithClock(xtest.FastClock(t)), WithTokenUpdateInterval(time.Duration(math.MaxInt64)))
444431

445432
mc := gomock.NewController(t)
446433

internal/xtest/clock.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package xtest
2+
3+
import (
4+
"sync/atomic"
5+
"testing"
6+
"time"
7+
8+
"github.com/jonboulle/clockwork"
9+
)
10+
11+
// FastClock returns fake clock with very fast time speed advanced until end of test
12+
// the clock stops advance at end of test
13+
func FastClock(t testing.TB) clockwork.FakeClock {
14+
clock := clockwork.NewFakeClock()
15+
var needStop atomic.Bool
16+
clockStopped := make(chan struct{})
17+
18+
go func() {
19+
defer close(clockStopped)
20+
21+
for {
22+
if needStop.Load() {
23+
return
24+
}
25+
26+
clock.Advance(time.Second)
27+
time.Sleep(time.Microsecond)
28+
}
29+
}()
30+
31+
t.Cleanup(func() {
32+
needStop.Store(true)
33+
<-clockStopped
34+
})
35+
36+
return clock
37+
}

0 commit comments

Comments
 (0)