| 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 t.Parallel() | |
| 17 | |
| 18 Convey(`A testing clock instance`, t, func() { | |
| 19 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) | |
| 20 c := New(now) | |
| 21 | |
| 22 Convey(`Returns the current time.`, func() { | |
| 23 So(c.Now(), ShouldResemble, now) | |
| 24 }) | |
| 25 | |
| 26 Convey(`When sleeping with a time of zero, immediately awakens.`
, func() { | |
| 27 c.Sleep(0) | |
| 28 So(c.Now(), ShouldResemble, now) | |
| 29 }) | |
| 30 | |
| 31 Convey(`When sleeping for a period of time, awakens when signall
ed.`, func() { | |
| 32 sleepingC := make(chan struct{}) | |
| 33 c.SetTimerCallback(func(_ clock.Timer) { | |
| 34 close(sleepingC) | |
| 35 }) | |
| 36 | |
| 37 awakeC := make(chan time.Time) | |
| 38 go func() { | |
| 39 c.Sleep(2 * time.Second) | |
| 40 awakeC <- c.Now() | |
| 41 }() | |
| 42 | |
| 43 <-sleepingC | |
| 44 c.Set(now.Add(1 * time.Second)) | |
| 45 c.Set(now.Add(2 * time.Second)) | |
| 46 So(<-awakeC, ShouldResemble, now.Add(2*time.Second)) | |
| 47 }) | |
| 48 | |
| 49 Convey(`Awakens after a period of time.`, func() { | |
| 50 afterC := c.After(2 * time.Second) | |
| 51 | |
| 52 c.Set(now.Add(1 * time.Second)) | |
| 53 c.Set(now.Add(2 * time.Second)) | |
| 54 So(<-afterC, ShouldResemble, now.Add(2*time.Second)) | |
| 55 }) | |
| 56 }) | |
| 57 } | |
| OLD | NEW |