Home | History | Annotate | Download | only in chan
      1 // run
      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 
      7 // Torture test for goroutines.
      8 // Make a lot of goroutines, threaded together, and tear them down cleanly.
      9 
     10 package main
     11 
     12 import (
     13 	"os"
     14 	"strconv"
     15 )
     16 
     17 func f(left, right chan int) {
     18 	left <- <-right
     19 }
     20 
     21 func main() {
     22 	var n = 10000
     23 	if len(os.Args) > 1 {
     24 		var err error
     25 		n, err = strconv.Atoi(os.Args[1])
     26 		if err != nil {
     27 			print("bad arg\n")
     28 			os.Exit(1)
     29 		}
     30 	}
     31 	leftmost := make(chan int)
     32 	right := leftmost
     33 	left := leftmost
     34 	for i := 0; i < n; i++ {
     35 		right = make(chan int)
     36 		go f(left, right)
     37 		left = right
     38 	}
     39 	go func(c chan int) { c <- 1 }(right)
     40 	<-leftmost
     41 }
     42