Skip to content

Commit

Permalink
ut: add concurrent ut for URI & QueryArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever committed Aug 28, 2023
1 parent fcc25cc commit 47a0270
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
46 changes: 41 additions & 5 deletions pkg/protocol/http1/req/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ import (
"encoding/base64"
"errors"
"fmt"
"golang.org/x/sync/errgroup"
"io"
"io/ioutil"
"mime/multipart"
"net/url"
"strings"
"testing"

"golang.org/x/sync/errgroup"

"github.com/cloudwego/hertz/internal/bytesconv"
"github.com/cloudwego/hertz/internal/bytestr"
"github.com/cloudwego/hertz/pkg/common/bytebufferpool"
Expand Down Expand Up @@ -204,6 +205,44 @@ func TestMethodAndPathAndQueryString(t *testing.T) {
}
}

func TestCopyURIMethodAndPathAndQueryString(t *testing.T) {
s := "PUT /foo/bar?query=1 HTTP/1.1\r\nExpect: 100-continue\r\nContent-Length: 5\r\nContent-Type: foo/bar\r\n\r\nabcdef4343"
zr := mock.NewZeroCopyReader(s)

var r protocol.Request
if err := Read(&r, zr); err != nil {
t.Fatalf("unexpected error: %s", err)
}

var copyR protocol.Request
r.CopyToAndMark(&copyR)

errG := errgroup.Group{}

for i := 0; i < 500; i++ {
errG.Go(func() error {
if string(copyR.RequestURI()) != "/foo/bar?query=1" {
return errors.New(fmt.Sprintf("unexpected request uri %s. Expecting %s", r.RequestURI(), "/foo/bar?query=1"))
}
if string(copyR.Method()) != "PUT" {
return errors.New(fmt.Sprintf("unexpected method %s. Expecting %s", r.Header.Method(), "PUT"))
}

if string(copyR.Path()) != "/foo/bar" {
return errors.New(fmt.Sprintf("unexpected uri path %s. Expecting %s", r.URI().Path(), "/foo/bar"))
}
if string(copyR.QueryString()) != "query=1" {
return errors.New(fmt.Sprintf("unexpected query string %s. Expecting %s", r.URI().QueryString(), "query=1"))
}

return nil
})
}

err := errG.Wait()
assert.Nil(t, err)
}

func TestRequestSuccess(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -407,8 +446,7 @@ func TestCopyRequestPostArgsBodyStream(t *testing.T) {
for i := 0; i < 500; i++ {
errG.Go(func() error {
if string(copyReq.PostArgs().Peek("key")) != content {
//assert.DeepEqual(t, content, string(copyReq.PostArgs().Peek("key")))
return errors.New("error happened")
return errors.New("race error happened")
}
return nil
})
Expand Down Expand Up @@ -1522,7 +1560,6 @@ Content-Type: application/octet-stream
eg.Go(func() error {
return testCopyRequestReadMultipartForm(t, &copyRequest)
})

}
err := eg.Wait()
assert.Nil(t, err)
Expand All @@ -1534,7 +1571,6 @@ Content-Type: application/octet-stream
eg.Go(func() error {
return testCopyRequestReadMultipartForm(t, &r)
})

}
err = eg.Wait()
assert.NotNil(t, err)
Expand Down
30 changes: 30 additions & 0 deletions pkg/protocol/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ import (
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"

"github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/test/assert"
"golang.org/x/sync/errgroup"
)

func TestURI_Username(t *testing.T) {
Expand Down Expand Up @@ -250,6 +253,33 @@ func TestURICopyToQueryArgs(t *testing.T) {
assert.DeepEqual(t, "bar", string(a1.Peek("foo")))
}

func TestCopyURI_QueryArgs(t *testing.T) {
t.Parallel()

var u URI
a := u.QueryArgs()
k := strings.Repeat("foo", 1000)
v := strings.Repeat("bar", 1000)
a.Set(k, v)

var copyU URI
u.CopyToAndMark(&copyU)

errG := errgroup.Group{}

for i := 0; i < 500; i++ {
errG.Go(func() error {
if string(copyU.QueryArgs().Peek(k)) != v {
return errors.NewPrivate("race error happened")
}
return nil
})
}

err := errG.Wait()
assert.Nil(t, err)
}

func TestURICopyTo(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 47a0270

Please sign in to comment.