forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare_test.go
117 lines (104 loc) · 3.11 KB
/
share_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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2020 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package js
import (
"context"
"io/ioutil"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/metrics"
)
func TestNewSharedArrayIntegration(t *testing.T) {
t.Parallel()
data := `'use strict';
var SharedArray = require("k6/data").SharedArray;
function generateArray() {
console.log("once");
var n = 50;
var arr = new Array(n);
for (var i = 0 ; i <n; i++) {
arr[i] = {value: "something" +i};
}
return arr;
}
var s = new SharedArray("something", generateArray);
exports.default = function() {
if (s[2].value !== "something2") {
throw new Error("bad s[2]="+s[2].value);
}
if (s.length != 50) {
throw new Error("bad length " +_s.length);
}
var i = 0;
for (var v of s) {
if (v.value !== "something"+i) {
throw new Error("bad v.value="+v.value+" for i="+i);
}
i++;
}
}`
logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
logger.Out = ioutil.Discard
hook := testutils.SimpleLogrusHook{
HookedLevels: []logrus.Level{logrus.InfoLevel, logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel},
}
logger.AddHook(&hook)
r1, err := getSimpleRunner(t, "/script.js", data, logger)
require.NoError(t, err)
entries := hook.Drain()
require.Len(t, entries, 1)
assert.Equal(t, logrus.InfoLevel, entries[0].Level)
assert.Equal(t, "once", entries[0].Message)
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: logger,
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, r1.MakeArchive())
require.NoError(t, err)
entries = hook.Drain()
require.Len(t, entries, 1)
assert.Equal(t, logrus.InfoLevel, entries[0].Level)
assert.Equal(t, "once", entries[0].Message)
testdata := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range testdata {
r := r
t.Run(name, func(t *testing.T) {
t.Parallel()
samples := make(chan metrics.SampleContainer, 100)
initVU, err := r.NewVU(1, 1, samples)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
require.NoError(t, err)
entries := hook.Drain()
assert.Len(t, entries, 0)
})
}
}