diff --git a/.travis.yml b/.travis.yml index f522ba548..5c05fa0a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 ./... diff --git a/g/container/garray/garray_z_unit_int_test.go b/g/container/garray/garray_z_unit_int_test.go index b8a5d2f6d..3dd6b0b5c 100644 --- a/g/container/garray/garray_z_unit_int_test.go +++ b/g/container/garray/garray_z_unit_int_test.go @@ -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() { diff --git a/g/container/garray/garray_z_unit_interface_test.go b/g/container/garray/garray_z_unit_interface_test.go index f4f0fd9a6..92f243ed4 100644 --- a/g/container/garray/garray_z_unit_interface_test.go +++ b/g/container/garray/garray_z_unit_interface_test.go @@ -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() { diff --git a/g/container/garray/garray_z_unit_string_test.go b/g/container/garray/garray_z_unit_string_test.go index bca25e1eb..7708cf3d8 100644 --- a/g/container/garray/garray_z_unit_string_test.go +++ b/g/container/garray/garray_z_unit_string_test.go @@ -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() { diff --git a/g/crypto/gcrc32/gcrc32_test.go b/g/crypto/gcrc32/gcrc32_test.go index f1977d098..6472a7a01 100644 --- a/g/crypto/gcrc32/gcrc32_test.go +++ b/g/crypto/gcrc32/gcrc32_test.go @@ -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" diff --git a/g/crypto/gmd5/gmd5.go b/g/crypto/gmd5/gmd5.go index 34893d7ff..1a9a56ea3 100644 --- a/g/crypto/gmd5/gmd5.go +++ b/g/crypto/gmd5/gmd5.go @@ -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. diff --git a/g/crypto/gsha1/gsha1.go b/g/crypto/gsha1/gsha1.go index 7ab16c27a..e38a8299c 100644 --- a/g/crypto/gsha1/gsha1.go +++ b/g/crypto/gsha1/gsha1.go @@ -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. diff --git a/g/net/ghttp/ghttp_client_request.go b/g/net/ghttp/ghttp_client_request.go index ec629715c..70b6cfba8 100644 --- a/g/net/ghttp/ghttp_client_request.go +++ b/g/net/ghttp/ghttp_client_request.go @@ -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请求 diff --git a/g/net/gtcp/gtcp_conn.go b/g/net/gtcp/gtcp_conn.go index 171642f35..43ee80ec3 100644 --- a/g/net/gtcp/gtcp_conn.go +++ b/g/net/gtcp/gtcp_conn.go @@ -10,7 +10,6 @@ import ( "bufio" "bytes" "crypto/tls" - "errors" "io" "net" "time" diff --git a/g/net/gtcp/gtcp_pool.go b/g/net/gtcp/gtcp_pool.go index 1f3ceaaf1..ec706e344 100644 --- a/g/net/gtcp/gtcp_pool.go +++ b/g/net/gtcp/gtcp_pool.go @@ -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" ) // 链接池链接对象 diff --git a/g/net/gtcp/gtcp_pool_pkg.go b/g/net/gtcp/gtcp_pool_pkg.go index 861a4967e..03a182de6 100644 --- a/g/net/gtcp/gtcp_pool_pkg.go +++ b/g/net/gtcp/gtcp_pool_pkg.go @@ -7,7 +7,6 @@ package gtcp import ( - "errors" "time" "github.com/gogf/gf/g/internal/errors" diff --git a/g/net/gudp/gudp_conn.go b/g/net/gudp/gudp_conn.go index 119d8ec00..2e6c9fdf0 100644 --- a/g/net/gudp/gudp_conn.go +++ b/g/net/gudp/gudp_conn.go @@ -7,7 +7,6 @@ package gudp import ( - "errors" "io" "net" "time" diff --git a/g/os/gmlock/gmlock_unit_lock_test.go b/g/os/gmlock/gmlock_unit_lock_test.go index c9ce524a8..da1132d4b 100644 --- a/g/os/gmlock/gmlock_unit_lock_test.go +++ b/g/os/gmlock/gmlock_unit_lock_test.go @@ -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) - }) } diff --git a/g/os/gmlock/gmlock_unit_mutex_test.go b/g/os/gmlock/gmlock_unit_mutex_test.go deleted file mode 100644 index 0e0b245d3..000000000 --- a/g/os/gmlock/gmlock_unit_mutex_test.go +++ /dev/null @@ -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) - }) -} diff --git a/g/os/gmutex/gmutex.go b/g/os/gmutex/gmutex.go index 6217520e2..7acc97df9 100644 --- a/g/os/gmutex/gmutex.go +++ b/g/os/gmutex/gmutex.go @@ -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) } }