mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 11:48:09 +08:00
Merge branch '完善garray测试' of https://github.com/jroam/gf into 完善garray测试
This commit is contained in:
commit
b5f117e932
@ -26,6 +26,10 @@ before_install:
|
||||
install:
|
||||
- cat /etc/hosts
|
||||
|
||||
before_script:
|
||||
- find . -name "*.go" | xargs gofmt -w
|
||||
- git diff --exit-code
|
||||
|
||||
script:
|
||||
- cd g
|
||||
- GOARCH=386 go test -v ./...
|
||||
|
@ -668,7 +668,7 @@ func TestSortedIntArray_LockFunc(t *testing.T) {
|
||||
gtest.Assert(i, n1[i-1])
|
||||
}
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -695,7 +695,7 @@ func TestSortedIntArray_RLockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []int) { //读锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -759,7 +759,7 @@ func TestSortedArray_LockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -788,7 +788,7 @@ func TestSortedArray_RLockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -868,7 +868,7 @@ func TestIntArray_LockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []int) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -894,7 +894,7 @@ func TestIntArray_RLockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []int) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
|
@ -737,7 +737,7 @@ func TestArray_LockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -763,7 +763,7 @@ func TestArray_RLockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []interface{}) { //互斥锁
|
||||
n1[3] = 7
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
|
@ -636,7 +636,7 @@ func TestSortedStringArray_LockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []string) { //互斥锁
|
||||
n1[3] = "e"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -662,7 +662,7 @@ func TestSortedStringArray_RLockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []string) { //读锁
|
||||
n1[3] = "e"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -741,7 +741,7 @@ func TestStringArray_LockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.LockFunc(func(n1 []string) { //互斥锁
|
||||
n1[3] = "f"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
@ -767,7 +767,7 @@ func TestStringArray_RLockFunc(t *testing.T) {
|
||||
ch1 := make(chan int64, 2)
|
||||
go a1.RLockFunc(func(n1 []string) { //读锁
|
||||
n1[2] = "g"
|
||||
time.Sleep(1 * time.Second) //暂停一秒
|
||||
time.Sleep(3 * time.Second) //暂停一秒
|
||||
})
|
||||
|
||||
go func() {
|
||||
|
@ -11,8 +11,6 @@ package gcrc32_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/g/crypto/gmd5"
|
||||
|
||||
"github.com/gogf/gf/g/crypto/gcrc32"
|
||||
"github.com/gogf/gf/g/crypto/gmd5"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
|
@ -9,12 +9,12 @@ package gmd5
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/internal/errors"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/gogf/gf/g/internal/errors"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
// Encrypt encrypts any type of variable using MD5 algorithms.
|
||||
|
@ -10,10 +10,11 @@ package gsha1
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"github.com/gogf/gf/g/internal/errors"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/gogf/gf/g/internal/errors"
|
||||
"github.com/gogf/gf/g/util/gconv"
|
||||
)
|
||||
|
||||
// Encrypt encrypts any type of variable using SHA1 algorithms.
|
||||
|
@ -14,13 +14,14 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
)
|
||||
|
||||
// http客户端
|
||||
@ -82,7 +83,7 @@ func (c *Client) Put(url string, data ...interface{}) (*ClientResponse, error) {
|
||||
// POST请求提交数据,默认使用表单方式提交数据(绝大部分场景下也是如此)。
|
||||
// 如果服务端对Content-Type有要求,可使用Client对象进行请求,单独设置相关属性。
|
||||
// 支持文件上传,需要字段格式为:FieldName=@file:
|
||||
func (c *Client) Post(url string, data ...interface{}) (*ClientResponse, error) {
|
||||
func (c *Client) Post(url string, data ...interface{}) (resp *ClientResponse, err error) {
|
||||
if len(c.prefix) > 0 {
|
||||
url = c.prefix + url
|
||||
}
|
||||
@ -119,23 +120,27 @@ func (c *Client) Post(url string, data ...interface{}) (*ClientResponse, error)
|
||||
}
|
||||
}
|
||||
writer.Close()
|
||||
if r, err := http.NewRequest("POST", url, buffer); err != nil {
|
||||
if req, err = http.NewRequest("POST", url, buffer); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
req = r
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
}
|
||||
} else {
|
||||
// 识别提交数据格式
|
||||
// 普通请求
|
||||
paramBytes := []byte(param)
|
||||
if r, err := http.NewRequest("POST", url, bytes.NewReader(paramBytes)); err != nil {
|
||||
if req, err = http.NewRequest("POST", url, bytes.NewReader(paramBytes)); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
req = r
|
||||
if json.Valid(paramBytes) {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if v, ok := c.header["Content-Type"]; ok {
|
||||
// 自定义请求类型
|
||||
req.Header.Set("Content-Type", v)
|
||||
} else {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
// 识别提交数据格式
|
||||
if json.Valid(paramBytes) {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
} else {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -163,23 +168,22 @@ func (c *Client) Post(url string, data ...interface{}) (*ClientResponse, error)
|
||||
req.SetBasicAuth(c.authUser, c.authPass)
|
||||
}
|
||||
// 执行请求
|
||||
resp := (*http.Response)(nil)
|
||||
r := (*http.Response)(nil)
|
||||
for {
|
||||
if r, err := c.Do(req); err != nil {
|
||||
if r, err = c.Do(req); err != nil {
|
||||
if c.retryCount > 0 {
|
||||
c.retryCount--
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
resp = r
|
||||
break
|
||||
}
|
||||
}
|
||||
r := &ClientResponse{
|
||||
resp = &ClientResponse{
|
||||
cookies: make(map[string]string),
|
||||
}
|
||||
r.Response = resp
|
||||
resp.Response = r
|
||||
// 浏览器模式
|
||||
if c.browserMode {
|
||||
now := time.Now()
|
||||
@ -191,7 +195,7 @@ func (c *Client) Post(url string, data ...interface{}) (*ClientResponse, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
return r, nil
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// DELETE请求
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
@ -7,10 +7,12 @@
|
||||
package gtcp
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/g/internal/errors"
|
||||
|
||||
"github.com/gogf/gf/g/container/gmap"
|
||||
"github.com/gogf/gf/g/container/gpool"
|
||||
"github.com/gogf/gf/g/internal/errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 链接池链接对象
|
||||
|
@ -7,7 +7,6 @@
|
||||
package gtcp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/g/internal/errors"
|
||||
|
@ -7,7 +7,6 @@
|
||||
package gudp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
@ -98,62 +98,7 @@ func Test_Locker_TryLock(t *testing.T) {
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Locker_LockFunc(t *testing.T) {
|
||||
//no expire
|
||||
gtest.Case(t, func() {
|
||||
key := "testLockFunc"
|
||||
array := garray.New()
|
||||
go func() {
|
||||
gmlock.LockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}) //
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.LockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1) //
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Locker_TryLockFunc(t *testing.T) {
|
||||
//no expire
|
||||
gtest.Case(t, func() {
|
||||
key := "testTryLockFunc"
|
||||
array := garray.New()
|
||||
go func() {
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Locker_LockFunc(t *testing.T) {
|
||||
@ -180,30 +125,6 @@ func Test_Locker_LockFunc(t *testing.T) {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
//expire
|
||||
gtest.Case(t, func() {
|
||||
key := "testLockFuncExpire"
|
||||
array := garray.New()
|
||||
go func() {
|
||||
gmlock.LockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}, 100*time.Millisecond) //
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gmlock.LockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2) //
|
||||
time.Sleep(350 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
func Test_Locker_TryLockFunc(t *testing.T) {
|
||||
//no expire
|
||||
@ -233,58 +154,4 @@ func Test_Locker_TryLockFunc(t *testing.T) {
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
//expire1
|
||||
gtest.Case(t, func() {
|
||||
key := "testTryLockFuncExpire1"
|
||||
array := garray.New()
|
||||
go func() {
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
}, 50*time.Millisecond)
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
|
||||
//expire2
|
||||
gtest.Case(t, func() {
|
||||
key := "testTryLockFuncExpire2"
|
||||
array := garray.New()
|
||||
go func() {
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}, 50*time.Millisecond) //unlock after expire, before func finish.
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gmlock.TryLockFunc(key, func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(70 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
@ -1,258 +0,0 @@
|
||||
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gmlock_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/g/container/garray"
|
||||
"github.com/gogf/gf/g/os/gmlock"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Mutex_RUnlock(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
for index := 0; index < 1000; index++ {
|
||||
go func() {
|
||||
mu.RLockFunc(func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), true)
|
||||
gtest.Assert(mu.IsLocked(), true)
|
||||
gtest.Assert(mu.IsWLocked(), false)
|
||||
for index := 0; index < 1000; index++ {
|
||||
go func() {
|
||||
mu.RUnlock()
|
||||
}()
|
||||
}
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), false)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_IsLocked(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(mu.IsLocked(), true)
|
||||
gtest.Assert(mu.IsWLocked(), true)
|
||||
gtest.Assert(mu.IsRLocked(), false)
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
gtest.Assert(mu.IsLocked(), false)
|
||||
gtest.Assert(mu.IsWLocked(), false)
|
||||
|
||||
go func() {
|
||||
mu.RLockFunc(func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), true)
|
||||
gtest.Assert(mu.IsLocked(), true)
|
||||
gtest.Assert(mu.IsWLocked(), false)
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
gtest.Assert(mu.IsRLocked(), false)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_Unlock(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
array := garray.New()
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
mu.Unlock()
|
||||
mu.Unlock()
|
||||
mu.Unlock()
|
||||
}()
|
||||
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_LockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
array := garray.New()
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_TryLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
array := garray.New()
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.TryLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(110 * time.Millisecond)
|
||||
mu.TryLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_RLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
array := garray.New()
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.RLockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 2)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
array := garray.New()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.RLockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.RLockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.RLockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
gtest.Assert(array.Len(), 0)
|
||||
time.Sleep(80 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Mutex_TryRLockFunc(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
mu := gmlock.NewMutex()
|
||||
array := garray.New()
|
||||
go func() {
|
||||
mu.LockFunc(func() {
|
||||
array.Append(1)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
mu.TryRLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(700 * time.Millisecond)
|
||||
mu.TryRLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
go func() {
|
||||
time.Sleep(700 * time.Millisecond)
|
||||
mu.TryRLockFunc(func() {
|
||||
array.Append(1)
|
||||
})
|
||||
}()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 1)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
gtest.Assert(array.Len(), 3)
|
||||
})
|
||||
}
|
@ -8,9 +8,10 @@
|
||||
package gmutex
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
"math"
|
||||
"runtime"
|
||||
|
||||
"github.com/gogf/gf/g/container/gtype"
|
||||
)
|
||||
|
||||
// The high level Mutex, which implements more rich features for mutex.
|
||||
@ -136,7 +137,6 @@ func (m *Mutex) TryRLock() bool {
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
m.locking.Set(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user