-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
init_and_modules_test.go
119 lines (102 loc) · 3.54 KB
/
init_and_modules_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package js_test
import (
"context"
"fmt"
"net/url"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
"go.k6.io/k6/js"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/loader"
"go.k6.io/k6/metrics"
)
type CheckModule struct {
t testing.TB
initCtxCalled int
vuCtxCalled int
}
func (cm *CheckModule) InitCtx(ctx context.Context) {
cm.initCtxCalled++
}
func (cm *CheckModule) VuCtx(ctx context.Context) {
cm.vuCtxCalled++
}
var uniqueModuleNumber int64 //nolint // we need this so multiple test can register differently named modules
func TestNewJSRunnerWithCustomModule(t *testing.T) {
t.Parallel()
checkModule := &CheckModule{t: t}
moduleName := fmt.Sprintf("k6/x/check-%d", atomic.AddInt64(&uniqueModuleNumber, 1))
modules.Register(moduleName, checkModule)
script := fmt.Sprintf(`
var check = require("%s");
check.initCtx();
module.exports.options = { vus: 1, iterations: 1 };
module.exports.default = function() {
check.vuCtx();
};
`, moduleName)
logger := testutils.NewLogger(t)
rtOptions := lib.RuntimeOptions{CompatibilityMode: null.StringFrom("base")}
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
runner, err := js.New(
&lib.TestPreInitState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
RuntimeOptions: rtOptions,
},
&loader.SourceData{
URL: &url.URL{Path: "blah", Scheme: "file"},
Data: []byte(script),
},
map[string]fsext.Fs{"file": fsext.NewMemMapFs(), "https": fsext.NewMemMapFs()},
)
require.NoError(t, err)
assert.Equal(t, checkModule.initCtxCalled, 1)
assert.Equal(t, checkModule.vuCtxCalled, 0)
vu, err := runner.NewVU(context.Background(), 1, 1, make(chan metrics.SampleContainer, 100))
require.NoError(t, err)
assert.Equal(t, checkModule.initCtxCalled, 2)
assert.Equal(t, checkModule.vuCtxCalled, 0)
vuCtx, vuCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer vuCancel()
activeVU := vu.Activate(&lib.VUActivationParams{RunContext: vuCtx})
require.NoError(t, activeVU.RunOnce())
assert.Equal(t, checkModule.initCtxCalled, 2)
assert.Equal(t, checkModule.vuCtxCalled, 1)
require.NoError(t, activeVU.RunOnce())
assert.Equal(t, checkModule.initCtxCalled, 2)
assert.Equal(t, checkModule.vuCtxCalled, 2)
arc := runner.MakeArchive()
assert.Equal(t, checkModule.initCtxCalled, 2) // shouldn't change, we're not executing the init context again
assert.Equal(t, checkModule.vuCtxCalled, 2)
runnerFromArc, err := js.NewFromArchive(
&lib.TestPreInitState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
RuntimeOptions: rtOptions,
}, arc)
require.NoError(t, err)
assert.Equal(t, checkModule.initCtxCalled, 3) // changes because we need to get the exported functions
assert.Equal(t, checkModule.vuCtxCalled, 2)
vuFromArc, err := runnerFromArc.NewVU(context.Background(), 2, 2, make(chan metrics.SampleContainer, 100))
require.NoError(t, err)
assert.Equal(t, checkModule.initCtxCalled, 4)
assert.Equal(t, checkModule.vuCtxCalled, 2)
activeVUFromArc := vuFromArc.Activate(&lib.VUActivationParams{RunContext: vuCtx})
require.NoError(t, activeVUFromArc.RunOnce())
assert.Equal(t, checkModule.initCtxCalled, 4)
assert.Equal(t, checkModule.vuCtxCalled, 3)
require.NoError(t, activeVUFromArc.RunOnce())
assert.Equal(t, checkModule.initCtxCalled, 4)
assert.Equal(t, checkModule.vuCtxCalled, 4)
}