4 // Copyright 2014 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
8 // Test that dequeuing from a pending channel doesn't
19 // checkLinear asserts that the running time of f(n) is in O(n).
20 // tries is the initial number of iterations.
21 func checkLinear(typ string, tries int, f func(n int)) {
22 // Depending on the machine and OS, this test might be too fast
23 // to measure with accurate enough granularity. On failure,
24 // make it run longer, hoping that the timing granularity
25 // is eventually sufficient.
27 timeF := func(n int) time.Duration {
43 // should be 2x (linear); allow up to 3x
46 fmt.Println(typ, "\t", time.Since(t0))
50 // If n ops run in under a second and the ratio
51 // doesn't work out, make n bigger, trying to reduce
52 // the effect that a constant amount of overhead has
53 // on the computed ratio.
54 if t1 < 1*time.Second {
58 // Once the test runs long enough for n ops,
59 // try to get the right ratio at least once.
60 // If five in a row all fail, give up.
61 if fails++; fails >= 5 {
62 panic(fmt.Sprintf("%s: too slow: %d channels: %v; %d channels: %v\n",
69 checkLinear("chanSelect", 1000, func(n int) {
71 c := make(chan bool) // global channel
72 var a []chan bool // local channels for each goroutine
73 for i := 0; i < n; i++ {
77 for j := 0; j < messages; j++ {
78 // queue ourselves on the global channel
86 for i := 0; i < messages; i++ {
87 // wake each goroutine up, forcing it to dequeue and then enqueue
88 // on the global channel.