OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package race_test | 5 package race_test |
6 | 6 |
7 import ( | 7 import ( |
8 "runtime" | 8 "runtime" |
9 "testing" | 9 "testing" |
10 "time" | 10 "time" |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 } | 340 } |
341 }() | 341 }() |
342 go func() { | 342 go func() { |
343 close(c) | 343 close(c) |
344 compl <- true | 344 compl <- true |
345 }() | 345 }() |
346 <-compl | 346 <-compl |
347 <-compl | 347 <-compl |
348 } | 348 } |
349 | 349 |
| 350 func TestRaceSelectReadWriteAsync(t *testing.T) { |
| 351 done := make(chan bool) |
| 352 x := 0 |
| 353 c1 := make(chan int, 10) |
| 354 c2 := make(chan int, 10) |
| 355 c3 := make(chan int) |
| 356 c2 <- 1 |
| 357 go func() { |
| 358 select { |
| 359 case c1 <- x: // read of x races with... |
| 360 case c3 <- 1: |
| 361 } |
| 362 done <- true |
| 363 }() |
| 364 select { |
| 365 case x = <-c2: // ... write to x here |
| 366 case c3 <- 1: |
| 367 } |
| 368 <-done |
| 369 } |
| 370 |
| 371 func TestRaceSelectReadWriteSync(t *testing.T) { |
| 372 done := make(chan bool) |
| 373 x := 0 |
| 374 c1 := make(chan int) |
| 375 c2 := make(chan int) |
| 376 c3 := make(chan int) |
| 377 // make c1 and c2 ready for communication |
| 378 go func() { |
| 379 <-c1 |
| 380 }() |
| 381 go func() { |
| 382 c2 <- 1 |
| 383 }() |
| 384 go func() { |
| 385 select { |
| 386 case c1 <- x: // read of x races with... |
| 387 case c3 <- 1: |
| 388 } |
| 389 done <- true |
| 390 }() |
| 391 select { |
| 392 case x = <-c2: // ... write to x here |
| 393 case c3 <- 1: |
| 394 } |
| 395 <-done |
| 396 } |
| 397 |
| 398 func TestNoRaceSelectReadWriteAsync(t *testing.T) { |
| 399 done := make(chan bool) |
| 400 x := 0 |
| 401 c1 := make(chan int) |
| 402 c2 := make(chan int) |
| 403 go func() { |
| 404 select { |
| 405 case c1 <- x: // read of x does not race with... |
| 406 case c2 <- 1: |
| 407 } |
| 408 done <- true |
| 409 }() |
| 410 select { |
| 411 case x = <-c1: // ... write to x here |
| 412 case c2 <- 1: |
| 413 } |
| 414 <-done |
| 415 } |
| 416 |
| 417 func TestRaceChanReadWriteAsync(t *testing.T) { |
| 418 done := make(chan bool) |
| 419 c1 := make(chan int, 10) |
| 420 c2 := make(chan int, 10) |
| 421 c2 <- 10 |
| 422 x := 0 |
| 423 go func() { |
| 424 c1 <- x // read of x races with... |
| 425 done <- true |
| 426 }() |
| 427 x = <-c2 // ... write to x here |
| 428 <-done |
| 429 } |
| 430 |
| 431 func TestRaceChanReadWriteSync(t *testing.T) { |
| 432 done := make(chan bool) |
| 433 c1 := make(chan int) |
| 434 c2 := make(chan int) |
| 435 // make c1 and c2 ready for communication |
| 436 go func() { |
| 437 <-c1 |
| 438 }() |
| 439 go func() { |
| 440 c2 <- 10 |
| 441 }() |
| 442 x := 0 |
| 443 go func() { |
| 444 c1 <- x // read of x races with... |
| 445 done <- true |
| 446 }() |
| 447 x = <-c2 // ... write to x here |
| 448 <-done |
| 449 } |
| 450 |
| 451 func TestNoRaceChanReadWriteAsync(t *testing.T) { |
| 452 done := make(chan bool) |
| 453 c1 := make(chan int, 10) |
| 454 x := 0 |
| 455 go func() { |
| 456 c1 <- x // read of x does not race with... |
| 457 done <- true |
| 458 }() |
| 459 x = <-c1 // ... write to x here |
| 460 <-done |
| 461 } |
| 462 |
350 func TestNoRaceProducerConsumerUnbuffered(t *testing.T) { | 463 func TestNoRaceProducerConsumerUnbuffered(t *testing.T) { |
351 type Task struct { | 464 type Task struct { |
352 f func() | 465 f func() |
353 done chan bool | 466 done chan bool |
354 } | 467 } |
355 | 468 |
356 queue := make(chan Task) | 469 queue := make(chan Task) |
357 | 470 |
358 go func() { | 471 go func() { |
359 t := <-queue | 472 t := <-queue |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 func TestRaceChanCloseSend(t *testing.T) { | 584 func TestRaceChanCloseSend(t *testing.T) { |
472 compl := make(chan bool, 1) | 585 compl := make(chan bool, 1) |
473 c := make(chan int, 10) | 586 c := make(chan int, 10) |
474 go func() { | 587 go func() { |
475 close(c) | 588 close(c) |
476 compl <- true | 589 compl <- true |
477 }() | 590 }() |
478 c <- 0 | 591 c <- 0 |
479 <-compl | 592 <-compl |
480 } | 593 } |
OLD | NEW |