update garray unit tests.

This commit is contained in:
jroam
2019-07-08 17:34:32 +08:00
parent a9a5a78e02
commit 46721d7552

View File

@ -642,16 +642,19 @@ func TestStringArray_Remove(t *testing.T) {
func TestStringArray_RLockFunc(t *testing.T) {
gtest.Case(t, func() {
s1 := []string{"a", "b", "c", "d"}
a1 := garray.NewStringArrayFrom(s1)
a1 := garray.NewStringArrayFrom(s1, true)
ch1 := make(chan int64, 2)
//go a1.RLockFunc(func(n1 []string) { //读锁
// n1[2] = "g"
// time.Sleep(3 * time.Second) //暂停一秒
//})
ch1 := make(chan int64, 3)
//go1
go a1.RLockFunc(func(n1 []string) { //读锁
n1[2] = "g"
time.Sleep(2 * time.Second) //暂停2秒
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
})
//go2
go func() {
time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等另一个goroutine执行锁后,再开始执行.
time.Sleep(100 * time.Millisecond) //故意暂停0.01秒,等go1执行锁后,再开始执行.
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
a1.Len()
ch1 <- gconv.Int64(time.Now().UnixNano() / 1000 / 1000)
@ -659,9 +662,10 @@ func TestStringArray_RLockFunc(t *testing.T) {
t1 := <-ch1
t2 := <-ch1
// 由于另一个goroutine加的读锁其它可读,所以ch1的操作间隔是很小的.a.len 操作并没有等待,
<-ch1 //等待go1完成
// 防止ci抖动,以豪秒为单位
gtest.AssertLT(t2-t1, 20)
//gtest.Assert(a1.Contains("g"), true)
gtest.Assert(a1.Contains("g"), true)
})
}