Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 clock | |
| 6 | |
| 7 import ( | |
| 8 "testing" | |
| 9 "time" | |
| 10 | |
| 11 "golang.org/x/net/context" | |
| 12 | |
| 13 . "github.com/smartystreets/goconvey/convey" | |
| 14 ) | |
| 15 | |
| 16 // timeBase is the amount of time where short-response goroutine events "should | |
| 17 // happen". This isn't a great measure, since scheduling can take longer. Making | |
| 18 // this short will make the test run faster at the possible expense of increased | |
| 19 // raciness. Making this longer will increase test time, but will potentially | |
| 20 // reduce the change of race-related errors. | |
| 21 // | |
| 22 // This should be kept >60ms which is a fairly gratuitous RTC-based scheduler | |
| 23 // delay (1 hr / 2^16) that some older OSes may be subject to. | |
| 24 const timeBase = 60 * time.Millisecond | |
|
iannucci
2016/02/11 02:24:04
Note that runtime.Gosched may also be pertinent he
dnj (Google)
2016/02/11 17:22:37
I've used it in a some of the test code. The probl
| |
| 25 | |
| 26 // veryLongTime is a time long enough that it won't feasably happen during the | |
| 27 // course of test execution. | |
| 28 const veryLongTime = 1000 * timeBase | |
| 29 | |
| 30 // TestSystemClock tests the non-trivial system clock methods. | |
| 31 func TestSystemClock(t *testing.T) { | |
| 32 t.Parallel() | |
| 33 | |
| 34 Convey(`A cancelable Context`, t, func() { | |
| 35 c, cancelFunc := context.WithCancel(context.Background()) | |
| 36 sc := GetSystemClock() | |
| 37 | |
| 38 Convey(`Will perform a full sleep if the Context isn't canceled. `, func() { | |
| 39 So(sc.Sleep(c, timeBase), ShouldBeNil) | |
| 40 }) | |
| 41 | |
| 42 Convey(`Will terminate the Sleep prematurely if the Context is c anceled.`, func() { | |
| 43 cancelFunc() | |
| 44 So(sc.Sleep(c, veryLongTime), ShouldEqual, context.Cance led) | |
| 45 }) | |
| 46 }) | |
| 47 } | |
| OLD | NEW |