blob: 2b61ec9dbb4f90a4aaccab1d0309061608274985 [file] [log] [blame]
Rob Pikef6c13bb2008-07-16 14:41:27 -07001// $G $D/$F.go && $L $F.$A && ./$A.out
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// Verify channel operations that test for blocking
8// Use several sizes and types of operands
9
10package main
11
Russ Coxd2117ad2009-01-23 17:04:56 -080012import "time"
Rob Pikef6c13bb2008-07-16 14:41:27 -070013
Russ Coxd2117ad2009-01-23 17:04:56 -080014func i32receiver(c chan int32, strobe chan bool) {
Rob Pikebc2f5f12008-08-11 22:07:49 -070015 if <-c != 123 { panic("i32 value") }
Russ Coxd2117ad2009-01-23 17:04:56 -080016 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070017}
18
Russ Coxd2117ad2009-01-23 17:04:56 -080019func i32sender(c chan int32, strobe chan bool) {
20 c <- 234;
21 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070022}
23
Russ Coxd2117ad2009-01-23 17:04:56 -080024func i64receiver(c chan int64, strobe chan bool) {
Rob Pikebc2f5f12008-08-11 22:07:49 -070025 if <-c != 123456 { panic("i64 value") }
Russ Coxd2117ad2009-01-23 17:04:56 -080026 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070027}
28
Russ Coxd2117ad2009-01-23 17:04:56 -080029func i64sender(c chan int64, strobe chan bool) {
30 c <- 234567;
31 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070032}
33
Russ Coxd2117ad2009-01-23 17:04:56 -080034func breceiver(c chan bool, strobe chan bool) {
Rob Pikebc2f5f12008-08-11 22:07:49 -070035 if ! <-c { panic("b value") }
Russ Coxd2117ad2009-01-23 17:04:56 -080036 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070037}
38
Russ Coxd2117ad2009-01-23 17:04:56 -080039func bsender(c chan bool, strobe chan bool) {
40 c <- true;
41 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070042}
43
Russ Coxd2117ad2009-01-23 17:04:56 -080044func sreceiver(c chan string, strobe chan bool) {
Rob Pikebc2f5f12008-08-11 22:07:49 -070045 if <-c != "hello" { panic("s value") }
Russ Coxd2117ad2009-01-23 17:04:56 -080046 strobe <- true
Rob Pikef6c13bb2008-07-16 14:41:27 -070047}
48
Russ Coxd2117ad2009-01-23 17:04:56 -080049func ssender(c chan string, strobe chan bool) {
50 c <- "hello again";
51 strobe <- true
52}
53
54var ticker = time.Tick(10*1000); // 10 us
55func sleep() {
56 <-ticker;
57 <-ticker;
58 sys.Gosched();
59 sys.Gosched();
60 sys.Gosched();
Rob Pikef6c13bb2008-07-16 14:41:27 -070061}
62
63func main() {
64 var i32 int32;
65 var i64 int64;
66 var b bool;
67 var s string;
68 var ok bool;
69
Russ Coxd2117ad2009-01-23 17:04:56 -080070 var sync = make(chan bool);
71
Russ Coxd448d182008-09-26 11:47:04 -070072 for buffer := 0; buffer < 2; buffer++ {
Russ Cox55645042009-01-06 15:19:02 -080073 c32 := make(chan int32, buffer);
74 c64 := make(chan int64, buffer);
75 cb := make(chan bool, buffer);
76 cs := make(chan string, buffer);
Russ Cox08ca30b2008-12-19 03:05:37 -080077
Russ Coxd448d182008-09-26 11:47:04 -070078 i32, ok = <-c32;
79 if ok { panic("blocked i32sender") }
Russ Cox08ca30b2008-12-19 03:05:37 -080080
Russ Coxd448d182008-09-26 11:47:04 -070081 i64, ok = <-c64;
82 if ok { panic("blocked i64sender") }
Russ Cox08ca30b2008-12-19 03:05:37 -080083
Russ Coxd448d182008-09-26 11:47:04 -070084 b, ok = <-cb;
85 if ok { panic("blocked bsender") }
Russ Cox08ca30b2008-12-19 03:05:37 -080086
Russ Coxd448d182008-09-26 11:47:04 -070087 s, ok = <-cs;
88 if ok { panic("blocked ssender") }
Russ Cox08ca30b2008-12-19 03:05:37 -080089
Russ Coxd2117ad2009-01-23 17:04:56 -080090 go i32receiver(c32, sync);
91 sleep();
Russ Coxd448d182008-09-26 11:47:04 -070092 ok = c32 <- 123;
Russ Coxd2117ad2009-01-23 17:04:56 -080093 if !ok { panic("i32receiver buffer=", buffer) }
94 <-sync;
Russ Cox08ca30b2008-12-19 03:05:37 -080095
Russ Coxd2117ad2009-01-23 17:04:56 -080096 go i32sender(c32, sync);
97 if buffer > 0 { <-sync } else { sleep() }
98 i32, ok = <-c32;
99 if !ok { panic("i32sender buffer=", buffer) }
100 if i32 != 234 { panic("i32sender value") }
101 if buffer == 0 { <-sync }
102
103 go i64receiver(c64, sync);
104 sleep();
Russ Coxd448d182008-09-26 11:47:04 -0700105 ok = c64 <- 123456;
106 if !ok { panic("i64receiver") }
Russ Coxd2117ad2009-01-23 17:04:56 -0800107 <-sync;
108
109 go i64sender(c64, sync);
110 if buffer > 0 { <-sync } else { sleep() }
Russ Coxd448d182008-09-26 11:47:04 -0700111 i64, ok = <-c64;
112 if !ok { panic("i64sender") }
113 if i64 != 234567 { panic("i64sender value") }
Russ Coxd2117ad2009-01-23 17:04:56 -0800114 if buffer == 0 { <-sync }
Russ Cox08ca30b2008-12-19 03:05:37 -0800115
Russ Coxd2117ad2009-01-23 17:04:56 -0800116 go breceiver(cb, sync);
117 sleep();
Russ Coxd448d182008-09-26 11:47:04 -0700118 ok = cb <- true;
119 if !ok { panic("breceiver") }
Russ Coxd2117ad2009-01-23 17:04:56 -0800120 <-sync;
121
122 go bsender(cb, sync);
123 if buffer > 0 { <-sync } else { sleep() }
Russ Coxd448d182008-09-26 11:47:04 -0700124 b, ok = <-cb;
125 if !ok { panic("bsender") }
126 if !b{ panic("bsender value") }
Russ Coxd2117ad2009-01-23 17:04:56 -0800127 if buffer == 0 { <-sync }
Russ Cox08ca30b2008-12-19 03:05:37 -0800128
Russ Coxd2117ad2009-01-23 17:04:56 -0800129 go sreceiver(cs, sync);
130 sleep();
Russ Coxd448d182008-09-26 11:47:04 -0700131 ok = cs <- "hello";
132 if !ok { panic("sreceiver") }
Russ Coxd2117ad2009-01-23 17:04:56 -0800133 <-sync;
134
135 go ssender(cs, sync);
136 if buffer > 0 { <-sync } else { sleep() }
Russ Coxd448d182008-09-26 11:47:04 -0700137 s, ok = <-cs;
138 if !ok { panic("ssender") }
139 if s != "hello again" { panic("ssender value") }
Russ Coxd2117ad2009-01-23 17:04:56 -0800140 if buffer == 0 { <-sync }
Russ Coxd448d182008-09-26 11:47:04 -0700141 }
Rob Pikebc2f5f12008-08-11 22:07:49 -0700142 print("PASS\n")
Rob Pikef6c13bb2008-07-16 14:41:27 -0700143}