mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-21 16:58:08 +08:00
Using waitgroup to wait asynchronous test case
This commit is contained in:
parent
8709d894ef
commit
bfbe6a708f
@ -16,6 +16,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -1818,15 +1819,19 @@ func TestRaceParamsContextCopy(t *testing.T) {
|
|||||||
DefaultWriter = os.Stdout
|
DefaultWriter = os.Stdout
|
||||||
router := Default()
|
router := Default()
|
||||||
nameGroup := router.Group("/:name")
|
nameGroup := router.Group("/:name")
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
{
|
{
|
||||||
nameGroup.GET("/api", func(c *Context) {
|
nameGroup.GET("/api", func(c *Context) {
|
||||||
go func(c *Context, param string) {
|
go func(c *Context, param string) {
|
||||||
time.Sleep(100 * time.Millisecond)
|
defer wg.Done()
|
||||||
|
// First assert must be executed after the second request
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
assert.Equal(t, c.Param("name"), param)
|
assert.Equal(t, c.Param("name"), param)
|
||||||
}(c.Copy(), c.Param("name"))
|
}(c.Copy(), c.Param("name"))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
performRequest(router, "GET", "/name1/api")
|
performRequest(router, "GET", "/name1/api")
|
||||||
performRequest(router, "GET", "/name2/api")
|
performRequest(router, "GET", "/name2/api")
|
||||||
time.Sleep(200 * time.Millisecond)
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user