| 1 |
package clock |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/suite" |
| 9 |
) |
| 10 |
|
| 11 |
func TestFreezeUnfreeze(t *testing.T) { |
| 12 |
defer Freeze(Now()).Unfreeze() |
| 13 |
} |
| 14 |
|
| 15 |
type FrozenSuite struct { |
| 16 |
suite.Suite |
| 17 |
epoch time.Time |
| 18 |
} |
| 19 |
|
| 20 |
func TestFrozenSuite(t *testing.T) { |
| 21 |
suite.Run(t, new(FrozenSuite)) |
| 22 |
} |
| 23 |
|
| 24 |
func (s *FrozenSuite) SetupSuite() { |
| 25 |
var err error |
| 26 |
s.epoch, err = time.Parse(time.RFC3339, "2009-02-19T00:00:00Z") |
| 27 |
s.Require().NoError(err) |
| 28 |
} |
| 29 |
|
| 30 |
func (s *FrozenSuite) SetupTest() { |
| 31 |
Freeze(s.epoch) |
| 32 |
} |
| 33 |
|
| 34 |
func (s *FrozenSuite) TearDownTest() { |
| 35 |
Unfreeze() |
| 36 |
} |
| 37 |
|
| 38 |
func (s *FrozenSuite) TestAdvanceNow() { |
| 39 |
s.Require().Equal(s.epoch, Now()) |
| 40 |
s.Require().Equal(42*time.Millisecond, Advance(42*time.Millisecond)) |
| 41 |
s.Require().Equal(s.epoch.Add(42*time.Millisecond), Now()) |
| 42 |
s.Require().Equal(55*time.Millisecond, Advance(13*time.Millisecond)) |
| 43 |
s.Require().Equal(74*time.Millisecond, Advance(19*time.Millisecond)) |
| 44 |
s.Require().Equal(s.epoch.Add(74*time.Millisecond), Now()) |
| 45 |
} |
| 46 |
|
| 47 |
func (s *FrozenSuite) TestSleep() { |
| 48 |
hits := make(chan int, 100) |
| 49 |
|
| 50 |
delays := []int{60, 100, 90, 131, 999, 5} |
| 51 |
for i, tc := range []struct { |
| 52 |
desc string |
| 53 |
fn func(delayMs int) |
| 54 |
}{{ |
| 55 |
desc: "Sleep", |
| 56 |
fn: func(delay int) { |
| 57 |
Sleep(time.Duration(delay) * time.Millisecond) |
| 58 |
hits <- delay |
| 59 |
}, |
| 60 |
}, { |
| 61 |
desc: "After", |
| 62 |
fn: func(delay int) { |
| 63 |
<-After(time.Duration(delay) * time.Millisecond) |
| 64 |
hits <- delay |
| 65 |
}, |
| 66 |
}, { |
| 67 |
desc: "AfterFunc", |
| 68 |
fn: func(delay int) { |
| 69 |
AfterFunc(time.Duration(delay)*time.Millisecond, |
| 70 |
func() { |
| 71 |
hits <- delay |
| 72 |
}) |
| 73 |
}, |
| 74 |
}, { |
| 75 |
desc: "NewTimer", |
| 76 |
fn: func(delay int) { |
| 77 |
t := NewTimer(time.Duration(delay) * time.Millisecond) |
| 78 |
<-t.C() |
| 79 |
hits <- delay |
| 80 |
}, |
| 81 |
}} { |
| 82 |
fmt.Printf("Test case #%d: %s", i, tc.desc) |
| 83 |
for _, delay := range delays { |
| 84 |
go tc.fn(delay) |
| 85 |
} |
| 86 |
// Spin-wait for all goroutines to fall asleep. |
| 87 |
ft := provider.(*frozenTime) |
| 88 |
for { |
| 89 |
var brk bool |
| 90 |
ft.mu.Lock() |
| 91 |
if len(ft.timers) == len(delays) { |
| 92 |
brk = true |
| 93 |
} |
| 94 |
ft.mu.Unlock() |
| 95 |
if brk { |
| 96 |
break |
| 97 |
} |
| 98 |
time.Sleep(10 * time.Millisecond) |
| 99 |
} |
| 100 |
|
| 101 |
runningMs := 0 |
| 102 |
for i, delayMs := range []int{5, 60, 90, 100, 131, 999} { |
| 103 |
fmt.Printf("Checking timer #%d, delay=%d\n", i, delayMs) |
| 104 |
delta := delayMs - runningMs - 1 |
| 105 |
Advance(time.Duration(delta) * time.Millisecond) |
| 106 |
// Check before each timer deadline that it is not triggered yet. |
| 107 |
s.assertHits(hits, []int{}) |
| 108 |
|
| 109 |
// When |
| 110 |
Advance(1 * time.Millisecond) |
| 111 |
|
| 112 |
// Then |
| 113 |
s.assertHits(hits, []int{delayMs}) |
| 114 |
|
| 115 |
runningMs += delta + 1 |
| 116 |
} |
| 117 |
|
| 118 |
Advance(1000 * time.Millisecond) |
| 119 |
s.assertHits(hits, []int{}) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Timers scheduled to trigger at the same time do that in the order they were |
| 124 |
// created. |
| 125 |
func (s *FrozenSuite) TestSameTime() { |
| 126 |
var hits []int |
| 127 |
|
| 128 |
AfterFunc(100, func() { hits = append(hits, 3) }) |
| 129 |
AfterFunc(100, func() { hits = append(hits, 1) }) |
| 130 |
AfterFunc(99, func() { hits = append(hits, 2) }) |
| 131 |
AfterFunc(100, func() { hits = append(hits, 5) }) |
| 132 |
AfterFunc(101, func() { hits = append(hits, 4) }) |
| 133 |
AfterFunc(101, func() { hits = append(hits, 6) }) |
| 134 |
|
| 135 |
// When |
| 136 |
Advance(100) |
| 137 |
|
| 138 |
// Then |
| 139 |
s.Require().Equal([]int{2, 3, 1, 5}, hits) |
| 140 |
} |
| 141 |
|
| 142 |
func (s *FrozenSuite) TestTimerStop() { |
| 143 |
hits := []int{} |
| 144 |
|
| 145 |
AfterFunc(100, func() { hits = append(hits, 1) }) |
| 146 |
t := AfterFunc(100, func() { hits = append(hits, 2) }) |
| 147 |
AfterFunc(100, func() { hits = append(hits, 3) }) |
| 148 |
Advance(99) |
| 149 |
s.Require().Equal([]int{}, hits) |
| 150 |
|
| 151 |
// When |
| 152 |
active1 := t.Stop() |
| 153 |
active2 := t.Stop() |
| 154 |
|
| 155 |
// Then |
| 156 |
s.Require().Equal(true, active1) |
| 157 |
s.Require().Equal(false, active2) |
| 158 |
Advance(1) |
| 159 |
s.Require().Equal([]int{1, 3}, hits) |
| 160 |
} |
| 161 |
|
| 162 |
func (s *FrozenSuite) TestReset() { |
| 163 |
hits := []int{} |
| 164 |
|
| 165 |
t1 := AfterFunc(100, func() { hits = append(hits, 1) }) |
| 166 |
t2 := AfterFunc(100, func() { hits = append(hits, 2) }) |
| 167 |
AfterFunc(100, func() { hits = append(hits, 3) }) |
| 168 |
Advance(99) |
| 169 |
s.Require().Equal([]int{}, hits) |
| 170 |
|
| 171 |
// When |
| 172 |
active1 := t1.Reset(1) // Reset to the same time |
| 173 |
active2 := t2.Reset(7) |
| 174 |
|
| 175 |
// Then |
| 176 |
s.Require().Equal(true, active1) |
| 177 |
s.Require().Equal(true, active2) |
| 178 |
|
| 179 |
Advance(1) |
| 180 |
s.Require().Equal([]int{3, 1}, hits) |
| 181 |
Advance(5) |
| 182 |
s.Require().Equal([]int{3, 1}, hits) |
| 183 |
Advance(1) |
| 184 |
s.Require().Equal([]int{3, 1, 2}, hits) |
| 185 |
} |
| 186 |
|
| 187 |
// Reset to the same time just puts the timer at the end of the trigger list |
| 188 |
// for the date. |
| 189 |
func (s *FrozenSuite) TestResetSame() { |
| 190 |
hits := []int{} |
| 191 |
|
| 192 |
t := AfterFunc(100, func() { hits = append(hits, 1) }) |
| 193 |
AfterFunc(100, func() { hits = append(hits, 2) }) |
| 194 |
AfterFunc(100, func() { hits = append(hits, 3) }) |
| 195 |
AfterFunc(101, func() { hits = append(hits, 4) }) |
| 196 |
Advance(9) |
| 197 |
|
| 198 |
// When |
| 199 |
active := t.Reset(91) |
| 200 |
|
| 201 |
// Then |
| 202 |
s.Require().Equal(true, active) |
| 203 |
|
| 204 |
Advance(90) |
| 205 |
s.Require().Equal([]int{}, hits) |
| 206 |
Advance(1) |
| 207 |
s.Require().Equal([]int{2, 3, 1}, hits) |
| 208 |
} |
| 209 |
|
| 210 |
func (s *FrozenSuite) TestTicker() { |
| 211 |
t := NewTicker(100) |
| 212 |
|
| 213 |
Advance(99) |
| 214 |
s.assertNotFired(t.C()) |
| 215 |
Advance(1) |
| 216 |
s.Require().Equal(<-t.C(), s.epoch.Add(100)) |
| 217 |
Advance(750) |
| 218 |
s.Require().Equal(<-t.C(), s.epoch.Add(200)) |
| 219 |
Advance(49) |
| 220 |
s.assertNotFired(t.C()) |
| 221 |
Advance(1) |
| 222 |
s.Require().Equal(<-t.C(), s.epoch.Add(900)) |
| 223 |
|
| 224 |
t.Stop() |
| 225 |
Advance(300) |
| 226 |
s.assertNotFired(t.C()) |
| 227 |
} |
| 228 |
|
| 229 |
func (s *FrozenSuite) TestTickerZero() { |
| 230 |
defer func() { |
| 231 |
recover() |
| 232 |
}() |
| 233 |
|
| 234 |
NewTicker(0) |
| 235 |
s.Fail("Should panic") |
| 236 |
} |
| 237 |
|
| 238 |
func (s *FrozenSuite) TestTick() { |
| 239 |
ch := Tick(100) |
| 240 |
|
| 241 |
Advance(99) |
| 242 |
s.assertNotFired(ch) |
| 243 |
Advance(1) |
| 244 |
s.Require().Equal(<-ch, s.epoch.Add(100)) |
| 245 |
Advance(750) |
| 246 |
s.Require().Equal(<-ch, s.epoch.Add(200)) |
| 247 |
Advance(49) |
| 248 |
s.assertNotFired(ch) |
| 249 |
Advance(1) |
| 250 |
s.Require().Equal(<-ch, s.epoch.Add(900)) |
| 251 |
} |
| 252 |
|
| 253 |
func (s *FrozenSuite) TestTickZero() { |
| 254 |
ch := Tick(0) |
| 255 |
s.Require().Nil(ch) |
| 256 |
} |
| 257 |
|
| 258 |
func (s *FrozenSuite) TestNewStoppedTimer() { |
| 259 |
t := NewStoppedTimer() |
| 260 |
|
| 261 |
// When/Then |
| 262 |
select { |
| 263 |
case <-t.C(): |
| 264 |
s.Fail("Timer should not have fired") |
| 265 |
default: |
| 266 |
} |
| 267 |
s.Require().Equal(false, t.Stop()) |
| 268 |
} |
| 269 |
|
| 270 |
func (s *FrozenSuite) TestWait4Scheduled() { |
| 271 |
After(100 * Millisecond) |
| 272 |
After(100 * Millisecond) |
| 273 |
s.Require().Equal(false, Wait4Scheduled(3, 0)) |
| 274 |
|
| 275 |
startedCh := make(chan struct{}) |
| 276 |
resultCh := make(chan bool) |
| 277 |
go func() { |
| 278 |
close(startedCh) |
| 279 |
resultCh <- Wait4Scheduled(3, 5*Second) |
| 280 |
}() |
| 281 |
// Allow some time for waiter to be set and start waiting for a signal. |
| 282 |
<-startedCh |
| 283 |
time.Sleep(50 * Millisecond) |
| 284 |
|
| 285 |
// When |
| 286 |
After(100 * Millisecond) |
| 287 |
|
| 288 |
// Then |
| 289 |
s.Require().Equal(true, <-resultCh) |
| 290 |
} |
| 291 |
|
| 292 |
// If there is enough timers scheduled already, then a shortcut execution path |
| 293 |
// is taken and Wait4Scheduled returns immediately. |
| 294 |
func (s *FrozenSuite) TestWait4ScheduledImmediate() { |
| 295 |
After(100 * Millisecond) |
| 296 |
After(100 * Millisecond) |
| 297 |
// When/Then |
| 298 |
s.Require().Equal(true, Wait4Scheduled(2, 0)) |
| 299 |
} |
| 300 |
|
| 301 |
func (s *FrozenSuite) TestSince() { |
| 302 |
s.Require().Equal(Duration(0), Since(Now())) |
| 303 |
s.Require().Equal(-Millisecond, Since(Now().Add(Millisecond))) |
| 304 |
s.Require().Equal(Millisecond, Since(Now().Add(-Millisecond))) |
| 305 |
} |
| 306 |
|
| 307 |
func (s *FrozenSuite) TestUntil() { |
| 308 |
s.Require().Equal(Duration(0), Until(Now())) |
| 309 |
s.Require().Equal(Millisecond, Until(Now().Add(Millisecond))) |
| 310 |
s.Require().Equal(-Millisecond, Until(Now().Add(-Millisecond))) |
| 311 |
} |
| 312 |
|
| 313 |
func (s *FrozenSuite) assertHits(got <-chan int, want []int) { |
| 314 |
for i, w := range want { |
| 315 |
var g int |
| 316 |
select { |
| 317 |
case g = <-got: |
| 318 |
case <-time.After(100 * time.Millisecond): |
| 319 |
s.Failf("Missing hit", "want=%v", w) |
| 320 |
return |
| 321 |
} |
| 322 |
s.Require().Equal(w, g, "Hit #%d", i) |
| 323 |
} |
| 324 |
for { |
| 325 |
select { |
| 326 |
case g := <-got: |
| 327 |
s.Failf("Unexpected hit", "got=%v", g) |
| 328 |
default: |
| 329 |
return |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
func (s *FrozenSuite) assertNotFired(ch <-chan time.Time) { |
| 335 |
select { |
| 336 |
case <-ch: |
| 337 |
s.Fail("Premature fire") |
| 338 |
default: |
| 339 |
} |
| 340 |
} |