Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8326a3a

Browse files
authored
chore: change mock clock to allow Advance() within timer/tick functions (#13500)
1 parent7c081dc commit8326a3a

File tree

10 files changed

+424
-217
lines changed

10 files changed

+424
-217
lines changed

‎clock/clock.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ type Clock interface {
2626
Now(tags...string) time.Time
2727
// Since returns the time elapsed since t. It is shorthand for Clock.Now().Sub(t).
2828
Since(t time.Time,tags...string) time.Duration
29+
// Until returns the duration until t. It is shorthand for t.Sub(Clock.Now()).
30+
Until(t time.Time,tags...string) time.Duration
2931
}
3032

3133
// Waiter can be waited on for an error.

‎clock/example_test.go‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestExampleTickerFunc(t *testing.T) {
4444
ctx,cancel:=context.WithTimeout(context.Background(),10*time.Second)
4545
defercancel()
4646

47-
mClock:=clock.NewMock()
47+
mClock:=clock.NewMock(t)
4848

4949
// Because the ticker is started on a goroutine, we can't immediately start
5050
// advancing the clock, or we will race with the start of the ticker. If we
@@ -76,9 +76,74 @@ func TestExampleTickerFunc(t *testing.T) {
7676
}
7777

7878
// Now that we know the ticker is started, we can advance the time.
79-
mClock.Advance(time.Hour).MustWait(ctx,t)
79+
mClock.Advance(time.Hour).MustWait(ctx)
8080

8181
iftks:=tc.Ticks();tks!=1 {
8282
t.Fatalf("expected 1 got %d ticks",tks)
8383
}
8484
}
85+
86+
typeexampleLatencyMeasurerstruct {
87+
mu sync.Mutex
88+
lastLatency time.Duration
89+
}
90+
91+
funcnewExampleLatencyMeasurer(ctx context.Context,clk clock.Clock)*exampleLatencyMeasurer {
92+
m:=&exampleLatencyMeasurer{}
93+
clk.TickerFunc(ctx,10*time.Second,func()error {
94+
start:=clk.Now()
95+
// m.doSomething()
96+
latency:=clk.Since(start)
97+
m.mu.Lock()
98+
deferm.mu.Unlock()
99+
m.lastLatency=latency
100+
returnnil
101+
})
102+
returnm
103+
}
104+
105+
func (m*exampleLatencyMeasurer)LastLatency() time.Duration {
106+
m.mu.Lock()
107+
deferm.mu.Unlock()
108+
returnm.lastLatency
109+
}
110+
111+
funcTestExampleLatencyMeasurer(t*testing.T) {
112+
t.Parallel()
113+
114+
// nolint:gocritic // trying to avoid Coder-specific stuff with an eye toward spinning this out
115+
ctx,cancel:=context.WithTimeout(context.Background(),10*time.Second)
116+
defercancel()
117+
118+
mClock:=clock.NewMock(t)
119+
trap:=mClock.Trap().Since()
120+
defertrap.Close()
121+
122+
lm:=newExampleLatencyMeasurer(ctx,mClock)
123+
124+
w:=mClock.Advance(10*time.Second)// triggers first tick
125+
c:=trap.MustWait(ctx)// call to Since()
126+
mClock.Advance(33*time.Millisecond)
127+
c.Release()
128+
w.MustWait(ctx)
129+
130+
ifl:=lm.LastLatency();l!=33*time.Millisecond {
131+
t.Fatalf("expected 33ms got %s",l.String())
132+
}
133+
134+
// Next tick is in 10s - 33ms, but if we don't want to calculate, we can use:
135+
d,w2:=mClock.AdvanceNext()
136+
c=trap.MustWait(ctx)
137+
mClock.Advance(17*time.Millisecond)
138+
c.Release()
139+
w2.MustWait(ctx)
140+
141+
expectedD:=10*time.Second-33*time.Millisecond
142+
ifd!=expectedD {
143+
t.Fatalf("expected %s got %s",expectedD.String(),d.String())
144+
}
145+
146+
ifl:=lm.LastLatency();l!=17*time.Millisecond {
147+
t.Fatalf("expected 17ms got %s",l.String())
148+
}
149+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp