Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // run |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [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 | // Test the situation in which two cases of a select can |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 8 | // both end up running. See http://codereview.appspot.com/180068. |
Rob Pike | 3fb5f32 | 2012-02-19 17:44:02 +1100 | [diff] [blame] | 9 | |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 10 | package main |
| 11 | |
| 12 | import ( |
| 13 | "flag" |
| 14 | "runtime" |
| 15 | ) |
| 16 | |
| 17 | var iterations *int = flag.Int("n", 100000, "number of iterations") |
| 18 | |
| 19 | // sender sends a counter to one of four different channels. If two |
| 20 | // cases both end up running in the same iteration, the same value will be sent |
| 21 | // to two different channels. |
| 22 | func sender(n int, c1, c2, c3, c4 chan<- int) { |
| 23 | defer close(c1) |
| 24 | defer close(c2) |
Russ Cox | 0a5fc261 | 2011-01-21 15:06:44 -0500 | [diff] [blame] | 25 | defer close(c3) |
| 26 | defer close(c4) |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 27 | |
| 28 | for i := 0; i < n; i++ { |
| 29 | select { |
| 30 | case c1 <- i: |
| 31 | case c2 <- i: |
| 32 | case c3 <- i: |
| 33 | case c4 <- i: |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // mux receives the values from sender and forwards them onto another channel. |
Shenghou Ma | faef52c | 2013-06-09 21:50:24 +0800 | [diff] [blame] | 39 | // It would be simpler to just have sender's four cases all be the same |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 40 | // channel, but this doesn't actually trigger the bug. |
Russ Cox | 0a5fc261 | 2011-01-21 15:06:44 -0500 | [diff] [blame] | 41 | func mux(out chan<- int, in <-chan int, done chan<- bool) { |
| 42 | for v := range in { |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 43 | out <- v |
| 44 | } |
Russ Cox | 0a5fc261 | 2011-01-21 15:06:44 -0500 | [diff] [blame] | 45 | done <- true |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // recver gets a steam of values from the four mux's and checks for duplicates. |
| 49 | func recver(in <-chan int) { |
| 50 | seen := make(map[int]bool) |
| 51 | |
Russ Cox | 0a5fc261 | 2011-01-21 15:06:44 -0500 | [diff] [blame] | 52 | for v := range in { |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 53 | if _, ok := seen[v]; ok { |
Russ Cox | 00f9f0c | 2010-03-30 10:34:57 -0700 | [diff] [blame] | 54 | println("got duplicate value: ", v) |
| 55 | panic("fail") |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 56 | } |
| 57 | seen[v] = true |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func main() { |
| 62 | runtime.GOMAXPROCS(2) |
| 63 | |
| 64 | c1 := make(chan int) |
| 65 | c2 := make(chan int) |
| 66 | c3 := make(chan int) |
| 67 | c4 := make(chan int) |
Russ Cox | 0a5fc261 | 2011-01-21 15:06:44 -0500 | [diff] [blame] | 68 | done := make(chan bool) |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 69 | cmux := make(chan int) |
| 70 | go sender(*iterations, c1, c2, c3, c4) |
Russ Cox | 0a5fc261 | 2011-01-21 15:06:44 -0500 | [diff] [blame] | 71 | go mux(cmux, c1, done) |
| 72 | go mux(cmux, c2, done) |
| 73 | go mux(cmux, c3, done) |
| 74 | go mux(cmux, c4, done) |
| 75 | go func() { |
| 76 | <-done |
| 77 | <-done |
| 78 | <-done |
| 79 | <-done |
| 80 | close(cmux) |
| 81 | }() |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 82 | // We keep the recver because it might catch more bugs in the future. |
| 83 | // However, the result of the bug linked to at the top is that we'll |
Russ Cox | 0a5fc261 | 2011-01-21 15:06:44 -0500 | [diff] [blame] | 84 | // end up panicking with: "throw: bad g->status in ready". |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 85 | recver(cmux) |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 86 | } |