Files

33 lines
551 B
Go
Raw Permalink Normal View History

2021-01-14 22:07:58 +01:00
package sync
import (
"fmt"
"runtime"
"testing"
)
2021-01-19 16:34:09 +01:00
func HammerMutex(m *NamedRWMutex, loops int, c chan bool) {
2021-01-14 22:07:58 +01:00
for i := 0; i < loops; i++ {
id := fmt.Sprintf("%v", i)
m.Lock(id)
m.Unlock(id)
}
2021-01-16 16:38:01 +01:00
c <- true
2021-01-14 22:07:58 +01:00
}
2021-01-19 16:34:09 +01:00
func TestNamedRWMutex(t *testing.T) {
2021-01-14 22:07:58 +01:00
if n := runtime.SetMutexProfileFraction(1); n != 0 {
t.Logf("got mutexrate %d expected 0", n)
}
defer runtime.SetMutexProfileFraction(0)
2021-01-19 16:34:09 +01:00
m := NewNamedRWMutex()
2021-01-14 22:07:58 +01:00
c := make(chan bool)
r := 10
for i := 0; i < r; i++ {
go HammerMutex(&m, 2000, c)
}
for i := 0; i < r; i++ {
<-c
}
}