Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
Rob Pike | 3fb5f32 | 2012-02-19 17:44:02 +1100 | [diff] [blame] | 7 | // Torture test for goroutines. |
| 8 | // Make a lot of goroutines, threaded together, and tear them down cleanly. |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 9 | |
| 10 | package main |
| 11 | |
| 12 | import ( |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 13 | "os" |
| 14 | "strconv" |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
Russ Cox | 08ca30b | 2008-12-19 03:05:37 -0800 | [diff] [blame] | 17 | func f(left, right chan int) { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 18 | left <- <-right |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | func main() { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 22 | var n = 10000 |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 23 | if len(os.Args) > 1 { |
Russ Cox | 44526cd | 2011-11-01 22:06:05 -0400 | [diff] [blame] | 24 | var err error |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 25 | n, err = strconv.Atoi(os.Args[1]) |
Russ Cox | 6cc001c | 2008-11-18 17:12:07 -0800 | [diff] [blame] | 26 | if err != nil { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 27 | print("bad arg\n") |
| 28 | os.Exit(1) |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 31 | leftmost := make(chan int) |
| 32 | right := leftmost |
| 33 | left := leftmost |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 34 | for i := 0; i < n; i++ { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 35 | right = make(chan int) |
| 36 | go f(left, right) |
| 37 | left = right |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 38 | } |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 39 | go func(c chan int) { c <- 1 }(right) |
| 40 | <-leftmost |
Rob Pike | fa2b4cb | 2008-09-30 12:31:47 -0700 | [diff] [blame] | 41 | } |