From e0745104f3404120d09710f4852776cf20c622c5 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Fri, 27 Jan 2023 11:28:50 +0200 Subject: [PATCH] Add conncurrent test and a bunch of fixes --- js/modules/k6/http/async_request_test.go | 71 +++++++++++++----------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/js/modules/k6/http/async_request_test.go b/js/modules/k6/http/async_request_test.go index 284ee60d3afe..352b3260c49a 100644 --- a/js/modules/k6/http/async_request_test.go +++ b/js/modules/k6/http/async_request_test.go @@ -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 @@ -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); } @@ -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) }) } @@ -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) @@ -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")