Skip to content

Commit

Permalink
Switch to new k6 JS module names
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Nov 15, 2021
1 parent e7af0ed commit d4ea55c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion js/modules/k6/http/cookiejar.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (j CookieJar) CookiesForURL(url string) map[string][]string {

// Set sets a cookie for a particular url with the given name value and additional opts
func (j CookieJar) Set(url, name, value string, opts goja.Value) (bool, error) {
rt := j.moduleInstance.GetRuntime()
rt := j.moduleInstance.vu.Runtime()

u, err := neturl.Parse(url)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/http/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (mi *ModuleInstance) File(data interface{}, args ...string) FileData {

dt, err := common.ToBytes(data)
if err != nil {
common.Throw(mi.GetRuntime(), err)
common.Throw(mi.vu.Runtime(), err)
}

return FileData{
Expand Down
22 changes: 11 additions & 11 deletions js/modules/k6/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ type RootModule struct{}

// ModuleInstance represents an instance of the HTTP module for every VU.
type ModuleInstance struct {
modules.InstanceCore
vu modules.VU
rootModule *RootModule
defaultClient *Client
exports *goja.Object
}

var (
_ modules.IsModuleV2 = &RootModule{}
_ modules.Instance = &ModuleInstance{}
_ modules.Module = &RootModule{}
_ modules.Instance = &ModuleInstance{}
)

// New returns a pointer to a new HTTP RootModule.
Expand All @@ -57,12 +57,12 @@ func New() *RootModule {
}

// NewModuleInstancePerVU returns an HTTP module instance for each VU.
func (r *RootModule) NewModuleInstance(ic modules.InstanceCore) modules.Instance {
rt := ic.GetRuntime()
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
rt := vu.Runtime()
mi := &ModuleInstance{
InstanceCore: ic,
rootModule: r,
exports: rt.NewObject(),
vu: vu,
rootModule: r,
exports: rt.NewObject(),
}
mi.defineConstants()

Expand Down Expand Up @@ -110,7 +110,7 @@ func (r *RootModule) NewModuleInstance(ic modules.InstanceCore) modules.Instance
return mi
}

func (mi *ModuleInstance) GetExports() modules.Exports {
func (mi *ModuleInstance) Exports() modules.Exports {
return modules.Exports{
Default: mi.exports,
// TODO: add new HTTP APIs like Client, Request, etc. as named exports?
Expand All @@ -119,7 +119,7 @@ func (mi *ModuleInstance) GetExports() modules.Exports {

// TODO: deprecate these?
func (mi *ModuleInstance) defineConstants() {
rt := mi.GetRuntime()
rt := mi.vu.Runtime()
mustAddProp := func(name, val string) {
err := mi.exports.DefineDataProperty(
name, rt.ToValue(val), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE,
Expand Down Expand Up @@ -158,7 +158,7 @@ func (mi *ModuleInstance) newCookieJar(call goja.ConstructorCall, rt *goja.Runti

// getVUCookieJar returns the active cookie jar for the current VU.
func (mi *ModuleInstance) getVUCookieJar(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
if state := mi.GetState(); state != nil {
if state := mi.vu.State(); state != nil {
return rt.ToValue(&CookieJar{mi, state.CookieJar})
}
common.Throw(rt, ErrJarForbiddenInInitContext)
Expand Down
12 changes: 6 additions & 6 deletions js/modules/k6/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ func getTestModuleInstance(t testing.TB, ctx context.Context, state *lib.State)
}

root := new(RootModule)
mii := &modulestest.InstanceCore{
Runtime: rt,
InitEnv: &common.InitEnvironment{
mii := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{
Registry: metrics.NewRegistry(),
},
Ctx: ctx,
State: state,
CtxField: ctx,
StateField: state,
}
mi, ok := root.NewModuleInstance(mii).(*ModuleInstance)
require.True(t, ok)

rt.Set("http", mi.GetExports().Default)
rt.Set("http", mi.Exports().Default)

return rt, mi
}
Expand Down
16 changes: 8 additions & 8 deletions js/modules/k6/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *Client) getMethodClosure(method string) func(url goja.Value, args ...go
// Request makes an http request of the provided `method` and returns a corresponding response by
// taking goja.Values as arguments
func (c *Client) Request(method string, url goja.Value, args ...goja.Value) (*Response, error) {
state := c.moduleInstance.GetState()
state := c.moduleInstance.vu.State()
if state == nil {
return nil, ErrHTTPForbiddenInInitContext
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (c *Client) Request(method string, url goja.Value, args ...goja.Value) (*Re
return &Response{Response: r}, nil
}

resp, err := httpext.MakeRequest(c.moduleInstance.GetContext(), state, req)
resp, err := httpext.MakeRequest(c.moduleInstance.vu.Context(), state, req)
if err != nil {
return nil, err
}
Expand All @@ -98,7 +98,7 @@ func (c *Client) Request(method string, url goja.Value, args ...goja.Value) (*Re
// a reverse dependency on js/common or goja.
func (c *Client) processResponse(resp *httpext.Response, respType httpext.ResponseType) {
if respType == httpext.ResponseTypeBinary && resp.Body != nil {
resp.Body = c.moduleInstance.GetRuntime().NewArrayBuffer(resp.Body.([]byte))
resp.Body = c.moduleInstance.vu.Runtime().NewArrayBuffer(resp.Body.([]byte))
}
}

Expand All @@ -111,8 +111,8 @@ func (c *Client) responseFromHttpext(resp *httpext.Response) *Response {
func (c *Client) parseRequest(
method string, reqURL, body interface{}, params goja.Value,
) (*httpext.ParsedHTTPRequest, error) {
rt := c.moduleInstance.GetRuntime()
state := c.moduleInstance.GetState()
rt := c.moduleInstance.vu.Runtime()
state := c.moduleInstance.vu.State()
if state == nil {
return nil, ErrHTTPForbiddenInInitContext
}
Expand Down Expand Up @@ -438,7 +438,7 @@ func (c *Client) prepareBatchObject(requests map[string]interface{}) (
// Batch makes multiple simultaneous HTTP requests. The provideds reqsV should be an array of request
// objects. Batch returns an array of responses and/or error
func (c *Client) Batch(reqsV goja.Value) (interface{}, error) {
state := c.moduleInstance.GetState()
state := c.moduleInstance.vu.State()
if state == nil {
return nil, ErrBatchForbiddenInInitContext
}
Expand Down Expand Up @@ -468,7 +468,7 @@ func (c *Client) Batch(reqsV goja.Value) (interface{}, error) {

reqCount := len(batchReqs)
errs := httpext.MakeBatchRequests(
c.moduleInstance.GetContext(), state, batchReqs, reqCount,
c.moduleInstance.vu.Context(), state, batchReqs, reqCount,
int(state.Options.Batch.Int64), int(state.Options.BatchPerHost.Int64),
c.processResponse,
)
Expand All @@ -487,7 +487,7 @@ func (c *Client) parseBatchRequest(key interface{}, val interface{}) (*httpext.P
ok bool
body, reqURL interface{}
params goja.Value
rt = c.moduleInstance.GetRuntime()
rt = c.moduleInstance.vu.Runtime()
)

switch data := val.(type) {
Expand Down
10 changes: 5 additions & 5 deletions js/modules/k6/http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (j jsonError) Error() string {

// HTML returns the body as an html.Selection
func (res *Response) HTML(selector ...string) html.Selection {
rt := res.client.moduleInstance.GetRuntime()
rt := res.client.moduleInstance.vu.Runtime()
if res.Body == nil {
err := fmt.Errorf("the body is null so we can't transform it to HTML" +
" - this likely was because of a request error getting the response")
Expand All @@ -70,7 +70,7 @@ func (res *Response) HTML(selector ...string) html.Selection {
common.Throw(rt, err)
}

sel, err := html.HTML{}.ParseHTML(res.client.moduleInstance.GetContext(), body)
sel, err := html.HTML{}.ParseHTML(res.client.moduleInstance.vu.Context(), body)
if err != nil {
common.Throw(rt, err)
}
Expand All @@ -83,7 +83,7 @@ func (res *Response) HTML(selector ...string) html.Selection {

// JSON parses the body of a response as JSON and returns it to the goja VM.
func (res *Response) JSON(selector ...string) goja.Value {
rt := res.client.moduleInstance.GetRuntime()
rt := res.client.moduleInstance.vu.Runtime()

if res.Body == nil {
err := fmt.Errorf("the body is null so we can't transform it to JSON" +
Expand Down Expand Up @@ -155,7 +155,7 @@ func checkErrorInJSON(input []byte, offset int, err error) error {
// SubmitForm parses the body as an html looking for a from and then submitting it
// TODO: document the actual arguments that can be provided
func (res *Response) SubmitForm(args ...goja.Value) (*Response, error) {
rt := res.client.moduleInstance.GetRuntime()
rt := res.client.moduleInstance.vu.Runtime()

formSelector := "form"
submitSelector := "[type=\"submit\"]"
Expand Down Expand Up @@ -244,7 +244,7 @@ func (res *Response) SubmitForm(args ...goja.Value) (*Response, error) {
// ClickLink parses the body as an html, looks for a link and than makes a request as if the link was
// clicked
func (res *Response) ClickLink(args ...goja.Value) (*Response, error) {
rt := res.client.moduleInstance.GetRuntime()
rt := res.client.moduleInstance.vu.Runtime()

selector := "a[href]"
requestParams := goja.Null()
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/http/response_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (e expectedStatuses) match(status int) bool {
// The arguments must be either integers or object of `{min: <integer>, max: <integer>}`
// kind. The "integer"ness is checked by the Number.isInteger.
func (mi *ModuleInstance) expectedStatuses(args ...goja.Value) *expectedStatuses { //nolint: golint
rt := mi.GetRuntime()
rt := mi.vu.Runtime()

if len(args) == 0 {
common.Throw(rt, errors.New("no arguments"))
Expand Down Expand Up @@ -108,7 +108,7 @@ func (c *Client) SetResponseCallback(val goja.Value) {
c.responseCallback = es.match
} else {
common.Throw(
c.moduleInstance.GetRuntime(),
c.moduleInstance.vu.Runtime(),
fmt.Errorf("unsupported argument, expected http.expectedStatuses"),
)
}
Expand Down
12 changes: 6 additions & 6 deletions js/modules/k6/ws/ws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1236,13 +1236,13 @@ func TestCookieJar(t *testing.T) {
}
}))

mii := &modulestest.InstanceCore{
Runtime: ts.rt,
InitEnv: &common.InitEnvironment{Registry: metrics.NewRegistry()},
Ctx: context.Background(),
State: ts.state,
mii := &modulestest.VU{
RuntimeField: ts.rt,
InitEnvField: &common.InitEnvironment{Registry: metrics.NewRegistry()},
CtxField: context.Background(),
StateField: ts.state,
}
err := ts.rt.Set("http", httpModule.New().NewModuleInstance(mii).GetExports().Default)
err := ts.rt.Set("http", httpModule.New().NewModuleInstance(mii).Exports().Default)
require.NoError(t, err)
ts.state.CookieJar, _ = cookiejar.New(nil)

Expand Down

0 comments on commit d4ea55c

Please sign in to comment.