You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
677 B
Go
42 lines
677 B
Go
4 years ago
|
package syncx
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestTryLock(t *testing.T) {
|
||
|
var lock SpinLock
|
||
|
assert.True(t, lock.TryLock())
|
||
|
assert.False(t, lock.TryLock())
|
||
|
lock.Unlock()
|
||
|
assert.True(t, lock.TryLock())
|
||
|
}
|
||
|
|
||
|
func TestSpinLock(t *testing.T) {
|
||
|
var lock SpinLock
|
||
|
lock.Lock()
|
||
|
assert.False(t, lock.TryLock())
|
||
|
lock.Unlock()
|
||
|
assert.True(t, lock.TryLock())
|
||
|
}
|
||
|
|
||
|
func TestSpinLockRace(t *testing.T) {
|
||
|
var lock SpinLock
|
||
|
lock.Lock()
|
||
|
var wait sync.WaitGroup
|
||
|
wait.Add(1)
|
||
|
go func() {
|
||
|
lock.Lock()
|
||
|
lock.Unlock()
|
||
|
wait.Done()
|
||
|
}()
|
||
|
time.Sleep(time.Millisecond * 100)
|
||
|
lock.Unlock()
|
||
|
wait.Wait()
|
||
|
assert.True(t, lock.TryLock())
|
||
|
}
|