|
| 1 | +package clock_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"sync" |
| 6 | +"testing" |
| 7 | +"time" |
| 8 | + |
| 9 | +"github.com/coder/coder/v2/clock" |
| 10 | +) |
| 11 | + |
| 12 | +typeexampleTickCounterstruct { |
| 13 | +ctx context.Context |
| 14 | +mu sync.Mutex |
| 15 | +ticksint |
| 16 | +clock clock.Clock |
| 17 | +} |
| 18 | + |
| 19 | +func (c*exampleTickCounter)Ticks()int { |
| 20 | +c.mu.Lock() |
| 21 | +deferc.mu.Unlock() |
| 22 | +returnc.ticks |
| 23 | +} |
| 24 | + |
| 25 | +func (c*exampleTickCounter)count() { |
| 26 | +_=c.clock.TickerFunc(c.ctx,time.Hour,func()error { |
| 27 | +c.mu.Lock() |
| 28 | +deferc.mu.Unlock() |
| 29 | +c.ticks++ |
| 30 | +returnnil |
| 31 | +},"mytag") |
| 32 | +} |
| 33 | + |
| 34 | +funcnewExampleTickCounter(ctx context.Context,clk clock.Clock)*exampleTickCounter { |
| 35 | +tc:=&exampleTickCounter{ctx:ctx,clock:clk} |
| 36 | +gotc.count() |
| 37 | +returntc |
| 38 | +} |
| 39 | + |
| 40 | +// TestExampleTickerFunc demonstrates how to test the use of TickerFunc. |
| 41 | +funcTestExampleTickerFunc(t*testing.T) { |
| 42 | +t.Parallel() |
| 43 | +// nolint:gocritic // trying to avoid Coder-specific stuff with an eye toward spinning this out |
| 44 | +ctx,cancel:=context.WithTimeout(context.Background(),10*time.Second) |
| 45 | +defercancel() |
| 46 | + |
| 47 | +mClock:=clock.NewMock() |
| 48 | + |
| 49 | +// Because the ticker is started on a goroutine, we can't immediately start |
| 50 | +// advancing the clock, or we will race with the start of the ticker. If we |
| 51 | +// win that race, the clock gets advanced _before_ the ticker starts, and |
| 52 | +// our ticker will not get a tick. |
| 53 | +// |
| 54 | +// To handle this, we set a trap for the call to TickerFunc(), so that we |
| 55 | +// can assert it has been called before advancing the clock. |
| 56 | +trap:=mClock.Trap().TickerFunc("mytag") |
| 57 | +defertrap.Close() |
| 58 | + |
| 59 | +tc:=newExampleTickCounter(ctx,mClock) |
| 60 | + |
| 61 | +// Here, we wait for our trap to be triggered. |
| 62 | +call,err:=trap.Wait(ctx) |
| 63 | +iferr!=nil { |
| 64 | +t.Fatal("ticker never started") |
| 65 | +} |
| 66 | +// it's good practice to release calls before any possible t.Fatal() calls |
| 67 | +// so that we don't leave dangling goroutines waiting for the call to be |
| 68 | +// released. |
| 69 | +call.Release() |
| 70 | +ifcall.Duration!=time.Hour { |
| 71 | +t.Fatal("unexpected duration") |
| 72 | +} |
| 73 | + |
| 74 | +iftks:=tc.Ticks();tks!=0 { |
| 75 | +t.Fatalf("expected 0 got %d ticks",tks) |
| 76 | +} |
| 77 | + |
| 78 | +// Now that we know the ticker is started, we can advance the time. |
| 79 | +mClock.Advance(time.Hour).MustWait(ctx,t) |
| 80 | + |
| 81 | +iftks:=tc.Ticks();tks!=1 { |
| 82 | +t.Fatalf("expected 1 got %d ticks",tks) |
| 83 | +} |
| 84 | +} |