Reviewers: dvyukov, gri,
Message:
Hello dvyukov@google.com, gri@golang.org (cc: fullung@gmail.com,
golang-dev@googlegroups.com),
I'd like you to review this change to
https://code.google.com/p/go
Description:
go/token: add test for concurrent use of FileSet.Pos
Update issue 4354.
Add a test to expose the race in the FileSet position cache.
Please review this at https://codereview.appspot.com/6940078/
Affected files:
M src/pkg/go/token/position_test.go
Index: src/pkg/go/token/position_test.go
===================================================================
--- a/src/pkg/go/token/position_test.go
+++ b/src/pkg/go/token/position_test.go
@@ -6,7 +6,10 @@
import (
"fmt"
+ "math/rand"
+ "runtime"
"testing"
+ "time"
)
func checkPos(t *testing.T, msg string, p, q Position) {
@@ -179,3 +182,30 @@
}
}
}
+
+// issue 4345. Test concurrent use of FileSet.Pos does not trigger a
+// race in the FileSet position cache.
+func TestFileSetRace(t *testing.T) {
+ defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(runtime.NumCPU()))
+ fset := NewFileSet()
+ for i := 0; i < 100; i++ {
+ fset.AddFile(fmt.Sprintf("file-%d", i), fset.Base(), 1031)
+ }
+ max := int32(fset.Base())
+ var done = make(chan struct{})
+ for i := 0; i < runtime.GOMAXPROCS(0)*2; i++ {
+ go func() {
+ for {
+ select {
+ case <-done:
+ return
+ default:
+ p := Pos(rand.Int31n(max))
+ fset.Position(p)
+ }
+ }
+ }()
+ }
+ <-time.After(200 * time.Millisecond)
+ close(done)
+}