github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/test/fixedbugs/issue5493.go (about) 1 // run 2 3 // Copyright 2013 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 package main 8 9 import ( 10 "runtime" 11 "sync" 12 "sync/atomic" 13 "time" 14 ) 15 16 const N = 10 17 var count int64 18 19 func run() error { 20 f1 := func() {} 21 f2 := func() { 22 func() { 23 f1() 24 }() 25 } 26 runtime.SetFinalizer(&f1, func(f *func()) { 27 atomic.AddInt64(&count, -1) 28 }) 29 go f2() 30 return nil 31 } 32 33 func main() { 34 // Does not work on 32-bits due to partially conservative GC. 35 // Try to enable when we have fully precise GC. 36 if runtime.GOARCH != "amd64" { 37 return 38 } 39 count = N 40 var wg sync.WaitGroup 41 wg.Add(N) 42 for i := 0; i < N; i++ { 43 go func() { 44 run() 45 wg.Done() 46 }() 47 } 48 wg.Wait() 49 for i := 0; i < 2*N; i++ { 50 time.Sleep(10 * time.Millisecond) 51 runtime.GC() 52 } 53 if count != 0 { 54 println(count, "out of", N, "finalizer are not called") 55 panic("not all finalizers are called") 56 } 57 } 58