Skip to content

Commit

Permalink
js/modules: Remove all calls for common utils
Browse files Browse the repository at this point in the history
Something was missed from the module migration to the new `modules.Module` API,
where the goja.Runtime and lib.State are now directly accessible without getting them from the context.
  • Loading branch information
codebien committed Feb 4, 2022
1 parent 9c58b15 commit 0e1a7ac
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 100 deletions.
29 changes: 5 additions & 24 deletions js/modules/k6/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions js/modules/k6/crypto/x509/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions js/modules/k6/data/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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";});`)
Expand Down
3 changes: 2 additions & 1 deletion js/modules/k6/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

func TestEncodingAlgorithms(t *testing.T) {
t.Parallel()

if testing.Short() {
return
}
Expand All @@ -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,
Expand Down
17 changes: 10 additions & 7 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
31 changes: 15 additions & 16 deletions js/modules/k6/k6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package k6
import (
"context"
"fmt"
"runtime"
"testing"
"time"

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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!!")
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 0e1a7ac

Please sign in to comment.