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