diff options
Diffstat (limited to 'src/pkg/sync/waitgroup_test.go')
| -rw-r--r-- | src/pkg/sync/waitgroup_test.go | 60 | 
1 files changed, 60 insertions, 0 deletions
| diff --git a/src/pkg/sync/waitgroup_test.go b/src/pkg/sync/waitgroup_test.go new file mode 100644 index 000000000..fe35732e7 --- /dev/null +++ b/src/pkg/sync/waitgroup_test.go @@ -0,0 +1,60 @@ +// Copyright 2011 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 sync_test + +import ( +	. "sync" +	"testing" +) + +func testWaitGroup(t *testing.T, wg1 *WaitGroup, wg2 *WaitGroup) { +	n := 16 +	wg1.Add(n) +	wg2.Add(n) +	exited := make(chan bool, n) +	for i := 0; i != n; i++ { +		go func(i int) { +			wg1.Done() +			wg2.Wait() +			exited <- true +		}(i) +	} +	wg1.Wait() +	for i := 0; i != n; i++ { +		select { +		case <-exited: +			t.Fatal("WaitGroup released group too soon") +		default: +		} +		wg2.Done() +	} +	for i := 0; i != n; i++ { +		<-exited // Will block if barrier fails to unlock someone. +	} +} + +func TestWaitGroup(t *testing.T) { +	wg1 := &WaitGroup{} +	wg2 := &WaitGroup{} + +	// Run the same test a few times to ensure barrier is in a proper state. +	for i := 0; i != 8; i++ { +		testWaitGroup(t, wg1, wg2) +	} +} + +func TestWaitGroupMisuse(t *testing.T) { +	defer func() { +		err := recover() +		if err != "sync: negative WaitGroup count" { +			t.Fatalf("Unexpected panic: %#v", err) +		} +	}() +	wg := &WaitGroup{} +	wg.Add(1) +	wg.Done() +	wg.Done() +	t.Fatal("Should panic") +} | 
