1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
package clockwork
import (
"context"
"testing"
"time"
)
func TestFakeTickerStop(t *testing.T) {
t.Parallel()
fc := &FakeClock{}
ft := fc.NewTicker(1)
ft.Stop()
select {
case <-ft.Chan():
t.Errorf("received unexpected tick!")
default:
}
}
func TestFakeTickerTick(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
fc := &FakeClock{}
now := fc.Now()
// The tick at now.Add(2) should not get through since we advance time by
// two units below and the channel can hold at most one tick until it's
// consumed.
first := now.Add(1)
second := now.Add(3)
// We wrap the Advance() calls with blockers to make sure that the ticker
// can go to sleep and produce ticks without time passing in parallel.
ft := fc.NewTicker(1)
fc.BlockUntil(1)
fc.Advance(2)
fc.BlockUntil(1)
select {
case tick := <-ft.Chan():
if tick != first {
t.Errorf("wrong tick time, got: %v, want: %v", tick, first)
}
case <-ctx.Done():
t.Errorf("expected tick!")
}
// Advance by one more unit, we should get another tick now.
fc.Advance(1)
fc.BlockUntil(1)
select {
case tick := <-ft.Chan():
if tick != second {
t.Errorf("wrong tick time, got: %v, want: %v", tick, second)
}
case <-ctx.Done():
t.Errorf("expected tick!")
}
ft.Stop()
}
func TestFakeTicker_Race(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
fc := NewFakeClock()
tickTime := 1 * time.Millisecond
ticker := fc.NewTicker(tickTime)
defer ticker.Stop()
fc.Advance(tickTime)
select {
case <-ticker.Chan():
case <-ctx.Done():
t.Fatalf("Ticker didn't detect the clock advance!")
}
}
func TestFakeTicker_Race2(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
fc := NewFakeClock()
ft := fc.NewTicker(5 * time.Second)
for i := 0; i < 100; i++ {
fc.Advance(5 * time.Second)
select {
case <-ft.Chan():
case <-ctx.Done():
t.Fatalf("Ticker didn't detect the clock advance!")
}
}
ft.Stop()
}
func TestFakeTicker_DeliveryOrder(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
fc := NewFakeClock()
ticker := fc.NewTicker(2 * time.Second).Chan()
timer := fc.NewTimer(5 * time.Second).Chan()
go func() {
for j := 0; j < 10; j++ {
fc.BlockUntil(1)
fc.Advance(1 * time.Second)
}
}()
<-ticker
a := <-timer
// Only perform ordering check if ticker channel is drained at first.
select {
case <-ticker:
default:
select {
case b := <-ticker:
if a.After(b) {
t.Fatalf("Expected timer before ticker, got timer %v after %v", a, b)
}
case <-ctx.Done():
t.Fatalf("Expected ticker event didn't arrive!")
}
}
}
|