github.com/likebike/go--@v0.0.0-20190911215757-0bd925d16e96/go/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 _ = y 57 go func() { 58 x := new(string) 59 runtime.SetFinalizer(x, func(x *string) { 60 y = 42 61 }) 62 c <- true 63 }() 64 <-c 65 runtime.GC() 66 time.Sleep(100 * time.Millisecond) 67 y = 66 68 }