Source file
test/tinyfin.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "runtime"
13 "time"
14 )
15
16 func main() {
17
18
19 if runtime.Compiler == "gccgo" {
20 return
21 }
22 const N = 100
23 finalized := make(chan int32, N)
24 for i := 0; i < N; i++ {
25 x := new(int32)
26 *x = int32(i)
27
28 runtime.SetFinalizer(x, func(p *int32) {
29 finalized <- *p
30 })
31 }
32 runtime.GC()
33 count := 0
34 done := make([]bool, N)
35 timeout := time.After(5*time.Second)
36 for {
37 select {
38 case <-timeout:
39 println("timeout,", count, "finalized so far")
40 panic("not all finalizers are called")
41 case x := <-finalized:
42
43
44
45 if x < 0 || x >= N {
46 println("got", x)
47 panic("corrupted")
48 }
49 if done[x] {
50 println("got", x)
51 panic("already finalized")
52 }
53 done[x] = true
54 count++
55 if count > N/10*9 {
56
57
58
59
60 return
61 }
62 }
63 }
64 }
65
View as plain text