mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-06 05:37:51 +08:00
51 lines
577 B
Go
51 lines
577 B
Go
package umutex
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestTryLock(t *testing.T) {
|
|
var result bool
|
|
|
|
mutex := New()
|
|
|
|
result = mutex.TryLock()
|
|
if result != true {
|
|
t.Error()
|
|
}
|
|
|
|
result = mutex.TryLock()
|
|
if result != false {
|
|
t.Error()
|
|
}
|
|
|
|
mutex.Unlock()
|
|
|
|
result = mutex.TryLock()
|
|
if result != true {
|
|
t.Error()
|
|
}
|
|
}
|
|
|
|
func TestForceLock(t *testing.T) {
|
|
var result bool
|
|
|
|
mutex := New()
|
|
|
|
result = mutex.TryLock()
|
|
if result != true {
|
|
t.Error()
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
mutex.ForceLock()
|
|
}()
|
|
|
|
mutex.Unlock()
|
|
wg.Wait()
|
|
}
|