github.com/aloncn/graphics-go@v0.0.1/src/runtime/race/testdata/finalizer_test.go (about) 1 // Copyright 2012 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. 4 5 package race_test 6 7 import ( 8 "runtime" 9 "sync" 10 "testing" 11 "time" 12 ) 13 14 func TestNoRaceFin(t *testing.T) { 15 c := make(chan bool) 16 go func() { 17 x := new(string) 18 runtime.SetFinalizer(x, func(x *string) { 19 *x = "foo" 20 }) 21 *x = "bar" 22 c <- true 23 }() 24 <-c 25 runtime.GC() 26 time.Sleep(100 * time.Millisecond) 27 } 28 29 var finVar struct { 30 sync.Mutex 31 cnt int 32 } 33 34 func TestNoRaceFinGlobal(t *testing.T) { 35 c := make(chan bool) 36 go func() { 37 x := new(string) 38 runtime.SetFinalizer(x, func(x *string) { 39 finVar.Lock() 40 finVar.cnt++ 41 finVar.Unlock() 42 }) 43 c <- true 44 }() 45 <-c 46 runtime.GC() 47 time.Sleep(100 * time.Millisecond) 48 finVar.Lock() 49 finVar.cnt++ 50 finVar.Unlock() 51 } 52 53 func TestRaceFin(t *testing.T) { 54 c := make(chan bool) 55 y := 0 56 go func() { 57 x := new(string) 58 runtime.SetFinalizer(x, func(x *string) { 59 y = 42 60 }) 61 c <- true 62 }() 63 <-c 64 runtime.GC() 65 time.Sleep(100 * time.Millisecond) 66 y = 66 67 }