/usr/share/gocode/src/golang.org/x/exp/old/netchan/netchan_test.go is in golang-golang-x-exp-dev 0.0~git20150826.1.eb7c1fa-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | // Copyright 2010 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.
package netchan
import (
"net"
"strings"
"testing"
"time"
)
const count = 10 // number of items in most tests
const closeCount = 5 // number of items when sender closes early
const base = 23
func exportSend(exp *Exporter, n int, t *testing.T, done chan bool) {
ch := make(chan int)
err := exp.Export("exportedSend", ch, Send)
if err != nil {
t.Fatal("exportSend:", err)
}
go func() {
for i := 0; i < n; i++ {
ch <- base + i
}
close(ch)
if done != nil {
done <- true
}
}()
}
func exportReceive(exp *Exporter, t *testing.T, expDone chan bool) {
ch := make(chan int)
err := exp.Export("exportedRecv", ch, Recv)
expDone <- true
if err != nil {
t.Fatal("exportReceive:", err)
}
for i := 0; i < count; i++ {
v, ok := <-ch
if !ok {
if i != closeCount {
t.Errorf("exportReceive expected close at %d; got one at %d", closeCount, i)
}
break
}
if v != base+i {
t.Errorf("export Receive: bad value: expected %d+%d=%d; got %d", base, i, base+i, v)
}
}
}
func importSend(imp *Importer, n int, t *testing.T, done chan bool) {
ch := make(chan int)
err := imp.ImportNValues("exportedRecv", ch, Send, 3, -1)
if err != nil {
t.Fatal("importSend:", err)
}
go func() {
for i := 0; i < n; i++ {
ch <- base + i
}
close(ch)
if done != nil {
done <- true
}
}()
}
func importReceive(imp *Importer, t *testing.T, done chan bool) {
ch := make(chan int)
err := imp.ImportNValues("exportedSend", ch, Recv, 3, count)
if err != nil {
t.Fatal("importReceive:", err)
}
for i := 0; i < count; i++ {
v, ok := <-ch
if !ok {
if i != closeCount {
t.Errorf("importReceive expected close at %d; got one at %d", closeCount, i)
}
break
}
if v != base+i {
t.Errorf("importReceive: bad value: expected %d+%d=%d; got %+d", base, i, base+i, v)
}
}
if done != nil {
done <- true
}
}
func TestExportSendImportReceive(t *testing.T) {
exp, imp := pair(t)
exportSend(exp, count, t, nil)
importReceive(imp, t, nil)
}
func TestExportReceiveImportSend(t *testing.T) {
exp, imp := pair(t)
expDone := make(chan bool)
done := make(chan bool)
go func() {
exportReceive(exp, t, expDone)
done <- true
}()
<-expDone
importSend(imp, count, t, nil)
<-done
}
func TestClosingExportSendImportReceive(t *testing.T) {
exp, imp := pair(t)
exportSend(exp, closeCount, t, nil)
importReceive(imp, t, nil)
}
func TestClosingImportSendExportReceive(t *testing.T) {
exp, imp := pair(t)
expDone := make(chan bool)
done := make(chan bool)
go func() {
exportReceive(exp, t, expDone)
done <- true
}()
<-expDone
importSend(imp, closeCount, t, nil)
<-done
}
func TestErrorForIllegalChannel(t *testing.T) {
exp, imp := pair(t)
// Now export a channel.
ch := make(chan int, 1)
err := exp.Export("aChannel", ch, Send)
if err != nil {
t.Fatal("export:", err)
}
ch <- 1234
close(ch)
// Now try to import a different channel.
ch = make(chan int)
err = imp.Import("notAChannel", ch, Recv, 1)
if err != nil {
t.Fatal("import:", err)
}
// Expect an error now. Start a timeout.
timeout := make(chan bool, 1) // buffered so closure will not hang around.
go func() {
time.Sleep(10 * time.Second) // very long, to give even really slow machines a chance.
timeout <- true
}()
select {
case err = <-imp.Errors():
if strings.Index(err.Error(), "no such channel") < 0 {
t.Error("wrong error for nonexistent channel:", err)
}
case <-timeout:
t.Error("import of nonexistent channel did not receive an error")
}
}
// Not a great test but it does at least invoke Drain.
func TestExportDrain(t *testing.T) {
exp, imp := pair(t)
done := make(chan bool)
go func() {
exportSend(exp, closeCount, t, nil)
done <- true
}()
<-done
go importReceive(imp, t, done)
exp.Drain(0)
<-done
}
// Not a great test but it does at least invoke Drain.
func TestImportDrain(t *testing.T) {
exp, imp := pair(t)
expDone := make(chan bool)
go exportReceive(exp, t, expDone)
<-expDone
importSend(imp, closeCount, t, nil)
imp.Drain(0)
}
// Not a great test but it does at least invoke Sync.
func TestExportSync(t *testing.T) {
exp, imp := pair(t)
done := make(chan bool)
exportSend(exp, closeCount, t, nil)
go importReceive(imp, t, done)
exp.Sync(0)
<-done
}
// Test hanging up the send side of an export.
// TODO: test hanging up the receive side of an export.
func TestExportHangup(t *testing.T) {
exp, imp := pair(t)
ech := make(chan int)
err := exp.Export("exportedSend", ech, Send)
if err != nil {
t.Fatal("export:", err)
}
// Prepare to receive two values. We'll actually deliver only one.
ich := make(chan int)
err = imp.ImportNValues("exportedSend", ich, Recv, 1, 2)
if err != nil {
t.Fatal("import exportedSend:", err)
}
// Send one value, receive it.
const Value = 1234
ech <- Value
v := <-ich
if v != Value {
t.Fatal("expected", Value, "got", v)
}
// Now hang up the channel. Importer should see it close.
exp.Hangup("exportedSend")
v, ok := <-ich
if ok {
t.Fatal("expected channel to be closed; got value", v)
}
}
// Test hanging up the send side of an import.
// TODO: test hanging up the receive side of an import.
func TestImportHangup(t *testing.T) {
exp, imp := pair(t)
ech := make(chan int)
err := exp.Export("exportedRecv", ech, Recv)
if err != nil {
t.Fatal("export:", err)
}
// Prepare to Send two values. We'll actually deliver only one.
ich := make(chan int)
err = imp.ImportNValues("exportedRecv", ich, Send, 1, 2)
if err != nil {
t.Fatal("import exportedRecv:", err)
}
// Send one value, receive it.
const Value = 1234
ich <- Value
v := <-ech
if v != Value {
t.Fatal("expected", Value, "got", v)
}
// Now hang up the channel. Exporter should see it close.
imp.Hangup("exportedRecv")
v, ok := <-ech
if ok {
t.Fatal("expected channel to be closed; got value", v)
}
}
// loop back exportedRecv to exportedSend,
// but receive a value from ctlch before starting the loop.
func exportLoopback(exp *Exporter, t *testing.T) {
inch := make(chan int)
if err := exp.Export("exportedRecv", inch, Recv); err != nil {
t.Fatal("exportRecv")
}
outch := make(chan int)
if err := exp.Export("exportedSend", outch, Send); err != nil {
t.Fatal("exportSend")
}
ctlch := make(chan int)
if err := exp.Export("exportedCtl", ctlch, Recv); err != nil {
t.Fatal("exportRecv")
}
go func() {
<-ctlch
for i := 0; i < count; i++ {
x := <-inch
if x != base+i {
t.Errorf("exportLoopback expected %d; got %d", i, x)
}
outch <- x
}
}()
}
// This test checks that channel operations can proceed
// even when other concurrent operations are blocked.
func TestIndependentSends(t *testing.T) {
if testing.Short() {
t.Logf("disabled test during -short")
return
}
exp, imp := pair(t)
exportLoopback(exp, t)
importSend(imp, count, t, nil)
done := make(chan bool)
go importReceive(imp, t, done)
// wait for export side to try to deliver some values.
time.Sleep(250 * time.Millisecond)
ctlch := make(chan int)
if err := imp.ImportNValues("exportedCtl", ctlch, Send, 1, 1); err != nil {
t.Fatal("importSend:", err)
}
ctlch <- 0
<-done
}
// This test cross-connects a pair of exporter/importer pairs.
type value struct {
I int
Source string
}
func TestCrossConnect(t *testing.T) {
e1, i1 := pair(t)
e2, i2 := pair(t)
crossExport(e1, e2, t)
crossImport(i1, i2, t)
}
// Export side of cross-traffic.
func crossExport(e1, e2 *Exporter, t *testing.T) {
s := make(chan value)
err := e1.Export("exportedSend", s, Send)
if err != nil {
t.Fatal("exportSend:", err)
}
r := make(chan value)
err = e2.Export("exportedReceive", r, Recv)
if err != nil {
t.Fatal("exportReceive:", err)
}
go crossLoop("export", s, r, t)
}
// Import side of cross-traffic.
func crossImport(i1, i2 *Importer, t *testing.T) {
s := make(chan value)
err := i2.Import("exportedReceive", s, Send, 2)
if err != nil {
t.Fatal("import of exportedReceive:", err)
}
r := make(chan value)
err = i1.Import("exportedSend", r, Recv, 2)
if err != nil {
t.Fatal("import of exported Send:", err)
}
crossLoop("import", s, r, t)
}
// Cross-traffic: send and receive 'count' numbers.
func crossLoop(name string, s, r chan value, t *testing.T) {
for si, ri := 0, 0; si < count && ri < count; {
select {
case s <- value{si, name}:
si++
case v := <-r:
if v.I != ri {
t.Errorf("loop: bad value: expected %d, hello; got %+v", ri, v)
}
ri++
}
}
}
const flowCount = 100
// test flow control from exporter to importer.
func TestExportFlowControl(t *testing.T) {
if testing.Short() {
t.Logf("disabled test during -short")
return
}
exp, imp := pair(t)
sendDone := make(chan bool, 1)
exportSend(exp, flowCount, t, sendDone)
ch := make(chan int)
err := imp.ImportNValues("exportedSend", ch, Recv, 20, -1)
if err != nil {
t.Fatal("importReceive:", err)
}
testFlow(sendDone, ch, flowCount, t)
}
// test flow control from importer to exporter.
func TestImportFlowControl(t *testing.T) {
if testing.Short() {
t.Logf("disabled test during -short")
return
}
exp, imp := pair(t)
ch := make(chan int)
err := exp.Export("exportedRecv", ch, Recv)
if err != nil {
t.Fatal("importReceive:", err)
}
sendDone := make(chan bool, 1)
importSend(imp, flowCount, t, sendDone)
testFlow(sendDone, ch, flowCount, t)
}
func testFlow(sendDone chan bool, ch <-chan int, N int, t *testing.T) {
go func() {
time.Sleep(500 * time.Millisecond)
sendDone <- false
}()
if <-sendDone {
t.Fatal("send did not block")
}
n := 0
for i := range ch {
t.Log("after blocking, got value ", i)
n++
}
if n != N {
t.Fatalf("expected %d values; got %d", N, n)
}
}
func pair(t *testing.T) (*Exporter, *Importer) {
c0, c1 := net.Pipe()
exp := NewExporter()
go exp.ServeConn(c0)
imp := NewImporter(c1)
return exp, imp
}
|