1
1
package k6test
2
2
3
3
import (
4
- "context"
5
4
"testing"
6
5
7
6
"github.com/grafana/xk6-browser/k6ext"
@@ -10,16 +9,21 @@ import (
10
9
k6eventloop "go.k6.io/k6/js/eventloop"
11
10
k6modulestest "go.k6.io/k6/js/modulestest"
12
11
k6lib "go.k6.io/k6/lib"
12
+ k6testutils "go.k6.io/k6/lib/testutils"
13
13
k6metrics "go.k6.io/k6/metrics"
14
14
15
15
"github.com/dop251/goja"
16
16
"github.com/oxtoacart/bpool"
17
- "github.com/sirupsen/logrus"
18
17
"github.com/stretchr/testify/require"
19
18
"gopkg.in/guregu/null.v3"
20
19
)
21
20
22
21
// VU is a k6 VU instance.
22
+ // TODO: Do we still need this VU wrapper?
23
+ // ToGojaValue can be a helper function that takes a goja.Runtime (although it's
24
+ // not much of a helper from calling ToValue(i) directly...), and we can access
25
+ // EventLoop from modulestest.Runtime.EventLoop. I guess we still need the
26
+ // RunLoop() override to call WaitOnRegistered()?
23
27
type VU struct {
24
28
* k6modulestest.VU
25
29
Loop * k6eventloop.EventLoop
@@ -34,7 +38,7 @@ func (v *VU) RunLoop(fn func() error) error {
34
38
return v .Loop .Start (fn )
35
39
}
36
40
37
- // NewVU returns a mock VU.
41
+ // NewVU returns a mock k6 VU.
38
42
func NewVU (tb testing.TB ) * VU {
39
43
tb .Helper ()
40
44
@@ -56,28 +60,18 @@ func NewVU(tb testing.TB) *VU {
56
60
BatchPerHost : null .IntFrom (20 ),
57
61
// HTTPDebug: null.StringFrom("full"),
58
62
},
59
- Logger : logrus . StandardLogger ( ),
63
+ Logger : k6testutils . NewLogger ( tb ),
60
64
Group : root ,
61
65
BPool : bpool .NewBufferPool (1 ),
62
66
Samples : samples ,
63
67
Tags : k6lib .NewTagMap (map [string ]string {"group" : root .Path }),
64
68
BuiltinMetrics : k6metrics .RegisterBuiltinMetrics (k6metrics .NewRegistry ()),
65
69
}
66
- vu := & VU {
67
- VU : & k6modulestest.VU {
68
- RuntimeField : rt ,
69
- InitEnvField : & k6common.InitEnvironment {
70
- Registry : k6metrics .NewRegistry (),
71
- },
72
- StateField : state ,
73
- },
74
- }
75
- ctx := k6ext .WithVU (context .Background (), vu )
76
- vu .CtxField = ctx
77
70
78
- loop := k6eventloop .New (vu )
79
- vu .RegisterCallbackField = loop .RegisterCallback
80
- vu .Loop = loop
71
+ testRT := k6modulestest .NewRuntime (tb )
72
+ ctx := k6ext .WithVU (testRT .VU .CtxField , testRT .VU )
73
+ testRT .VU .CtxField = ctx
74
+ testRT .MoveToVUContext (state )
81
75
82
- return vu
76
+ return & VU { VU : testRT . VU , Loop : testRT . EventLoop }
83
77
}
0 commit comments