1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func TestNoRaceChanSync(t *testing.T) {
25 func TestNoRaceChanSyncRev(t *testing.T) {
37 func TestNoRaceChanAsync(t *testing.T) {
40 c := make(chan int, 10)
49 func TestRaceChanAsyncRev(t *testing.T) {
52 c := make(chan int, 10)
61 func TestNoRaceChanAsyncCloseRecv(t *testing.T) {
64 c := make(chan int, 10)
78 func TestNoRaceChanAsyncCloseRecv2(t *testing.T) {
81 c := make(chan int, 10)
90 func TestNoRaceChanAsyncCloseRecv3(t *testing.T) {
93 c := make(chan int, 10)
103 func TestNoRaceChanSyncCloseRecv(t *testing.T) {
120 func TestNoRaceChanSyncCloseRecv2(t *testing.T) {
132 func TestNoRaceChanSyncCloseRecv3(t *testing.T) {
145 func TestRaceChanSyncCloseSend(t *testing.T) {
162 func TestRaceChanAsyncCloseSend(t *testing.T) {
165 c := make(chan int, 10)
181 func TestRaceChanCloseClose(t *testing.T) {
182 compl := make(chan bool, 2)
189 if recover() != nil {
199 if recover() != nil {
211 func TestRaceChanSendLen(t *testing.T) {
214 c := make(chan int, 10)
225 func TestRaceChanRecvLen(t *testing.T) {
228 c := make(chan int, 10)
240 func TestRaceChanSendSend(t *testing.T) {
241 compl := make(chan bool, 2)
245 c := make(chan int, 1)
268 func TestNoRaceChanPtr(t *testing.T) {
280 func TestRaceChanWrongSend(t *testing.T) {
284 c := make(chan int, 2)
301 func TestRaceChanWrongClose(t *testing.T) {
305 c := make(chan int, 1)
306 done := make(chan bool)
322 if _, who := <-c; who {
331 func TestRaceChanSendClose(t *testing.T) {
332 compl := make(chan bool, 2)
333 c := make(chan int, 1)
342 time.Sleep(10 * time.Millisecond)
350 func TestRaceChanSendSelectClose(t *testing.T) {
351 compl := make(chan bool, 2)
352 c := make(chan int, 1)
359 time.Sleep(10 * time.Millisecond)
373 func TestRaceSelectReadWriteAsync(t *testing.T) {
374 done := make(chan bool)
376 c1 := make(chan int, 10)
377 c2 := make(chan int, 10)
382 case c1 <- x: // read of x races with...
388 case x = <-c2: // ... write to x here
394 func TestRaceSelectReadWriteSync(t *testing.T) {
395 done := make(chan bool)
400 // make c1 and c2 ready for communication
409 case c1 <- x: // read of x races with...
415 case x = <-c2: // ... write to x here
421 func TestNoRaceSelectReadWriteAsync(t *testing.T) {
422 done := make(chan bool)
428 case c1 <- x: // read of x does not race with...
434 case x = <-c1: // ... write to x here
440 func TestRaceChanReadWriteAsync(t *testing.T) {
441 done := make(chan bool)
442 c1 := make(chan int, 10)
443 c2 := make(chan int, 10)
447 c1 <- x // read of x races with...
450 x = <-c2 // ... write to x here
454 func TestRaceChanReadWriteSync(t *testing.T) {
455 done := make(chan bool)
458 // make c1 and c2 ready for communication
467 c1 <- x // read of x races with...
470 x = <-c2 // ... write to x here
474 func TestNoRaceChanReadWriteAsync(t *testing.T) {
475 done := make(chan bool)
476 c1 := make(chan int, 10)
479 c1 <- x // read of x does not race with...
482 x = <-c1 // ... write to x here
486 func TestNoRaceProducerConsumerUnbuffered(t *testing.T) {
492 queue := make(chan Task)
500 doit := func(f func()) {
501 done := make(chan bool, 1)
502 queue <- Task{f, done}
513 func TestRaceChanItselfSend(t *testing.T) {
514 compl := make(chan bool, 1)
515 c := make(chan int, 10)
520 c = make(chan int, 20)
524 func TestRaceChanItselfRecv(t *testing.T) {
525 compl := make(chan bool, 1)
526 c := make(chan int, 10)
533 c = make(chan int, 20)
537 func TestRaceChanItselfNil(t *testing.T) {
538 c := make(chan int, 10)
547 func TestRaceChanItselfClose(t *testing.T) {
548 compl := make(chan bool, 1)
558 func TestRaceChanItselfLen(t *testing.T) {
559 compl := make(chan bool, 1)
569 func TestRaceChanItselfCap(t *testing.T) {
570 compl := make(chan bool, 1)
580 func TestNoRaceChanCloseLen(t *testing.T) {
581 c := make(chan int, 10)
582 r := make(chan int, 10)
594 func TestNoRaceChanCloseCap(t *testing.T) {
595 c := make(chan int, 10)
596 r := make(chan int, 10)
608 func TestRaceChanCloseSend(t *testing.T) {
609 compl := make(chan bool, 1)
610 c := make(chan int, 10)
619 func TestNoRaceChanMutex(t *testing.T) {
620 done := make(chan struct{})
621 mtx := make(chan struct{}, 1)
636 func TestNoRaceSelectMutex(t *testing.T) {
637 done := make(chan struct{})
638 mtx := make(chan struct{}, 1)
639 aux := make(chan bool)
644 case mtx <- struct{}{}:
655 case mtx <- struct{}{}:
666 func TestRaceChanSem(t *testing.T) {
667 done := make(chan struct{})
668 mtx := make(chan bool, 2)
683 func TestNoRaceChanWaitGroup(t *testing.T) {
685 chanWg := make(chan bool, N/2)
686 data := make([]int, N)
687 for i := 0; i < N; i++ {
694 for i := 0; i < cap(chanWg); i++ {
697 for i := 0; i < N; i++ {
702 // Test that sender synchronizes with receiver even if the sender was blocked.
703 func TestNoRaceBlockedSendSync(t *testing.T) {
704 c := make(chan *int, 1)
710 // Give the sender time to actually block.
711 // This sleep is completely optional: race report must not be printed
712 // regardless of whether the sender actually blocks or not.
713 // It cannot lead to flakiness.
714 time.Sleep(10 * time.Millisecond)
722 // The same as TestNoRaceBlockedSendSync above, but sender unblock happens in a select.
723 func TestNoRaceBlockedSelectSendSync(t *testing.T) {
724 c := make(chan *int, 1)
730 time.Sleep(10 * time.Millisecond)
737 case <-make(chan int):
741 // Test that close synchronizes with a read from the empty closed channel.
742 // See https://golang.org/issue/36714.
743 func TestNoRaceCloseHappensBeforeRead(t *testing.T) {
744 for i := 0; i < 100; i++ {
746 var write = make(chan struct{})
747 var read = make(chan struct{})
767 // Test that we call the proper race detector function when c.elemsize==0.
768 // See https://github.com/golang/go/issues/42598
769 func TestNoRaceElemSize0(t *testing.T) {
771 var c = make(chan struct{}, 2)
782 time.Sleep(10 * time.Millisecond)