diff --git a/js/modules/k6/crypto/crypto_test.go b/js/modules/k6/crypto/crypto_test.go index dbc3ffb5738e..285a0761fe02 100644 --- a/js/modules/k6/crypto/crypto_test.go +++ b/js/modules/k6/crypto/crypto_test.go @@ -49,14 +49,12 @@ func TestCryptoAlgorithms(t *testing.T) { rt := goja.New() rt.SetFieldNameMapper(common.FieldNameMapper{}) - ctx := context.Background() - ctx = common.WithRuntime(ctx, rt) m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: ctx, + CtxField: context.Background(), StateField: nil, }, ).(*Crypto) @@ -201,15 +199,13 @@ func TestStreamingApi(t *testing.T) { state := &lib.State{Group: root} ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, CtxField: ctx, - StateField: nil, + StateField: state, }, ).(*Crypto) require.True(t, ok) @@ -271,18 +267,11 @@ func TestOutputEncoding(t *testing.T) { rt := goja.New() rt.SetFieldNameMapper(common.FieldNameMapper{}) - root, _ := lib.NewGroup("", nil) - state := &lib.State{Group: root} - - ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) - m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: ctx, + CtxField: context.Background(), StateField: nil, }, ).(*Crypto) @@ -359,18 +348,11 @@ func TestHMac(t *testing.T) { rt := goja.New() rt.SetFieldNameMapper(common.FieldNameMapper{}) - root, _ := lib.NewGroup("", nil) - state := &lib.State{Group: root} - - ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) - m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: ctx, + CtxField: context.Background(), StateField: nil, }, ).(*Crypto) @@ -499,13 +481,12 @@ func TestAWSv4(t *testing.T) { // example values from https://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html rt := goja.New() rt.SetFieldNameMapper(common.FieldNameMapper{}) - ctx := common.WithRuntime(context.Background(), rt) m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: ctx, + CtxField: context.Background(), StateField: nil, }, ).(*Crypto) diff --git a/js/modules/k6/crypto/x509/x509_test.go b/js/modules/k6/crypto/x509/x509_test.go index b8d5ef8b19fa..73f96e7e6a4f 100644 --- a/js/modules/k6/crypto/x509/x509_test.go +++ b/js/modules/k6/crypto/x509/x509_test.go @@ -37,12 +37,11 @@ import ( func makeRuntime(t *testing.T) *goja.Runtime { rt := goja.New() rt.SetFieldNameMapper(common.FieldNameMapper{}) - ctx := common.WithRuntime(context.Background(), rt) m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: ctx, + CtxField: context.Background(), StateField: nil, }, ).(*X509) diff --git a/js/modules/k6/data/share_test.go b/js/modules/k6/data/share_test.go index 2229322df0f3..741a41abe8a2 100644 --- a/js/modules/k6/data/share_test.go +++ b/js/modules/k6/data/share_test.go @@ -53,7 +53,7 @@ func newConfiguredRuntime() (*goja.Runtime, error) { &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: common.WithRuntime(context.Background(), rt), + CtxField: context.Background(), StateField: nil, }, ).(*Data) @@ -173,7 +173,7 @@ func TestSharedArrayAnotherRuntimeWorking(t *testing.T) { vu := &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: common.WithRuntime(context.Background(), rt), + CtxField: context.Background(), StateField: nil, } m, ok := New().NewModuleInstance(vu).(*Data) @@ -186,7 +186,7 @@ func TestSharedArrayAnotherRuntimeWorking(t *testing.T) { // create another Runtime with new ctx but keep the initEnv rt = goja.New() vu.RuntimeField = rt - vu.CtxField = common.WithRuntime(context.Background(), rt) + vu.CtxField = context.Background() require.NoError(t, rt.Set("data", m.Exports().Named)) _, err = rt.RunString(`var array = new data.SharedArray("shared", function() {throw "wat";});`) diff --git a/js/modules/k6/encoding/encoding_test.go b/js/modules/k6/encoding/encoding_test.go index 2f2e59060271..1fb1ecbb8cd7 100644 --- a/js/modules/k6/encoding/encoding_test.go +++ b/js/modules/k6/encoding/encoding_test.go @@ -34,6 +34,7 @@ import ( func TestEncodingAlgorithms(t *testing.T) { t.Parallel() + if testing.Short() { return } @@ -42,7 +43,7 @@ func TestEncodingAlgorithms(t *testing.T) { rt.SetFieldNameMapper(common.FieldNameMapper{}) m, ok := New().NewModuleInstance( &modulestest.VU{ - CtxField: common.WithRuntime(context.Background(), rt), + CtxField: context.Background(), RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, StateField: nil, diff --git a/js/modules/k6/execution/execution_test.go b/js/modules/k6/execution/execution_test.go index f6d7ca1dbf25..e07e657e98a7 100644 --- a/js/modules/k6/execution/execution_test.go +++ b/js/modules/k6/execution/execution_test.go @@ -60,9 +60,11 @@ func setupTagsExecEnv(t *testing.T) execEnv { Logger: testLog, } - rt := goja.New() - ctx := common.WithRuntime(context.Background(), rt) - ctx = lib.WithState(ctx, state) + var ( + rt = goja.New() + ctx = context.Background() + ) + m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, @@ -187,10 +189,11 @@ func TestVUTags(t *testing.T) { func TestAbortTest(t *testing.T) { //nolint: tparallel t.Parallel() - rt := goja.New() - ctx := common.WithRuntime(context.Background(), rt) - state := &lib.State{} - ctx = lib.WithState(ctx, state) + var ( + rt = goja.New() + state = &lib.State{} + ctx = context.Background() + ) m, ok := New().NewModuleInstance( &modulestest.VU{ diff --git a/js/modules/k6/k6_test.go b/js/modules/k6/k6_test.go index 6e541d9033f8..b4bd5caa1291 100644 --- a/js/modules/k6/k6_test.go +++ b/js/modules/k6/k6_test.go @@ -23,7 +23,6 @@ package k6 import ( "context" "fmt" - "runtime" "testing" "time" @@ -45,7 +44,7 @@ func TestFail(t *testing.T) { &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: common.WithRuntime(context.Background(), rt), + CtxField: context.Background(), StateField: nil, }, ).(*K6) @@ -73,7 +72,7 @@ func TestSleep(t *testing.T) { &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: common.WithRuntime(context.Background(), rt), + CtxField: context.Background(), StateField: nil, }, ).(*K6) @@ -90,13 +89,14 @@ func TestSleep(t *testing.T) { t.Run("Cancel", func(t *testing.T) { t.Parallel() + rt := goja.New() ctx, cancel := context.WithCancel(context.Background()) m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: common.WithRuntime(ctx, rt), + CtxField: ctx, StateField: nil, }, ).(*K6) @@ -111,12 +111,11 @@ func TestSleep(t *testing.T) { assert.NoError(t, err) dch <- endTime.Sub(startTime) }() - runtime.Gosched() + time.Sleep(1 * time.Second) - runtime.Gosched() cancel() - runtime.Gosched() d := <-dch + assert.True(t, d > 500*time.Millisecond, "did not sleep long enough") assert.True(t, d < 2*time.Second, "slept for too long!!") }) @@ -130,7 +129,7 @@ func TestRandSeed(t *testing.T) { &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: common.WithRuntime(context.Background(), rt), + CtxField: context.Background(), StateField: nil, }, ).(*K6) @@ -154,6 +153,7 @@ func TestRandSeed(t *testing.T) { func TestGroup(t *testing.T) { t.Parallel() + setupGroupTest := func() (*goja.Runtime, *lib.State, *lib.Group) { root, err := lib.NewGroup("", nil) assert.NoError(t, err) @@ -167,13 +167,13 @@ func TestGroup(t *testing.T) { SystemTags: stats.NewSystemTagSet(stats.TagGroup), }, } - ctx := lib.WithState(context.Background(), state) state.BuiltinMetrics = metrics.RegisterBuiltinMetrics(metrics.NewRegistry()) + m, ok := New().NewModuleInstance( &modulestest.VU{ RuntimeField: rt, InitEnvField: &common.InitEnvironment{}, - CtxField: common.WithRuntime(ctx, rt), + CtxField: context.Background(), StateField: state, }, ).(*K6) @@ -231,8 +231,6 @@ func checkTestRuntime(t testing.TB, ctxs ...*context.Context) ( if len(ctxs) == 1 { // hacks ctx = *ctxs[0] } - ctx = common.WithRuntime(ctx, rt) - ctx = lib.WithState(ctx, state) state.BuiltinMetrics = metrics.RegisterBuiltinMetrics(metrics.NewRegistry()) m, ok := New().NewModuleInstance( &modulestest.VU{ @@ -425,9 +423,11 @@ func TestCheckTypes(t *testing.T) { func TestCheckContextExpiry(t *testing.T) { t.Parallel() + ctx, cancel := context.WithCancel(context.Background()) rt, _, _ := checkTestRuntime(t, &ctx) - root := lib.GetState(ctx).Group + root, err := lib.NewGroup("", nil) + require.NoError(t, err) v, err := rt.RunString(`k6.check(null, { "check": true })`) if assert.NoError(t, err) { @@ -441,9 +441,8 @@ func TestCheckContextExpiry(t *testing.T) { cancel() v, err = rt.RunString(`k6.check(null, { "check": true })`) - if assert.NoError(t, err) { - assert.Equal(t, true, v.Export()) - } + require.NoError(t, err) + assert.Equal(t, true, v.Export()) assert.Equal(t, int64(1), check.Passes) assert.Equal(t, int64(0), check.Fails) diff --git a/js/modules/k6/ws/ws.go b/js/modules/k6/ws/ws.go index a1d95331285c..944f79b61841 100644 --- a/js/modules/k6/ws/ws.go +++ b/js/modules/k6/ws/ws.go @@ -85,6 +85,7 @@ func (*RootModule) NewModuleInstance(m modules.VU) modules.Instance { var ErrWSInInitContext = common.NewInitContextError("using websockets in the init context is not supported") type Socket struct { + rt *goja.Runtime ctx context.Context conn *websocket.Conn eventHandlers map[string][]goja.Callable @@ -263,6 +264,7 @@ func (mi *WS) Connect(url string, args ...goja.Value) (*WSHTTPResponse, error) { socket := Socket{ ctx: ctx, + rt: rt, conn: conn, eventHandlers: make(map[string][]goja.Callable), pingSendTimestamps: make(map[string]time.Time), @@ -407,7 +409,7 @@ func (s *Socket) handleEvent(event string, args ...goja.Value) { if handlers, ok := s.eventHandlers[event]; ok { for _, handler := range handlers { if _, err := handler(goja.Undefined(), args...); err != nil { - common.Throw(common.GetRuntime(s.ctx), err) + common.Throw(s.rt, err) } } } @@ -416,7 +418,7 @@ func (s *Socket) handleEvent(event string, args ...goja.Value) { // Send writes the given string message to the connection. func (s *Socket) Send(message string) { if err := s.conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil { - s.handleEvent("error", common.GetRuntime(s.ctx).ToValue(err)) + s.handleEvent("error", s.rt.ToValue(err)) } stats.PushIfNotDone(s.ctx, s.samplesOutput, stats.Sample{ @@ -430,24 +432,23 @@ func (s *Socket) Send(message string) { // SendBinary writes the given ArrayBuffer message to the connection. func (s *Socket) SendBinary(message goja.Value) { if message == nil { - common.Throw(common.GetRuntime(s.ctx), errors.New("missing argument, expected ArrayBuffer")) + common.Throw(s.rt, errors.New("missing argument, expected ArrayBuffer")) } msg := message.Export() if ab, ok := msg.(goja.ArrayBuffer); ok { if err := s.conn.WriteMessage(websocket.BinaryMessage, ab.Bytes()); err != nil { - s.handleEvent("error", common.GetRuntime(s.ctx).ToValue(err)) + s.handleEvent("error", s.rt.ToValue(err)) } } else { - rt := common.GetRuntime(s.ctx) var jsType string switch { case goja.IsNull(message), goja.IsUndefined(message): jsType = message.String() default: - jsType = message.ToObject(rt).ClassName() + jsType = message.ToObject(s.rt).ClassName() } - common.Throw(rt, fmt.Errorf("expected ArrayBuffer as argument, received: %s", jsType)) + common.Throw(s.rt, fmt.Errorf("expected ArrayBuffer as argument, received: %s", jsType)) } stats.PushIfNotDone(s.ctx, s.samplesOutput, stats.Sample{ @@ -459,14 +460,13 @@ func (s *Socket) SendBinary(message goja.Value) { } func (s *Socket) Ping() { - rt := common.GetRuntime(s.ctx) deadline := time.Now().Add(writeWait) pingID := strconv.Itoa(s.pingSendCounter) data := []byte(pingID) err := s.conn.WriteControl(websocket.PingMessage, data, deadline) if err != nil { - s.handleEvent("error", rt.ToValue(err)) + s.handleEvent("error", s.rt.ToValue(err)) return } @@ -578,19 +578,17 @@ func (s *Socket) closeConnection(code int) error { // Stop the main control loop close(s.done) }() - rt := common.GetRuntime(s.ctx) - err = s.conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(code, ""), time.Now().Add(writeWait), ) if err != nil { // Call the user-defined error handler - s.handleEvent("error", rt.ToValue(err)) + s.handleEvent("error", s.rt.ToValue(err)) } // Call the user-defined close handler - s.handleEvent("close", rt.ToValue(code)) + s.handleEvent("close", s.rt.ToValue(code)) }) return err diff --git a/js/modules/k6/ws/ws_test.go b/js/modules/k6/ws/ws_test.go index a2f15785ebca..7e3a259de3bc 100644 --- a/js/modules/k6/ws/ws_test.go +++ b/js/modules/k6/ws/ws_test.go @@ -132,11 +132,8 @@ func newTestState(t testing.TB) testState { Tags: lib.NewTagMap(nil), } - ctx := lib.WithState(tb.Context, state) - ctx = common.WithRuntime(ctx, rt) - m := New().NewModuleInstance(&modulestest.VU{ - CtxField: ctx, + CtxField: tb.Context, InitEnvField: &common.InitEnvironment{}, RuntimeField: rt, StateField: state, @@ -144,7 +141,6 @@ func newTestState(t testing.TB) testState { require.NoError(t, rt.Set("ws", m.Exports().Default)) return testState{ - ctxPtr: &ctx, rt: rt, tb: tb, state: state, @@ -181,12 +177,8 @@ func TestSession(t *testing.T) { Tags: lib.NewTagMap(nil), } - ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) - m := New().NewModuleInstance(&modulestest.VU{ - CtxField: ctx, + CtxField: context.Background(), InitEnvField: &common.InitEnvironment{}, RuntimeField: rt, StateField: state, @@ -579,12 +571,8 @@ func TestSocketSendBinary(t *testing.T) { //nolint: tparallel Tags: lib.NewTagMap(nil), } - ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) - m := New().NewModuleInstance(&modulestest.VU{ - CtxField: ctx, + CtxField: context.Background(), InitEnvField: &common.InitEnvironment{}, RuntimeField: rt, StateField: state, @@ -675,12 +663,8 @@ func TestErrors(t *testing.T) { Tags: lib.NewTagMap(nil), } - ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) - m := New().NewModuleInstance(&modulestest.VU{ - CtxField: ctx, + CtxField: context.Background(), InitEnvField: &common.InitEnvironment{}, RuntimeField: rt, StateField: state, @@ -794,12 +778,8 @@ func TestSystemTags(t *testing.T) { Tags: lib.NewTagMap(nil), } - ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) - m := New().NewModuleInstance(&modulestest.VU{ - CtxField: ctx, + CtxField: context.Background(), InitEnvField: &common.InitEnvironment{}, RuntimeField: rt, StateField: state, @@ -864,12 +844,8 @@ func TestTLSConfig(t *testing.T) { Tags: lib.NewTagMap(nil), } - ctx := context.Background() - ctx = lib.WithState(ctx, state) - ctx = common.WithRuntime(ctx, rt) - m := New().NewModuleInstance(&modulestest.VU{ - CtxField: ctx, + CtxField: context.Background(), InitEnvField: &common.InitEnvironment{}, RuntimeField: rt, StateField: state, @@ -1010,11 +986,8 @@ func TestUserAgent(t *testing.T) { Tags: lib.NewTagMap(nil), } - ctx := lib.WithState(context.Background(), state) - ctx = common.WithRuntime(ctx, rt) - m := New().NewModuleInstance(&modulestest.VU{ - CtxField: ctx, + CtxField: context.Background(), InitEnvField: &common.InitEnvironment{}, RuntimeField: rt, StateField: state,