summaryrefslogtreecommitdiff
path: root/gcc/testsuite/go.test/test/chan/nonblock.go
blob: 52f04bfb12f53c4d87c1c653616bfcd1f5a094f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// $G $D/$F.go && $L $F.$A && ./$A.out

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Verify channel operations that test for blocking
// Use several sizes and types of operands

package main

import "runtime"
import "time"

func i32receiver(c chan int32, strobe chan bool) {
	if <-c != 123 {
		panic("i32 value")
	}
	strobe <- true
}

func i32sender(c chan int32, strobe chan bool) {
	c <- 234
	strobe <- true
}

func i64receiver(c chan int64, strobe chan bool) {
	if <-c != 123456 {
		panic("i64 value")
	}
	strobe <- true
}

func i64sender(c chan int64, strobe chan bool) {
	c <- 234567
	strobe <- true
}

func breceiver(c chan bool, strobe chan bool) {
	if !<-c {
		panic("b value")
	}
	strobe <- true
}

func bsender(c chan bool, strobe chan bool) {
	c <- true
	strobe <- true
}

func sreceiver(c chan string, strobe chan bool) {
	if <-c != "hello" {
		panic("s value")
	}
	strobe <- true
}

func ssender(c chan string, strobe chan bool) {
	c <- "hello again"
	strobe <- true
}

var ticker = time.Tick(10 * 1000) // 10 us
func sleep() {
	<-ticker
	<-ticker
	runtime.Gosched()
	runtime.Gosched()
	runtime.Gosched()
}

const maxTries = 10000 // Up to 100ms per test.

func main() {
	var i32 int32
	var i64 int64
	var b bool
	var s string
	var ok bool

	var sync = make(chan bool)

	for buffer := 0; buffer < 2; buffer++ {
		c32 := make(chan int32, buffer)
		c64 := make(chan int64, buffer)
		cb := make(chan bool, buffer)
		cs := make(chan string, buffer)

		i32, ok = <-c32
		if ok {
			panic("blocked i32sender")
		}

		i64, ok = <-c64
		if ok {
			panic("blocked i64sender")
		}

		b, ok = <-cb
		if ok {
			panic("blocked bsender")
		}

		s, ok = <-cs
		if ok {
			panic("blocked ssender")
		}

		go i32receiver(c32, sync)
		try := 0
		for !(c32 <- 123) {
			try++
			if try > maxTries {
				println("i32receiver buffer=", buffer)
				panic("fail")
			}
			sleep()
		}
		<-sync

		go i32sender(c32, sync)
		if buffer > 0 {
			<-sync
		}
		try = 0
		for i32, ok = <-c32; !ok; i32, ok = <-c32 {
			try++
			if try > maxTries {
				println("i32sender buffer=", buffer)
				panic("fail")
			}
			sleep()
		}
		if i32 != 234 {
			panic("i32sender value")
		}
		if buffer == 0 {
			<-sync
		}

		go i64receiver(c64, sync)
		try = 0
		for !(c64 <- 123456) {
			try++
			if try > maxTries {
				panic("i64receiver")
			}
			sleep()
		}
		<-sync

		go i64sender(c64, sync)
		if buffer > 0 {
			<-sync
		}
		try = 0
		for i64, ok = <-c64; !ok; i64, ok = <-c64 {
			try++
			if try > maxTries {
				panic("i64sender")
			}
			sleep()
		}
		if i64 != 234567 {
			panic("i64sender value")
		}
		if buffer == 0 {
			<-sync
		}

		go breceiver(cb, sync)
		try = 0
		for !(cb <- true) {
			try++
			if try > maxTries {
				panic("breceiver")
			}
			sleep()
		}
		<-sync

		go bsender(cb, sync)
		if buffer > 0 {
			<-sync
		}
		try = 0
		for b, ok = <-cb; !ok; b, ok = <-cb {
			try++
			if try > maxTries {
				panic("bsender")
			}
			sleep()
		}
		if !b {
			panic("bsender value")
		}
		if buffer == 0 {
			<-sync
		}

		go sreceiver(cs, sync)
		try = 0
		for !(cs <- "hello") {
			try++
			if try > maxTries {
				panic("sreceiver")
			}
			sleep()
		}
		<-sync

		go ssender(cs, sync)
		if buffer > 0 {
			<-sync
		}
		try = 0
		for s, ok = <-cs; !ok; s, ok = <-cs {
			try++
			if try > maxTries {
				panic("ssender")
			}
			sleep()
		}
		if s != "hello again" {
			panic("ssender value")
		}
		if buffer == 0 {
			<-sync
		}
	}
	print("PASS\n")
}