Revert r215321.
[official-gcc.git] / libgo / go / sync / once_test.go
blob8afda82f3e18dc74f77209c4d652eaa9a49c4d48
1 // Copyright 2009 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.
5 package sync_test
7 import (
8 . "sync"
9 "testing"
12 type one int
14 func (o *one) Increment() {
15 *o++
18 func run(t *testing.T, once *Once, o *one, c chan bool) {
19 once.Do(func() { o.Increment() })
20 if v := *o; v != 1 {
21 t.Errorf("once failed inside run: %d is not 1", v)
23 c <- true
26 func TestOnce(t *testing.T) {
27 o := new(one)
28 once := new(Once)
29 c := make(chan bool)
30 const N = 10
31 for i := 0; i < N; i++ {
32 go run(t, once, o, c)
34 for i := 0; i < N; i++ {
35 <-c
37 if *o != 1 {
38 t.Errorf("once failed outside run: %d is not 1", *o)
42 func TestOncePanic(t *testing.T) {
43 once := new(Once)
44 for i := 0; i < 2; i++ {
45 func() {
46 defer func() {
47 if recover() == nil {
48 t.Fatalf("Once.Do() has not panic'ed")
50 }()
51 once.Do(func() {
52 panic("failed")
54 }()
56 once.Do(func() {})
57 once.Do(func() {
58 t.Fatalf("Once called twice")
62 func BenchmarkOnce(b *testing.B) {
63 var once Once
64 f := func() {}
65 b.RunParallel(func(pb *testing.PB) {
66 for pb.Next() {
67 once.Do(f)