blob: 4c832583e075520f13250e1694d4da9b1e79427a [file] [log] [blame]
Russ Cox33e396a2010-02-03 16:31:34 -08001// $G $D/$F.go && $L $F.$A && ./$A.out
2
3// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// trivial finalizer test
8
9package main
10
11import "runtime"
12
13const N = 250
14
15type A struct {
16 b *B
17 n int
18}
19
20type B struct {
21 n int
22}
23
24var i int
25var nfinal int
26var final [N]int
27
28func finalA(a *A) {
29 if final[a.n] != 0 {
30 panicln("finalA", a.n, final[a.n])
31 }
32 final[a.n] = 1
33}
34
35func finalB(b *B) {
36 if final[b.n] != 1 {
37 panicln("finalB", b.n, final[b.n])
38 }
39 final[b.n] = 2
40 nfinal++
41}
42
43func main() {
44 runtime.GOMAXPROCS(4)
45 for i = 0; i < N; i++ {
46 b := &B{i}
47 a := &A{b, i}
48 runtime.SetFinalizer(b, finalB)
49 runtime.SetFinalizer(a, finalA)
50 }
51 for i := 0; i < N; i++ {
52 runtime.GC()
53 runtime.Gosched()
54 }
55 if nfinal < N*9/10 {
56 panic("not enough finalizing:", nfinal, "/", N)
57 }
58}