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

js/modules: Remove common utils dependency #2376

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 44 additions & 28 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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why these were here? 😕 Seems they were here from almost the start (274eef8), but they don't seem necessary, so 👍 for removing them

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 @@ -209,9 +209,7 @@ func TestGroup(t *testing.T) {
})
}

func checkTestRuntime(t testing.TB, ctxs ...*context.Context) (
*goja.Runtime, chan stats.SampleContainer, *metrics.BuiltinMetrics,
) {
func checkTestRuntime(t testing.TB) (*goja.Runtime, chan stats.SampleContainer, *metrics.BuiltinMetrics) {
rt := goja.New()

root, err := lib.NewGroup("", nil)
Expand All @@ -227,26 +225,19 @@ func checkTestRuntime(t testing.TB, ctxs ...*context.Context) (
"group": root.Path,
}),
}
ctx := context.Background()
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{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: ctx,
CtxField: context.Background(),
StateField: state,
},
).(*K6)
require.True(t, ok)
require.NoError(t, rt.Set("k6", m.Exports().Named))
if len(ctxs) == 1 { // hacks
*ctxs[0] = ctx
}

return rt, samples, state.BuiltinMetrics
}

Expand Down Expand Up @@ -425,9 +416,35 @@ func TestCheckTypes(t *testing.T) {

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

rt := goja.New()
ctx, cancel := context.WithCancel(context.Background())
rt, _, _ := checkTestRuntime(t, &ctx)
root := lib.GetState(ctx).Group
root, err := lib.NewGroup("", nil)
require.NoError(t, err)

samples := make(chan stats.SampleContainer, 1000)
state := &lib.State{
Group: root,
Options: lib.Options{
SystemTags: &stats.DefaultSystemTagSet,
},
Samples: samples,
Tags: lib.NewTagMap(map[string]string{
"group": root.Path,
}),
}

state.BuiltinMetrics = metrics.RegisterBuiltinMetrics(metrics.NewRegistry())
m, ok := New().NewModuleInstance(
&modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: ctx,
StateField: state,
},
).(*K6)
require.True(t, ok)
require.NoError(t, rt.Set("k6", m.Exports().Named))

v, err := rt.RunString(`k6.check(null, { "check": true })`)
if assert.NoError(t, err) {
Expand All @@ -441,9 +458,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