...
Run Format

Source file test/chan/nonblock.go

Documentation: test/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  // Test channel operations that test for blocking.
     8  // Use several sizes and types of operands.
     9  
    10  package main
    11  
    12  import "runtime"
    13  import "time"
    14  
    15  func i32receiver(c chan int32, strobe chan bool) {
    16  	if <-c != 123 {
    17  		panic("i32 value")
    18  	}
    19  	strobe <- true
    20  }
    21  
    22  func i32sender(c chan int32, strobe chan bool) {
    23  	c <- 234
    24  	strobe <- true
    25  }
    26  
    27  func i64receiver(c chan int64, strobe chan bool) {
    28  	if <-c != 123456 {
    29  		panic("i64 value")
    30  	}
    31  	strobe <- true
    32  }
    33  
    34  func i64sender(c chan int64, strobe chan bool) {
    35  	c <- 234567
    36  	strobe <- true
    37  }
    38  
    39  func breceiver(c chan bool, strobe chan bool) {
    40  	if !<-c {
    41  		panic("b value")
    42  	}
    43  	strobe <- true
    44  }
    45  
    46  func bsender(c chan bool, strobe chan bool) {
    47  	c <- true
    48  	strobe <- true
    49  }
    50  
    51  func sreceiver(c chan string, strobe chan bool) {
    52  	if <-c != "hello" {
    53  		panic("s value")
    54  	}
    55  	strobe <- true
    56  }
    57  
    58  func ssender(c chan string, strobe chan bool) {
    59  	c <- "hello again"
    60  	strobe <- true
    61  }
    62  
    63  var ticker = time.Tick(10 * 1000) // 10 us
    64  func sleep() {
    65  	<-ticker
    66  	<-ticker
    67  	runtime.Gosched()
    68  	runtime.Gosched()
    69  	runtime.Gosched()
    70  }
    71  
    72  const maxTries = 10000 // Up to 100ms per test.
    73  
    74  func main() {
    75  	var i32 int32
    76  	var i64 int64
    77  	var b bool
    78  	var s string
    79  
    80  	var sync = make(chan bool)
    81  
    82  	for buffer := 0; buffer < 2; buffer++ {
    83  		c32 := make(chan int32, buffer)
    84  		c64 := make(chan int64, buffer)
    85  		cb := make(chan bool, buffer)
    86  		cs := make(chan string, buffer)
    87  
    88  		select {
    89  		case i32 = <-c32:
    90  			panic("blocked i32sender")
    91  		default:
    92  		}
    93  
    94  		select {
    95  		case i64 = <-c64:
    96  			panic("blocked i64sender")
    97  		default:
    98  		}
    99  
   100  		select {
   101  		case b = <-cb:
   102  			panic("blocked bsender")
   103  		default:
   104  		}
   105  
   106  		select {
   107  		case s = <-cs:
   108  			panic("blocked ssender")
   109  		default:
   110  		}
   111  
   112  		go i32receiver(c32, sync)
   113  		try := 0
   114  	Send32:
   115  		for {
   116  			select {
   117  			case c32 <- 123:
   118  				break Send32
   119  			default:
   120  				try++
   121  				if try > maxTries {
   122  					println("i32receiver buffer=", buffer)
   123  					panic("fail")
   124  				}
   125  				sleep()
   126  			}
   127  		}
   128  		<-sync
   129  
   130  		go i32sender(c32, sync)
   131  		if buffer > 0 {
   132  			<-sync
   133  		}
   134  		try = 0
   135  	Recv32:
   136  		for {
   137  			select {
   138  			case i32 = <-c32:
   139  				break Recv32
   140  			default:
   141  				try++
   142  				if try > maxTries {
   143  					println("i32sender buffer=", buffer)
   144  					panic("fail")
   145  				}
   146  				sleep()
   147  			}
   148  		}
   149  		if i32 != 234 {
   150  			panic("i32sender value")
   151  		}
   152  		if buffer == 0 {
   153  			<-sync
   154  		}
   155  
   156  		go i64receiver(c64, sync)
   157  		try = 0
   158  	Send64:
   159  		for {
   160  			select {
   161  			case c64 <- 123456:
   162  				break Send64
   163  			default:
   164  				try++
   165  				if try > maxTries {
   166  					panic("i64receiver")
   167  				}
   168  				sleep()
   169  			}
   170  		}
   171  		<-sync
   172  
   173  		go i64sender(c64, sync)
   174  		if buffer > 0 {
   175  			<-sync
   176  		}
   177  		try = 0
   178  	Recv64:
   179  		for {
   180  			select {
   181  			case i64 = <-c64:
   182  				break Recv64
   183  			default:
   184  				try++
   185  				if try > maxTries {
   186  					panic("i64sender")
   187  				}
   188  				sleep()
   189  			}
   190  		}
   191  		if i64 != 234567 {
   192  			panic("i64sender value")
   193  		}
   194  		if buffer == 0 {
   195  			<-sync
   196  		}
   197  
   198  		go breceiver(cb, sync)
   199  		try = 0
   200  	SendBool:
   201  		for {
   202  			select {
   203  			case cb <- true:
   204  				break SendBool
   205  			default:
   206  				try++
   207  				if try > maxTries {
   208  					panic("breceiver")
   209  				}
   210  				sleep()
   211  			}
   212  		}
   213  		<-sync
   214  
   215  		go bsender(cb, sync)
   216  		if buffer > 0 {
   217  			<-sync
   218  		}
   219  		try = 0
   220  	RecvBool:
   221  		for {
   222  			select {
   223  			case b = <-cb:
   224  				break RecvBool
   225  			default:
   226  				try++
   227  				if try > maxTries {
   228  					panic("bsender")
   229  				}
   230  				sleep()
   231  			}
   232  		}
   233  		if !b {
   234  			panic("bsender value")
   235  		}
   236  		if buffer == 0 {
   237  			<-sync
   238  		}
   239  
   240  		go sreceiver(cs, sync)
   241  		try = 0
   242  	SendString:
   243  		for {
   244  			select {
   245  			case cs <- "hello":
   246  				break SendString
   247  			default:
   248  				try++
   249  				if try > maxTries {
   250  					panic("sreceiver")
   251  				}
   252  				sleep()
   253  			}
   254  		}
   255  		<-sync
   256  
   257  		go ssender(cs, sync)
   258  		if buffer > 0 {
   259  			<-sync
   260  		}
   261  		try = 0
   262  	RecvString:
   263  		for {
   264  			select {
   265  			case s = <-cs:
   266  				break RecvString
   267  			default:
   268  				try++
   269  				if try > maxTries {
   270  					panic("ssender")
   271  				}
   272  				sleep()
   273  			}
   274  		}
   275  		if s != "hello again" {
   276  			panic("ssender value")
   277  		}
   278  		if buffer == 0 {
   279  			<-sync
   280  		}
   281  	}
   282  }
   283  

View as plain text