sync: Add monitor tests
This commit is contained in:
parent
6e902df516
commit
eebe3d2179
47
sync/monitor_test.go
Normal file
47
sync/monitor_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package usync
|
||||
|
||||
import "testing"
|
||||
import "math/rand"
|
||||
|
||||
func TestMonitor (test *testing.T) {
|
||||
mon := NewMonitor(9)
|
||||
func () {
|
||||
value, done := mon.Borrow()
|
||||
defer done()
|
||||
test.Log(value)
|
||||
if value != 9 { test.Fatal("not equal") }
|
||||
} ()
|
||||
func () {
|
||||
value, done := mon.BorrowReturn()
|
||||
value += 3
|
||||
defer done(value)
|
||||
} ()
|
||||
func () {
|
||||
value, done := mon.Borrow()
|
||||
defer done()
|
||||
test.Log(value)
|
||||
if value != 12 { test.Fatal("not equal") }
|
||||
} ()
|
||||
mon.Set(11)
|
||||
func () {
|
||||
value, done := mon.Borrow()
|
||||
defer done()
|
||||
test.Log(value)
|
||||
if value != 11 { test.Fatal("not equal") }
|
||||
} ()
|
||||
}
|
||||
|
||||
func TestMonitorConcurrent (test *testing.T) {
|
||||
mon := NewMonitor(map[int] int { })
|
||||
for index := 0; index < 16; index ++ {
|
||||
go func () {
|
||||
for index := 0; index < 8192; index ++ {
|
||||
func () {
|
||||
value, done := mon.Borrow()
|
||||
defer done()
|
||||
value[rand.Int()] = rand.Int()
|
||||
} ()
|
||||
}
|
||||
} ()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user