initial import

This commit is contained in:
kevin
2020-07-26 17:09:05 +08:00
commit 7e3a369a8f
647 changed files with 54754 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
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())
}