Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 1a76fe1

Browse files
authored
Use moveToContext only when needed in tests (#525)
1 parent aaae86c commit 1a76fe1

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

common/network_manager_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func newTestNetworkManager(t *testing.T, k6opts k6lib.Options) (*NetworkManager,
5757
}, nil)
5858

5959
vu := k6test.NewVU(t)
60+
vu.MoveToVUContext()
6061
st := vu.State()
6162
st.Options = k6opts
6263
logger := log.New(st.Logger, false, nil)

k6ext/k6test/vu.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import (
2626
// RunLoop() override to call WaitOnRegistered()?
2727
type VU struct {
2828
*k6modulestest.VU
29-
Loop *k6eventloop.EventLoop
29+
Loop *k6eventloop.EventLoop
30+
toBeState *k6lib.State
3031
}
3132

3233
// ToGojaValue is a convenience method for converting any value to a goja value.
@@ -38,6 +39,13 @@ func (v *VU) RunLoop(fn func() error) error {
3839
return v.Loop.Start(fn)
3940
}
4041

42+
// MoveToVUContext moves the VU to VU context, adding a predefined k6 lib State and nilling the InitEnv
43+
// to simulate how that is done in the real k6.
44+
func (v *VU) MoveToVUContext() {
45+
v.VU.StateField = v.toBeState
46+
v.VU.InitEnvField = nil
47+
}
48+
4149
// NewVU returns a mock k6 VU.
4250
func NewVU(tb testing.TB) *VU {
4351
tb.Helper()
@@ -71,7 +79,6 @@ func NewVU(tb testing.TB) *VU {
7179
testRT := k6modulestest.NewRuntime(tb)
7280
ctx := k6ext.WithVU(testRT.VU.CtxField, testRT.VU)
7381
testRT.VU.CtxField = ctx
74-
testRT.MoveToVUContext(state)
7582

76-
return &VU{VU: testRT.VU, Loop: testRT.EventLoop}
83+
return &VU{VU: testRT.VU, Loop: testRT.EventLoop, toBeState: state}
7784
}

tests/test_browser.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/grafana/xk6-browser/k6ext"
3434
"github.com/grafana/xk6-browser/k6ext/k6test"
3535

36-
k6common "go.k6.io/k6/js/common"
3736
k6http "go.k6.io/k6/js/modules/k6/http"
3837
k6httpmultibin "go.k6.io/k6/lib/testutils/httpmultibin"
3938
k6metrics "go.k6.io/k6/metrics"
@@ -105,43 +104,28 @@ func newTestBrowser(tb testing.TB, opts ...interface{}) *testBrowser {
105104
k6m := k6ext.RegisterCustomMetrics(registry)
106105
vu.CtxField = k6ext.WithCustomMetrics(vu.Context(), k6m)
107106

107+
v := chromium.NewBrowserType(vu)
108+
bt, ok := v.(*chromium.BrowserType)
109+
if !ok {
110+
panic(fmt.Errorf("testBrowser: unexpected browser type %T", v))
111+
}
112+
vu.MoveToVUContext()
113+
// enable the HTTP test server only when necessary
108114
var (
109-
state = vu.StateField
110-
rt = vu.RuntimeField
115+
testServer *k6httpmultibin.HTTPMultiBin
116+
state = vu.StateField
117+
rt = vu.RuntimeField
118+
lc *logCache
111119
)
112120

113-
// enable the HTTP test server only when necessary
114-
var testServer *k6httpmultibin.HTTPMultiBin
121+
if enableLogCache {
122+
lc = attachLogCache(state.Logger)
123+
}
115124
if enableHTTPMultiBin {
116125
testServer = k6httpmultibin.NewHTTPMultiBin(tb)
117126
state.TLSConfig = testServer.TLSClientConfig
118127
state.Transport = testServer.HTTPTransport
119128
}
120-
121-
var lc *logCache
122-
if enableLogCache {
123-
lc = attachLogCache(state.Logger)
124-
}
125-
126-
// launch the browser
127-
initEnv := &k6common.InitEnvironment{
128-
Logger: state.Logger,
129-
Registry: registry,
130-
}
131-
// NewBrowserType must be run from the init context, but since the recent
132-
// validation introduced in k6/js/modulestest, InitEnvField and StateField
133-
// must not be both set at the same time. So we nil StateField, create
134-
// the BrowserType, and revert it before running Launch().
135-
// See https://github.com/grafana/k6/pull/2602
136-
vu.InitEnvField = initEnv
137-
vu.StateField = nil
138-
v := chromium.NewBrowserType(vu)
139-
bt, ok := v.(*chromium.BrowserType)
140-
if !ok {
141-
panic(fmt.Errorf("testBrowser: unexpected browser type %T", v))
142-
}
143-
vu.InitEnvField = nil
144-
vu.StateField = state
145129
b := bt.Launch(rt.ToValue(launchOpts))
146130
tb.Cleanup(func() {
147131
select {

0 commit comments

Comments
 (0)