Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP requests in the init context #284

Closed
rafaelcapucho opened this issue Jul 16, 2017 · 3 comments
Closed

HTTP requests in the init context #284

rafaelcapucho opened this issue Jul 16, 2017 · 3 comments

Comments

@rafaelcapucho
Copy link

rafaelcapucho commented Jul 16, 2017

Hi, I noted that I'm unable to call functions outside of the export default function, as the following example:

import http from "k6/http";
import { check, fail } from "k6";

const host = __ENV.HOST || "django:8000";
const headers = {
  "Content-Type": "application/json",
  "Authorization": 'Bearer *******',
};

function get_organizations_id(){
    var ids = new Array();
    let response_get = http.get(`http://${host}/api/1.0/organizations/`, {headers: headers});
    response_get.json().results.forEach((x, i) => {
      ids.push(x.id);
    });
    return ids;
}

let organization_ids = get_organizations_id();   // <---- It produces errors

export default function() {
    console.log(get_organizations_id()); // <----- It works as expected
};

I left two comments pointing the function calls.
The error that it produces when I call it outside of default function is like:

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: Panic at 19: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: Panic at 19: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x88 pc=0x8cb2e0]

goroutine 1 [running]:
panic(0xa04e80, 0xc4206769e0)
	/usr/local/go/src/runtime/panic.go:500 +0x1a1
github.com/dop251/goja.(*Runtime).RunProgram.func1(0xc420460308)
	/go/src/github.com/dop251/goja/runtime.go:795 +0xa4
panic(0xa04e80, 0xc4206769e0)
	/usr/local/go/src/runtime/panic.go:458 +0x243
github.com/dop251/goja.(*vm).try.func1(0xc4201761a0, 0x0, 0xc4204601e0, 0x0, 0x0, 0x0, 0xc420460238)
	/go/src/github.com/dop251/goja/vm.go:364 +0x400
panic(0xa14860, 0xc420012080)
	/usr/local/go/src/runtime/panic.go:458 +0x243
github.com/loadimpact/k6/js/modules/k6/http.(*HTTP).request(0xf441c8, 0xdb98c0, 0xc422fa00c0, 0xc420785500, 0x0, 0xabca46, 0x3, 0xdbdec0, 0xc4206768f0, 0xc4215577c0, ...)
	/go/src/github.com/loadimpact/k6/js/modules/k6/http/http.go:130 +0x210
github.com/loadimpact/k6/js/modules/k6/http.(*HTTP).Request(0xf441c8, 0xdb98c0, 0xc422fa00c0, 0xabca46, 0x3, 0xdbdec0, 0xc4206768f0, 0xc4215577c0, 0x2, 0x2, ...)
	/go/src/github.com/loadimpact/k6/js/modules/k6/http/http.go:256 +0x11d
github.com/loadimpact/k6/js/modules/k6/http.(*HTTP).Get(0xf441c8, 0xdb98c0, 0xc422fa00c0, 0xdbdec0, 0xc4206768f0, 0xc420676980, 0x1, 0x1, 0x0, 0x0, ...)
	/go/src/github.com/loadimpact/k6/js/modules/k6/http/http.go:264 +0x17e
reflect.Value.call(0xa74ac0, 0xf441c8, 0xa13, 0xac24af, 0x9, 0xc420262dc0, 0x3, 0x3, 0xb1458d, 0xa428a0, ...)
	/usr/local/go/src/reflect/value.go:434 +0x5c8
reflect.Value.CallSlice(0xa74ac0, 0xf441c8, 0xa13, 0xc420262dc0, 0x3, 0x3, 0x2, 0x2, 0x28)
	/usr/local/go/src/reflect/value.go:315 +0xa4
github.com/loadimpact/k6/js/common.Bind.func1(0xc420849a10, 0x2, 0x2, 0xc420849a10, 0x0, 0x2)
	/go/src/github.com/loadimpact/k6/js/common/bridge.go:170 +0x230
reflect.callReflect(0xc421556560, 0xc42045f9e8)
	/usr/local/go/src/reflect/value.go:506 +0x39e
reflect.makeFuncStub(0xdbdec0, 0xc4206768f0, 0xc420676980, 0x1, 0x1, 0x0, 0xc4208499f0, 0xc421557680, 0xc42045fd78, 0x61d1a8, ...)
	/usr/local/go/src/reflect/asm_amd64.s:17 +0x40
reflect.Value.call(0xc421a549c0, 0xc421556560, 0x13, 0xabcf17, 0x4, 0xc420849980, 0x2, 0x2, 0x0, 0x0, ...)
	/usr/local/go/src/reflect/value.go:434 +0x5c8
reflect.Value.Call(0xc421a549c0, 0xc421556560, 0x13, 0xc420849980, 0x2, 0x2, 0xc421557620, 0x16, 0x0)
	/usr/local/go/src/reflect/value.go:302 +0xa4
github.com/dop251/goja.(*Runtime).wrapReflectFunc.func1(0xdbdce0, 0xc421556780, 0xc42139ad60, 0x2, 0xa, 0x0, 0x0)
	/go/src/github.com/dop251/goja/runtime.go:1062 +0x859
