Skip to content

Commit

Permalink
Add lib.RuntimeState and use it to create js.Runner
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 8, 2022
1 parent 8eb01ee commit bed3354
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 168 deletions.
9 changes: 5 additions & 4 deletions api/v1/setup_teardown_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ func TestSetupData(t *testing.T) {
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)

runner, err := js.New(
logger,
&lib.RuntimeState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: testCase.script},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)
runner.SetOptions(lib.Options{
Expand Down
9 changes: 5 additions & 4 deletions cmd/integration_tests/eventloop/eventloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ func eventLoopTest(t *testing.T, script []byte, testHandle func(context.Context,
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
logger,
&lib.RuntimeState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: script,
},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down
12 changes: 8 additions & 4 deletions cmd/test_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,16 @@ func (lt *loadedTest) initializeFirstRunner(gs *globalState) error {
testType = detectTestType(lt.source.Data)
}

state := &lib.RuntimeState{
Logger: gs.logger,
RuntimeOptions: lt.runtimeOptions,
BuiltinMetrics: lt.builtInMetrics,
Registry: lt.metricsRegistry,
}
switch testType {
case testTypeJS:
logger.Debug("Trying to load as a JS test...")
runner, err := js.New(
gs.logger, lt.source, lt.fileSystems, lt.runtimeOptions, lt.builtInMetrics, lt.metricsRegistry,
)
runner, err := js.New(state, lt.source, lt.fileSystems)
// TODO: should we use common.UnwrapGojaInterruptedError() here?
if err != nil {
return fmt.Errorf("could not load JS test '%s': %w", testPath, err)
Expand All @@ -126,7 +130,7 @@ func (lt *loadedTest) initializeFirstRunner(gs *globalState) error {
switch arc.Type {
case testTypeJS:
logger.Debug("Evaluating JS from archive bundle...")
lt.initRunner, err = js.NewFromArchive(gs.logger, arc, lt.runtimeOptions, lt.builtInMetrics, lt.metricsRegistry)
lt.initRunner, err = js.NewFromArchive(state, arc)
if err != nil {
return fmt.Errorf("could not load JS from test archive bundle '%s': %w", testPath, err)
}
Expand Down
13 changes: 9 additions & 4 deletions converter/har/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ func TestBuildK6RequestObject(t *testing.T) {
assert.NoError(t, err)
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
_, err = js.New(testutils.NewLogger(t), &loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: []byte(fmt.Sprintf("export default function() { res = http.batch([%v]); }", v)),
}, nil, lib.RuntimeOptions{}, builtinMetrics, registry)
_, err = js.New(
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, &loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: []byte(fmt.Sprintf("export default function() { res = http.batch([%v]); }", v)),
}, nil)
assert.NoError(t, err)
}

Expand Down
82 changes: 48 additions & 34 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,13 @@ func TestSentReceivedMetrics(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r, err := js.New(
testutils.NewLogger(t),
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: []byte(ts.Code)},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -690,12 +691,13 @@ func TestRunTags(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r, err := js.New(
testutils.NewLogger(t),
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: script},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -785,12 +787,13 @@ func TestSetupTeardownThresholds(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
testutils.NewLogger(t),
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: script},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -838,12 +841,13 @@ func TestSetupException(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
testutils.NewLogger(t),
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Scheme: "file", Path: "/script.js"}, Data: script},
map[string]afero.Fs{"file": memfs},
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -891,11 +895,13 @@ func TestVuInitException(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
logger,
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Scheme: "file", Path: "/script.js"}, Data: script},
nil, lib.RuntimeOptions{},
builtinMetrics,
registry,
nil,
)
require.NoError(t, err)

Expand Down Expand Up @@ -962,12 +968,13 @@ func TestEmittedMetricsWhenScalingDown(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
testutils.NewLogger(t),
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: script},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -1050,7 +1057,11 @@ func TestMetricsEmission(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
testutils.NewLogger(t),
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: []byte(fmt.Sprintf(`
import { sleep } from "k6";
import { Counter } from "k6/metrics";
Expand All @@ -1074,9 +1085,6 @@ func TestMetricsEmission(t *testing.T) {
}
`, tc.minIterDuration, tc.defaultBody))},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -1162,12 +1170,13 @@ func TestMinIterationDurationInSetupTeardownStage(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
testutils.NewLogger(t),
&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: []byte(tc.script)},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -1278,8 +1287,13 @@ func TestActiveVUsCount(t *testing.T) {

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(logger, &loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: script}, nil, rtOpts,
builtinMetrics, registry)
runner, err := js.New(
&lib.RuntimeState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
RuntimeOptions: rtOpts,
}, &loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: script}, nil)
require.NoError(t, err)

mockOutput := mockoutput.New()
Expand Down
41 changes: 25 additions & 16 deletions core/local/k6execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,16 @@ func TestExecutionInfoVUSharing(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
logger,
&lib.RuntimeState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: script,
},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -200,15 +201,16 @@ func TestExecutionInfoScenarioIter(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
logger,
&lib.RuntimeState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: script,
},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -281,15 +283,16 @@ func TestSharedIterationsStable(t *testing.T) {
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
logger,
&lib.RuntimeState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: script,
},
nil,
lib.RuntimeOptions{},
builtinMetrics,
registry,
)
require.NoError(t, err)

Expand Down Expand Up @@ -414,10 +417,16 @@ func TestExecutionInfoAll(t *testing.T) {

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(logger, &loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: []byte(tc.script),
}, nil, lib.RuntimeOptions{}, builtinMetrics, registry)
runner, err := js.New(
&lib.RuntimeState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
},
&loader.SourceData{
URL: &url.URL{Path: "/script.js"},
Data: []byte(tc.script),
}, nil)
require.NoError(t, err)

ctx, cancel, execScheduler, samples := newTestExecutionScheduler(t, runner, logger, lib.Options{})
Expand Down
Loading

0 comments on commit bed3354

Please sign in to comment.