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

Unified Diff: common/clock/systemclock_test.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Actually upload the patch. Created 4 years, 10 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
« no previous file with comments | « common/clock/systemclock.go ('k') | common/clock/systemtimer.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/clock/systemclock_test.go
diff --git a/common/clock/systemclock_test.go b/common/clock/systemclock_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..c085ed12526041c0e2fc66c8b700f203a1a2c2dd
--- /dev/null
+++ b/common/clock/systemclock_test.go
@@ -0,0 +1,47 @@
+// Copyright 2016 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 clock
+
+import (
+ "testing"
+ "time"
+
+ "golang.org/x/net/context"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+// timeBase is the amount of time where short-response goroutine events "should
+// happen". This isn't a great measure, since scheduling can take longer. Making
+// this short will make the test run faster at the possible expense of increased
+// raciness. Making this longer will increase test time, but will potentially
+// reduce the change of race-related errors.
+//
+// This should be kept >60ms which is a fairly gratuitous RTC-based scheduler
+// delay (1 hr / 2^16) that some older OSes may be subject to.
+const timeBase = 60 * time.Millisecond
+
+// veryLongTime is a time long enough that it won't feasably happen during the
+// course of test execution.
+const veryLongTime = 1000 * timeBase
+
+// TestSystemClock tests the non-trivial system clock methods.
+func TestSystemClock(t *testing.T) {
+ t.Parallel()
+
+ Convey(`A cancelable Context`, t, func() {
+ c, cancelFunc := context.WithCancel(context.Background())
+ sc := GetSystemClock()
+
+ Convey(`Will perform a full sleep if the Context isn't canceled.`, func() {
+ So(sc.Sleep(c, timeBase), ShouldBeNil)
+ })
+
+ Convey(`Will terminate the Sleep prematurely if the Context is canceled.`, func() {
+ cancelFunc()
+ So(sc.Sleep(c, veryLongTime), ShouldEqual, context.Canceled)
+ })
+ })
+}
« no previous file with comments | « common/clock/systemclock.go ('k') | common/clock/systemtimer.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698