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

Commitd153c36

Browse files
committed
feat: add Next() method to mock Clock
1 parent878bf34 commitd153c36

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

‎clock/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,22 @@ w.MustWait(ctx)
188188
// d contains the duration we advanced
189189
```
190190

191+
`d, ok := Peek()` returns the duration until the next event, if any (`ok` is`true`). You can use
192+
this to advance a specific time, regardless of the tickers and timer events:
193+
194+
```go
195+
desired:= time.Minute// time to advance
196+
for desired >0 {
197+
p,ok:= mClock.Peek()
198+
if !ok || p > desired {
199+
mClock.Advance(desired).MustWait(ctx)
200+
break
201+
}
202+
mClock.Advance(p).MustWait(ctx)
203+
desired -= p
204+
}
205+
```
206+
191207
###Traps
192208

193209
A trap allows you to match specific calls into the library while mocking, block their return,

‎clock/mock.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,18 @@ func (m *Mock) AdvanceNext() (time.Duration, AdvanceWaiter) {
357357
returnd,w
358358
}
359359

360+
// Peek returns the duration until the next ticker or timer event and the value
361+
// true, or, if there are no running tickers or timers, it returns zero and
362+
// false.
363+
func (m*Mock)Peek() (d time.Duration,okbool) {
364+
m.mu.Lock()
365+
deferm.mu.Unlock()
366+
ifm.nextTime.IsZero() {
367+
return0,false
368+
}
369+
returnm.nextTime.Sub(m.cur),true
370+
}
371+
360372
// Trapper allows the creation of Traps
361373
typeTrapperstruct {
362374
// mock is the underlying Mock. This is a thin wrapper around Mock so that

‎clock/mock_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestAfterFunc_NegativeDuration(t *testing.T) {
8484
funcTestNewTicker(t*testing.T) {
8585
t.Parallel()
8686
// nolint:gocritic // trying to avoid Coder-specific stuff with an eye toward spinning this out
87-
ctx,cancel:=context.WithTimeout(context.Background(),1000*time.Second)
87+
ctx,cancel:=context.WithTimeout(context.Background(),10*time.Second)
8888
defercancel()
8989

9090
mClock:=clock.NewMock(t)
@@ -167,3 +167,50 @@ func TestNewTicker(t *testing.T) {
167167
}
168168
}
169169
}
170+
171+
funcTestPeek(t*testing.T) {
172+
t.Parallel()
173+
// nolint:gocritic // trying to avoid Coder-specific stuff with an eye toward spinning this out
174+
ctx,cancel:=context.WithTimeout(context.Background(),10*time.Second)
175+
defercancel()
176+
177+
mClock:=clock.NewMock(t)
178+
d,ok:=mClock.Peek()
179+
ifd!=0 {
180+
t.Fatal("expected Peek() to return 0")
181+
}
182+
ifok {
183+
t.Fatal("expected Peek() to return false")
184+
}
185+
186+
tmr:=mClock.NewTimer(time.Second)
187+
d,ok=mClock.Peek()
188+
ifd!=time.Second {
189+
t.Fatal("expected Peek() to return 1s")
190+
}
191+
if!ok {
192+
t.Fatal("expected Peek() to return true")
193+
}
194+
195+
mClock.Advance(999*time.Millisecond).MustWait(ctx)
196+
d,ok=mClock.Peek()
197+
ifd!=time.Millisecond {
198+
t.Fatal("expected Peek() to return 1ms")
199+
}
200+
if!ok {
201+
t.Fatal("expected Peek() to return true")
202+
}
203+
204+
stopped:=tmr.Stop()
205+
if!stopped {
206+
t.Fatal("expected Stop() to return true")
207+
}
208+
209+
d,ok=mClock.Peek()
210+
ifd!=0 {
211+
t.Fatal("expected Peek() to return 0")
212+
}
213+
ifok {
214+
t.Fatal("expected Peek() to return false")
215+
}
216+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp