compiler: Do not declare type switch variable outside case statements.
[official-gcc.git] / libgo / go / sync / once_test.go
blob1eec8d18ea52e93aec5c6d115d30227ad3b383fb
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 var once Once
44 func() {
45 defer func() {
46 if r := recover(); r == nil {
47 t.Fatalf("Once.Do did not panic")
49 }()
50 once.Do(func() {
51 panic("failed")
53 }()
55 once.Do(func() {
56 t.Fatalf("Once.Do called twice")
60 func BenchmarkOnce(b *testing.B) {
61 var once Once
62 f := func() {}
63 b.RunParallel(func(pb *testing.PB) {
64 for pb.Next() {
65 once.Do(f)