Skip to content

Commit

Permalink
Add conncurrent test and a bunch of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jan 27, 2023
1 parent 9d72974 commit e074510
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions js/modules/k6/http/async_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func wrapInAsyncLambda(input string) string {
}

func runOnEventLoop(runtime *modulestest.Runtime, code string) error {
// TODO move this in modulestest.Runtime and extend it
err := runtime.EventLoop.Start(func() error {
_, err := runtime.VU.Runtime().RunString(wrapInAsyncLambda(code))
return err
Expand All @@ -27,22 +28,18 @@ func runOnEventLoop(runtime *modulestest.Runtime, code string) error {

func TestAsyncRequest(t *testing.T) {
t.Parallel()
t.Run("HTTPRequest", func(t *testing.T) {
t.Run("EmptyBody", func(t *testing.T) {
t.Parallel()
t.Run("EmptyBody", func(t *testing.T) {
t.Parallel()
ts := newTestCase(t)
ts := newTestCase(t)

sr := func(input string) string {
return ts.tb.Replacer.Replace(wrapInAsyncLambda(input))
}
_, err := ts.runtime.VU.Runtime().RunString(sr(`
sr := ts.tb.Replacer.Replace
err := runOnEventLoop(ts.runtime, sr(`
var reqUrl = "HTTPBIN_URL/cookies"
var res = http.get(reqUrl);
var res = await http.asyncRequest("GET", reqUrl);
var jar = new http.CookieJar();
jar.set("HTTPBIN_URL/cookies", "key", "value");
res = await http.asyncRequest("GET", "HTTPBIN_URL/cookies", null, { cookies: { key2: "value2" }, jar: jar });
res = await http.asyncRequest("GET", reqUrl, null, { cookies: { key2: "value2" }, jar: jar });
if (res.json().key != "value") { throw new Error("wrong cookie value: " + res.json().key); }
Expand All @@ -55,22 +52,36 @@ func TestAsyncRequest(t *testing.T) {
if (res.request["cookies"]["key2"][0].name != "key2") { throw new Error("wrong http request cookies: " + JSON.stringify(JSON.stringify(res.request["cookies"]["key2"]))) }
if (res.request["headers"]["User-Agent"][0] != "TestUserAgent") { throw new Error("wrong http request headers: " + JSON.stringify(res.request)) }
`))
assert.NoError(t, err)
})
t.Run("NonEmptyBody", func(t *testing.T) {
t.Parallel()
ts := newTestCase(t)
assert.NoError(t, err)
})
t.Run("NonEmptyBody", func(t *testing.T) {
t.Parallel()
ts := newTestCase(t)

sr := func(input string) string {
return ts.tb.Replacer.Replace(wrapInAsyncLambda(input))
}
_, err := ts.runtime.VU.Runtime().RunString(sr(`
var res = await http.asyncRequest("HTTPBIN_URL/post", {a: "a", b: 2}, {headers: {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"}});
sr := ts.tb.Replacer.Replace
err := runOnEventLoop(ts.runtime, sr(`
var res = await http.asyncRequest("POST", "HTTPBIN_URL/post", {a: "a", b: 2}, {headers: {"Content-Type": "application/x-www-form-urlencoded; charset=utf-8"}});
if (res.status != 200) { throw new Error("wrong status: " + res.status); }
if (res.request["body"] != "a=a&b=2") { throw new Error("http request body was not set properly: " + JSON.stringify(res.request))}
`))
assert.NoError(t, err)
})
assert.NoError(t, err)
})
t.Run("Concurrent", func(t *testing.T) {
t.Parallel()
ts := newTestCase(t)
sr := ts.tb.Replacer.Replace
err := runOnEventLoop(ts.runtime, sr(`
let start = Date.now()
let p1 = http.asyncRequest("GET", "HTTPBIN_URL/delay/200ms").then(() => { return Date.now() - start})
let p2 = http.asyncRequest("GET", "HTTPBIN_URL/delay/100ms").then(() => { return Date.now() - start})
let time1 = await p1;
let time2 = await p2;
if (time1 < time2) {
throw("request that should've taken 200ms took less time then one that should take 100ms " + time1 +">" + time2 )
}
`))
assert.NoError(t, err)
})
}

Expand Down Expand Up @@ -183,10 +194,8 @@ func TestAsyncRequestErrors(t *testing.T) {
defer hook.Reset()

js := `
(async function(){
var r = await http.asyncRequest("GET", "https:// test.k6.io");
globalThis.ret = {error: r.error, error_code: r.error_code};
})()
var r = await http.asyncRequest("GET", "https:// test.k6.io");
globalThis.ret = {error: r.error, error_code: r.error_code};
`
err := runOnEventLoop(ts.runtime, js)
require.NoError(t, err)
Expand Down Expand Up @@ -220,12 +229,10 @@ func TestAsyncRequestErrors(t *testing.T) {
defer hook.Reset()

js := `
(async function(){
var r = await http.asyncRequest("GET", "https:// test.k6.io");
r.html();
r.json();
globalThis.ret = r.error_code; // not reached because of json()
})()
var r = await http.asyncRequest("GET", "https:// test.k6.io");
r.html();
r.json();
globalThis.ret = r.error_code; // not reached because of json()
`
err := runOnEventLoop(ts.runtime, js)
ret := rt.GlobalObject().Get("ret")
Expand Down

0 comments on commit e074510

Please sign in to comment.