github.com/dop251/goja.(*vm)._nativeCall(0xc4201761a0, 0xc42023d290, 0x2)
	/go/src/github.com/dop251/goja/vm.go:1827 +0x2d0
github.com/dop251/goja.call.exec(0xc400000002, 0xc4201761a0)
	/go/src/github.com/dop251/goja/vm.go:1808 +0x749
github.com/dop251/goja.(*call).exec(0xc420681ce0, 0xc4201761a0)
	<autogenerated>:816 +0x5a
github.com/dop251/goja.(*vm).run(0xc4201761a0)
	/go/src/github.com/dop251/goja/vm.go:288 +0x92
github.com/dop251/goja.(*vm).(github.com/dop251/goja.run)-fm()
	/go/src/github.com/dop251/goja/vm.go:375 +0x2a
github.com/dop251/goja.(*vm).try(0xc4201761a0, 0xc420460240, 0x0)
	/go/src/github.com/dop251/goja/vm.go:370 +0x143
github.com/dop251/goja.(*vm).runTry(0xc4201761a0, 0xb16f18)
	/go/src/github.com/dop251/goja/vm.go:375 +0x4f
github.com/dop251/goja.(*Runtime).RunProgram(0xc420785500, 0xc421b03c20, 0x0, 0x0, 0x0, 0x0)
	/go/src/github.com/dop251/goja/runtime.go:806 +0x212
github.com/loadimpact/k6/js.(*Bundle).instantiate(0xc420d7a780, 0xc420785500, 0xc4217c8b80, 0x0, 0xc420847e90)
	/go/src/github.com/loadimpact/k6/js/bundle.go:226 +0x529
github.com/loadimpact/k6/js.NewBundle(0xc420e2e1e0, 0xdbd600, 0xf441c8, 0x7f9a59546e10, 0x0, 0xc421f9af00)
	/go/src/github.com/loadimpact/k6/js/bundle.go:99 +0x498
github.com/loadimpact/k6/js.New(0xc420e2e1e0, 0xdbd600, 0xf441c8, 0x2, 0xa6de01, 0x1)
	/go/src/github.com/loadimpact/k6/js/runner.go:51 +0x43
main.makeRunner(0xabc89b, 0x2, 0xc420e2e1e0, 0xdbd600, 0xf441c8, 0xc42002a008, 0xdbd600, 0xf441c8, 0xc420e2e1e0)
	/go/src/github.com/loadimpact/k6/run.go:247 +0x2b4
main.actionRun(0xc4223e1b80, 0x10100, 0xc4223e1b80)
	/go/src/github.com/loadimpact/k6/run.go:404 +0x40d
gopkg.in/urfave/cli%2ev1.HandleAction(0x9f5d40, 0xb174d0, 0xc4223e1b80, 0xc421facd00, 0x0)
	/go/src/gopkg.in/urfave/cli.v1/app.go:485 +0xd4
gopkg.in/urfave/cli%2ev1.Command.Run(0xabccf2, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0, 0xace85f, 0x1a, 0x0, ...)
	/go/src/gopkg.in/urfave/cli.v1/command.go:193 +0xb96
gopkg.in/urfave/cli%2ev1.(*App).Run(0xc4200664e0, 0xc42000c1b0, 0x9, 0x9, 0x0, 0x0)
	/go/src/gopkg.in/urfave/cli.v1/app.go:250 +0x812
main.main()
	/go/src/github.com/loadimpact/k6/main.go:82 +0x5ec

I'd like to execute it from outside because it must execute only once, the tests itself will use it later on.
Thanks!

@robingustafsson
Copy link
Member

@rafaelcapucho HTTP calls aren't allowed in the "init" stage of k6 (in the global scope). This should be clarified in the docs, so thanks for bringing it to our attention. All it says now regarding the "init" vs "VU" code is here: https://docs.k6.io/docs/running-k6#section-adding-more-vus

I believe your use case would be solved by #194. Until that is implemented I'd suggest a workaround like:

...
let organization_ids = null;
export default function() {
    // Get organization IDs on first iteration of each VU
    if (__ITER === 0) {
        organization_ids = get_organizations_id();
    }
    ...
}

The __ITER global variable contains a zero-based number representing the current VU's iteration count. Can be used in combination with __VU (current VU number, one-based) to do even more specific initialization.

@liclac
Copy link
Contributor

liclac commented Jul 31, 2017

Something you can do as well is to open() or import URLs, but that only works with simple GETs - a PR that adds support for passing headers to both (on the Loader level) would be very welcome though.

There should also be a more sensible error message than that; adding an issue for this.

@liclac liclac changed the title I can't call functions outside of Default Function HTTP requests in the init context Dec 10, 2017
@na-- na-- added this to the v1.0.0 milestone Oct 4, 2018
@na--
Copy link
Member

na-- commented Oct 16, 2018

This was also closed by #811

@na-- na-- closed this as completed Oct 16, 2018
@na-- na-- removed this from the v1.0.0 milestone Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants