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 testWaitGroup(t
*testing
.T
, wg1
*WaitGroup
, wg2
*WaitGroup
) {
17 exited
:= make(chan bool, n
)
18 for i
:= 0; i
!= n
; i
++ {
26 for i
:= 0; i
!= n
; i
++ {
29 t
.Fatal("WaitGroup released group too soon")
34 for i
:= 0; i
!= n
; i
++ {
35 <-exited
// Will block if barrier fails to unlock someone.
39 func TestWaitGroup(t
*testing
.T
) {
43 // Run the same test a few times to ensure barrier is in a proper state.
44 for i
:= 0; i
!= 8; i
++ {
45 testWaitGroup(t
, wg1
, wg2
)
49 func TestWaitGroupMisuse(t
*testing
.T
) {
52 if err
!= "sync: negative WaitGroup counter" {
53 t
.Fatalf("Unexpected panic: %#v", err
)
60 t
.Fatal("Should panic")
63 func TestWaitGroupRace(t
*testing
.T
) {
64 // Run this test for about 1ms.
65 for i
:= 0; i
< 1000; i
++ {
80 // Wait for goroutine 1 and 2
82 if atomic
.LoadInt32(n
) != 2 {
83 t
.Fatal("Spurious wakeup from Wait")
88 func BenchmarkWaitGroupUncontended(b
*testing
.B
) {
89 type PaddedWaitGroup
struct {
93 b
.RunParallel(func(pb
*testing
.PB
) {
94 var wg PaddedWaitGroup
103 func benchmarkWaitGroupAddDone(b
*testing
.B
, localWork
int) {
105 b
.RunParallel(func(pb
*testing
.PB
) {
109 for i
:= 0; i
< localWork
; i
++ {
119 func BenchmarkWaitGroupAddDone(b
*testing
.B
) {
120 benchmarkWaitGroupAddDone(b
, 0)
123 func BenchmarkWaitGroupAddDoneWork(b
*testing
.B
) {
124 benchmarkWaitGroupAddDone(b
, 100)
127 func benchmarkWaitGroupWait(b
*testing
.B
, localWork
int) {
129 b
.RunParallel(func(pb
*testing
.PB
) {
133 for i
:= 0; i
< localWork
; i
++ {
142 func BenchmarkWaitGroupWait(b
*testing
.B
) {
143 benchmarkWaitGroupWait(b
, 0)
146 func BenchmarkWaitGroupWaitWork(b
*testing
.B
) {
147 benchmarkWaitGroupWait(b
, 100)