test: add more tests (#1106)

* style: format code

* test: add more tests

* fix: staticcheck errors
This commit is contained in:
Kevin Wan
2021-10-01 10:03:56 +08:00
committed by GitHub
parent d1bfb5ef61
commit d1e702e8a3
3 changed files with 54 additions and 6 deletions

View File

@@ -1,11 +1,14 @@
package syncx
import (
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/lang"
)
func TestTryLock(t *testing.T) {
@@ -37,3 +40,31 @@ func TestSpinLockRace(t *testing.T) {
wait.Wait()
assert.True(t, lock.TryLock())
}
func TestSpinLock_TryLock(t *testing.T) {
var lock SpinLock
var count int32
var wait sync.WaitGroup
wait.Add(2)
sig := make(chan lang.PlaceholderType)
go func() {
lock.TryLock()
sig <- lang.Placeholder
atomic.AddInt32(&count, 1)
runtime.Gosched()
lock.Unlock()
wait.Done()
}()
go func() {
<-sig
lock.Lock()
atomic.AddInt32(&count, 1)
lock.Unlock()
wait.Done()
}()
wait.Wait()
assert.Equal(t, int32(2), atomic.LoadInt32(&count))
}