Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Unified Diff: go/src/infra/libs/clock/testclock/testclock_test.go

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Refactored test timer to trigger off of underlying clock. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/src/infra/libs/clock/testclock/testclock_test.go
diff --git a/go/src/infra/libs/clock/testclock/testclock_test.go b/go/src/infra/libs/clock/testclock/testclock_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..2639d3bd8225d2b32865692ff0da8f97bdb8aa65
--- /dev/null
+++ b/go/src/infra/libs/clock/testclock/testclock_test.go
@@ -0,0 +1,59 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package testclock
+
+import (
+ "testing"
+ "time"
+
+ . "github.com/smartystreets/goconvey/convey"
+ "infra/libs/clock"
+)
+
+func TestTestClock(t *testing.T) {
+ Convey(`A testing clock instance`, t, func() {
+ now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC)
+ c := New(now)
+
+ Convey(`Returns the current time.`, func() {
+ So(c.Now(), ShouldResemble, now)
+ })
+
+ Convey(`When sleeping with a time of zero, immediately awakens.`, func() {
+ c.Sleep(0)
+ So(c.Now(), ShouldResemble, now)
+ })
+
+ Convey(`When sleeping for a period of time, awakens when signalled.`, func() {
+ sleepingC := make(chan struct{})
+ c.SetTimerCallback(func(_ clock.Timer) {
+ close(sleepingC)
+ })
+
+ awakeC := make(chan time.Time)
+ go func() {
+ c.Sleep(2 * time.Second)
+ awakeC <- c.Now()
+ }()
+
+ <-sleepingC
+ c.SetNow(now.Add(1 * time.Second))
+ c.SetNow(now.Add(2 * time.Second))
+ So(<-awakeC, ShouldResemble, now.Add(2*time.Second))
+ })
+
+ Convey(`Awakens after a period of time.`, func() {
+ afterC := c.After(2 * time.Second)
+ awakeC := make(chan time.Time)
+ go func() {
+ awakeC <- <-afterC
+ }()
+
+ c.SetNow(now.Add(1 * time.Second))
+ c.SetNow(now.Add(2 * time.Second))
+ So(<-awakeC, ShouldResemble, now.Add(2*time.Second))
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698