add unittest for cluster

This commit is contained in:
hdt3213
2021-05-04 19:43:27 +08:00
parent eeebbbcccf
commit f1ab47bd83
17 changed files with 226 additions and 55 deletions

View File

@@ -1,14 +1,8 @@
package lock
import (
"fmt"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
)
const (
@@ -121,32 +115,3 @@ func (locks *Locks) RUnLocks(keys ...string) {
mu.RUnlock()
}
}
func GoID() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.Atoi(idField)
if err != nil {
panic(fmt.Sprintf("cannot get goroutine id: %v", err))
}
return id
}
func debug(testing.T) {
lm := Locks{}
size := 10
var wg sync.WaitGroup
wg.Add(size)
for i := 0; i < size; i++ {
go func(i int) {
lm.Locks("1", "2")
println("go: " + strconv.Itoa(GoID()))
time.Sleep(time.Second)
println("go: " + strconv.Itoa(GoID()))
lm.UnLocks("1", "2")
wg.Done()
}(i)
}
wg.Wait()
}