]> Cypherpunks repositories - gostls13.git/commitdiff
net: fix data race in benchmarks
authorMikio Hara <mikioh.mikioh@gmail.com>
Wed, 1 Apr 2015 03:21:15 +0000 (12:21 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Wed, 1 Apr 2015 22:02:41 +0000 (22:02 +0000)
Fixes #10307.

Change-Id: If70f36a6f1c4e465a47a0bc4d38b318424111106
Reviewed-on: https://go-review.googlesource.com/8330
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/net/dnsclient_unix_test.go
src/net/dnsname_test.go
src/net/interface_test.go
src/net/ip_test.go
src/net/main_test.go
src/net/tcp_test.go

index 2934634769ad07193c9a523fc9584b2495cdd7dc..c85e147a0d57bfe7772a7b541a44ef135b967994 100644 (file)
@@ -219,8 +219,7 @@ func TestReloadResolvConfChange(t *testing.T) {
 }
 
 func BenchmarkGoLookupIP(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        for i := 0; i < b.N; i++ {
                goLookupIP("www.example.com")
@@ -228,8 +227,7 @@ func BenchmarkGoLookupIP(b *testing.B) {
 }
 
 func BenchmarkGoLookupIPNoSuchHost(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        for i := 0; i < b.N; i++ {
                goLookupIP("some.nonexistent")
@@ -237,8 +235,7 @@ func BenchmarkGoLookupIPNoSuchHost(b *testing.B) {
 }
 
 func BenchmarkGoLookupIPWithBrokenNameServer(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        onceLoadConfig.Do(loadDefaultConfig)
        if cfg.dnserr != nil || cfg.dnsConfig == nil {
index 4fecf8dbe24fa01b09a457e618df1d94b447c09a..b118fe1774394e05d2b3a96f530297bb1dabc981 100644 (file)
@@ -69,8 +69,7 @@ func TestDNSNames(t *testing.T) {
 }
 
 func BenchmarkDNSNames(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        benchmarks := append(tests, []testCase{
                {strings.Repeat("a", 63), true},
index 666f11a980c2c8919508460290d1c4b0f9e66a2a..0e5c2e3ddf88defc1603ea1f3b114406ccd9ea3a 100644 (file)
@@ -229,8 +229,7 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) (nmaf4, nmaf6 int) {
 }
 
 func BenchmarkInterfaces(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        for i := 0; i < b.N; i++ {
                if _, err := Interfaces(); err != nil {
@@ -240,8 +239,7 @@ func BenchmarkInterfaces(b *testing.B) {
 }
 
 func BenchmarkInterfaceByIndex(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        ifi := loopbackInterface()
        if ifi == nil {
@@ -255,8 +253,7 @@ func BenchmarkInterfaceByIndex(b *testing.B) {
 }
 
 func BenchmarkInterfaceByName(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        ifi := loopbackInterface()
        if ifi == nil {
@@ -270,8 +267,7 @@ func BenchmarkInterfaceByName(b *testing.B) {
 }
 
 func BenchmarkInterfaceAddrs(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        for i := 0; i < b.N; i++ {
                if _, err := InterfaceAddrs(); err != nil {
@@ -281,8 +277,7 @@ func BenchmarkInterfaceAddrs(b *testing.B) {
 }
 
 func BenchmarkInterfacesAndAddrs(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        ifi := loopbackInterface()
        if ifi == nil {
@@ -296,8 +291,7 @@ func BenchmarkInterfacesAndAddrs(b *testing.B) {
 }
 
 func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        ifi := loopbackInterface()
        if ifi == nil {
index e6f4e650ca74e9334bf4525fa8b97d24a465518e..1215b69d0289bcffa05b9364bf8eabe00ccb3bcf 100644 (file)
@@ -53,8 +53,7 @@ func TestParseIP(t *testing.T) {
 }
 
 func BenchmarkParseIP(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        for i := 0; i < b.N; i++ {
                for _, tt := range parseIPTests {
@@ -111,8 +110,7 @@ func TestIPString(t *testing.T) {
 }
 
 func BenchmarkIPString(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        for i := 0; i < b.N; i++ {
                for _, tt := range ipStringTests {
@@ -164,8 +162,7 @@ func TestIPMaskString(t *testing.T) {
 }
 
 func BenchmarkIPMaskString(b *testing.B) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        for i := 0; i < b.N; i++ {
                for _, tt := range ipMaskStringTests {
index bc0f92ed25ab080483ec7d05b85ad7bfd6e37a48..3ea27f179165e0f8349d8f55d13b5a5d5d777c86 100644 (file)
@@ -11,23 +11,29 @@ import (
        "runtime"
        "sort"
        "strings"
+       "sync"
        "testing"
 )
 
-var sw socktest.Switch
+var (
+       sw socktest.Switch
+
+       // uninstallTestHooks runs just before a run of benchmarks.
+       testHookUninstaller sync.Once
+)
 
 func TestMain(m *testing.M) {
        installTestHooks()
 
        st := m.Run()
 
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
        if !testing.Short() {
                printLeakedGoroutines()
                printLeakedSockets()
                printSocketStats()
        }
        forceCloseSockets()
-       uninstallTestHooks()
        os.Exit(st)
 }
 
index 434c9c6cef82c6972af39bff2579890fd1c2d452..e33e5f2f29230acda7e85feb39c0256d301929e1 100644 (file)
@@ -59,8 +59,7 @@ func BenchmarkTCP6PersistentTimeout(b *testing.B) {
 }
 
 func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
-       uninstallTestHooks()
-       defer installTestHooks()
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
 
        const msgLen = 512
        conns := b.N
@@ -170,13 +169,13 @@ func BenchmarkTCP6ConcurrentReadWrite(b *testing.B) {
 }
 
 func benchmarkTCPConcurrentReadWrite(b *testing.B, laddr string) {
+       testHookUninstaller.Do(func() { uninstallTestHooks() })
+
        // The benchmark creates GOMAXPROCS client/server pairs.
        // Each pair creates 4 goroutines: client reader/writer and server reader/writer.
        // The benchmark stresses concurrent reading and writing to the same connection.
        // Such pattern is used in net/http and net/rpc.
 
-       uninstallTestHooks()
-       defer installTestHooks()
        b.StopTimer()
 
        P := runtime.GOMAXPROCS(0)