-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
141 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,37 @@ | ||
package setting | ||
|
||
import "time" | ||
|
||
// 设置 | ||
type Setting struct { | ||
NotIgnoreEmpty bool //控制是否使用空值 | ||
// 控制是否使用空值 | ||
NotIgnoreEmpty bool | ||
|
||
//是否自动加ContentType | ||
NoAutoContentType bool | ||
|
||
//超时时间 | ||
Timeout time.Duration | ||
|
||
// 目前用作SetTimeout 和 WithContext是互斥 | ||
// index是自增id,主要给互斥API定优先级 | ||
// 对于互斥api,后面的会覆盖前面的 | ||
Index int | ||
|
||
//当前time 的index | ||
TimeoutIndex int | ||
} | ||
|
||
func (s *Setting) SetTimeout(d time.Duration) { | ||
s.Index++ | ||
s.TimeoutIndex = s.Index | ||
s.Timeout = d | ||
} | ||
|
||
func (s *Setting) Reset() { | ||
s.NotIgnoreEmpty = false | ||
s.NoAutoContentType = false | ||
s.Index = 0 | ||
//s.TimeoutIndex = 0 | ||
s.Timeout = time.Duration(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package gout | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func setupDataFlow(t *testing.T) *gin.Engine { | ||
router := gin.New() | ||
|
||
router.GET("/timeout", func(c *gin.Context) { | ||
ctx := c.Request.Context() | ||
select { | ||
case <-ctx.Done(): | ||
fmt.Printf("setTimeout done\n") | ||
case <-time.After(2 * time.Second): | ||
assert.Fail(t, "test timeout fail") | ||
} | ||
}) | ||
|
||
return router | ||
} | ||
|
||
func Test_Global_Timeout(t *testing.T) { | ||
router := setupDataFlow(t) | ||
|
||
const ( | ||
longTimeout = 400 | ||
middleTimeout = 300 | ||
shortTimeout = 200 | ||
) | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP)) | ||
defer ts.Close() | ||
|
||
// 只设置timeout | ||
SetTimeout(shortTimeout * time.Millisecond) //设置全局超时时间 | ||
err := GET(ts.URL + "/timeout").Do() | ||
assert.Error(t, err) | ||
|
||
// 使用互斥api的原则,后面的覆盖前面的 | ||
// 这里是WithContext生效, 超时时间400ms | ||
ctx, _ := context.WithTimeout(context.Background(), longTimeout*time.Millisecond) | ||
s := time.Now() | ||
SetTimeout(shortTimeout * time.Millisecond) // 设置全局超时时间 | ||
err = GET(ts.URL + "/timeout"). | ||
WithContext(ctx). | ||
Do() | ||
|
||
assert.Error(t, err) | ||
assert.GreaterOrEqual(t, int(time.Now().Sub(s)), int(middleTimeout*time.Millisecond)) | ||
|
||
SetTimeout(time.Duration(0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gout | ||
|
||
// Version show version | ||
const Version = "v0.1.5" | ||
const Version = "v0.1.7" |