blob: 6ffae7df65f45406b423113de7d5817fc00b808a [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// run
Rob Pikefa2b4cb2008-09-30 12:31:47 -07002
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 Pike3fb5f322012-02-19 17:44:02 +11007// Torture test for goroutines.
8// Make a lot of goroutines, threaded together, and tear them down cleanly.
Rob Pikefa2b4cb2008-09-30 12:31:47 -07009
10package main
11
12import (
Rob Pike4f61fc92010-09-04 10:36:13 +100013 "os"
14 "strconv"
Rob Pikefa2b4cb2008-09-30 12:31:47 -070015)
16
Russ Cox08ca30b2008-12-19 03:05:37 -080017func f(left, right chan int) {
Rob Pike4f61fc92010-09-04 10:36:13 +100018 left <- <-right
Rob Pikefa2b4cb2008-09-30 12:31:47 -070019}
20
21func main() {
Rob Pike4f61fc92010-09-04 10:36:13 +100022 var n = 10000
Russ Cox918afd942009-05-08 15:21:41 -070023 if len(os.Args) > 1 {
Russ Cox44526cd2011-11-01 22:06:05 -040024 var err error
Rob Pike4f61fc92010-09-04 10:36:13 +100025 n, err = strconv.Atoi(os.Args[1])
Russ Cox6cc001c2008-11-18 17:12:07 -080026 if err != nil {
Rob Pike4f61fc92010-09-04 10:36:13 +100027 print("bad arg\n")
28 os.Exit(1)
Rob Pikefa2b4cb2008-09-30 12:31:47 -070029 }
30 }
Rob Pike4f61fc92010-09-04 10:36:13 +100031 leftmost := make(chan int)
32 right := leftmost
33 left := leftmost
Rob Pikefa2b4cb2008-09-30 12:31:47 -070034 for i := 0; i < n; i++ {
Rob Pike4f61fc92010-09-04 10:36:13 +100035 right = make(chan int)
36 go f(left, right)
37 left = right
Rob Pikefa2b4cb2008-09-30 12:31:47 -070038 }
Rob Pike4f61fc92010-09-04 10:36:13 +100039 go func(c chan int) { c <- 1 }(right)
40 <-leftmost
Rob Pikefa2b4cb2008-09-30 12:31:47 -070041}