2012-02-16 21:50:37 -07:00
|
|
|
// run
|
2010-02-03 17:31:34 -07:00
|
|
|
|
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
// trivial finalizer test
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2010-03-26 15:15:30 -06:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
)
|
2010-02-03 17:31:34 -07:00
|
|
|
|
|
|
|
const N = 250
|
|
|
|
|
|
|
|
type A struct {
|
|
|
|
b *B
|
|
|
|
n int
|
|
|
|
}
|
|
|
|
|
|
|
|
type B struct {
|
|
|
|
n int
|
|
|
|
}
|
|
|
|
|
|
|
|
var i int
|
|
|
|
var nfinal int
|
|
|
|
var final [N]int
|
|
|
|
|
2010-03-26 15:15:30 -06:00
|
|
|
// the unused return is to test finalizers with return values
|
|
|
|
func finalA(a *A) (unused [N]int) {
|
2010-02-03 17:31:34 -07:00
|
|
|
if final[a.n] != 0 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("finalA", a.n, final[a.n])
|
|
|
|
panic("fail")
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
final[a.n] = 1
|
2010-03-26 15:15:30 -06:00
|
|
|
return
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func finalB(b *B) {
|
|
|
|
if final[b.n] != 1 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("finalB", b.n, final[b.n])
|
|
|
|
panic("fail")
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
final[b.n] = 2
|
|
|
|
nfinal++
|
|
|
|
}
|
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
func nofinalB(b *B) {
|
|
|
|
panic("nofinalB run")
|
|
|
|
}
|
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
func main() {
|
|
|
|
runtime.GOMAXPROCS(4)
|
|
|
|
for i = 0; i < N; i++ {
|
|
|
|
b := &B{i}
|
|
|
|
a := &A{b, i}
|
2011-10-06 09:42:51 -06:00
|
|
|
c := new(B)
|
|
|
|
runtime.SetFinalizer(c, nofinalB)
|
2010-02-03 17:31:34 -07:00
|
|
|
runtime.SetFinalizer(b, finalB)
|
|
|
|
runtime.SetFinalizer(a, finalA)
|
2011-10-06 09:42:51 -06:00
|
|
|
runtime.SetFinalizer(c, nil)
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
runtime.GC()
|
|
|
|
runtime.Gosched()
|
2010-03-30 11:34:57 -06:00
|
|
|
time.Sleep(1e6)
|
2011-10-06 09:42:51 -06:00
|
|
|
if nfinal >= N*8/10 {
|
|
|
|
break
|
|
|
|
}
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
2010-03-24 20:52:07 -06:00
|
|
|
if nfinal < N*8/10 {
|
2010-03-30 11:34:57 -06:00
|
|
|
println("not enough finalizing:", nfinal, "/", N)
|
|
|
|
panic("fail")
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
}
|