Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(584)

Side by Side Diff: src/pkg/time/sleep_test.go

Issue 7181052: code review 7181052: time: make TestReset more reliable (Closed)
Patch Set: diff -r c3f6b7dd0104 https://go.googlecode.com/hg/ Created 11 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package time_test 5 package time_test
6 6
7 import ( 7 import (
8 "errors" 8 "errors"
9 "fmt" 9 "fmt"
10 "runtime" 10 "runtime"
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 }() 240 }()
241 for i := 0; i < 100; i++ { 241 for i := 0; i < 100; i++ {
242 Sleep(0) 242 Sleep(0)
243 tmp := make(chan bool, 1) 243 tmp := make(chan bool, 1)
244 tmp <- true 244 tmp <- true
245 <-tmp 245 <-tmp
246 } 246 }
247 <-c 247 <-c
248 } 248 }
249 249
250 func TestReset(t *testing.T) { 250 func testReset(d Duration) error {
251 » t0 := NewTimer(100 * Millisecond) 251 » t0 := NewTimer(2 * d)
252 » Sleep(50 * Millisecond) 252 » Sleep(d)
253 » if t0.Reset(150*Millisecond) != true { 253 » if t0.Reset(3*d) != true {
254 » » t.Fatalf("resetting unfired timer returned false") 254 » » return errors.New("resetting unfired timer returned false")
255 } 255 }
256 » Sleep(100 * Millisecond) 256 » Sleep(2 * d)
257 select { 257 select {
258 case <-t0.C: 258 case <-t0.C:
259 » » t.Fatalf("timer fired early") 259 » » return errors.New("timer fired early")
260 default: 260 default:
261 } 261 }
262 » Sleep(100 * Millisecond) 262 » Sleep(2 * d)
263 select { 263 select {
264 case <-t0.C: 264 case <-t0.C:
265 default: 265 default:
266 » » t.Fatalf("reset timer did not fire") 266 » » return errors.New("reset timer did not fire")
267 } 267 }
268 268
269 if t0.Reset(50*Millisecond) != false { 269 if t0.Reset(50*Millisecond) != false {
270 » » t.Fatalf("resetting expired timer returned true") 270 » » return errors.New("resetting expired timer returned true")
271 } 271 }
272 return nil
272 } 273 }
274
275 func TestReset(t *testing.T) {
276 // We try to run this test with increasingly larger multiples
277 // until one works so slow, loaded hardware isn't as flaky,
278 // but without slowing down fast machines unnecessarily.
279 const unit = 25 * Millisecond
280 tries := []Duration{
281 1 * unit,
282 3 * unit,
283 7 * unit,
284 15 * unit,
285 }
286 var err error
287 for _, d := range tries {
288 err = testReset(d)
289 if err == nil {
290 t.Logf("passed using duration %v", d)
291 return
292 }
293 }
294 t.Error(err)
295 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b