OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package testclock |
| 6 |
| 7 import ( |
| 8 "testing" |
| 9 "time" |
| 10 |
| 11 . "github.com/smartystreets/goconvey/convey" |
| 12 "infra/libs/clock" |
| 13 ) |
| 14 |
| 15 func TestTestClock(t *testing.T) { |
| 16 Convey(`A testing clock instance`, t, func() { |
| 17 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) |
| 18 c := New(now) |
| 19 |
| 20 Convey(`Returns the current time.`, func() { |
| 21 So(c.Now(), ShouldResemble, now) |
| 22 }) |
| 23 |
| 24 Convey(`When sleeping with a time of zero, immediately awakens.`
, func() { |
| 25 c.Sleep(0) |
| 26 So(c.Now(), ShouldResemble, now) |
| 27 }) |
| 28 |
| 29 Convey(`When sleeping for a period of time, awakens when signall
ed.`, func() { |
| 30 sleepingC := make(chan struct{}) |
| 31 c.SetTimerCallback(func(_ clock.Timer) { |
| 32 close(sleepingC) |
| 33 }) |
| 34 |
| 35 awakeC := make(chan time.Time) |
| 36 go func() { |
| 37 c.Sleep(2 * time.Second) |
| 38 awakeC <- c.Now() |
| 39 }() |
| 40 |
| 41 <-sleepingC |
| 42 c.SetNow(now.Add(1 * time.Second)) |
| 43 c.SetNow(now.Add(2 * time.Second)) |
| 44 So(<-awakeC, ShouldResemble, now.Add(2*time.Second)) |
| 45 }) |
| 46 |
| 47 Convey(`Awakens after a period of time.`, func() { |
| 48 afterC := c.After(2 * time.Second) |
| 49 awakeC := make(chan time.Time) |
| 50 go func() { |
| 51 awakeC <- <-afterC |
| 52 }() |
| 53 |
| 54 c.SetNow(now.Add(1 * time.Second)) |
| 55 c.SetNow(now.Add(2 * time.Second)) |
| 56 So(<-awakeC, ShouldResemble, now.Add(2*time.Second)) |
| 57 }) |
| 58 }) |
| 59 } |
OLD | NEW |