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.
40 lines
647 B
Go
40 lines
647 B
Go
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() {
|
|
wait.Done()
|
|
}()
|
|
time.Sleep(time.Millisecond * 100)
|
|
lock.Unlock()
|
|
wait.Wait()
|
|
assert.True(t, lock.TryLock())
|
|
}
|