forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_test.go
136 lines (113 loc) · 3.89 KB
/
common_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package executor
import (
"context"
"io"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/minirunner"
"go.k6.io/k6/metrics"
)
func simpleRunner(vuFn func(context.Context, *lib.State) error) lib.Runner {
return &minirunner.MiniRunner{
Fn: func(ctx context.Context, state *lib.State, _ chan<- metrics.SampleContainer) error {
return vuFn(ctx, state)
},
}
}
func getTestRunState(tb testing.TB, options lib.Options, runner lib.Runner) *lib.TestRunState {
reg := metrics.NewRegistry()
piState := &lib.TestPreInitState{
Logger: testutils.NewLogger(tb),
RuntimeOptions: lib.RuntimeOptions{},
Registry: reg,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(reg),
}
require.NoError(tb, runner.SetOptions(options))
return &lib.TestRunState{
TestPreInitState: piState,
Options: options,
Runner: runner,
RunTags: piState.Registry.RootTagSet().WithTagsFromMap(options.RunTags),
}
}
func setupExecutor(t testing.TB, config lib.ExecutorConfig, es *lib.ExecutionState) (
context.Context, context.CancelFunc, lib.Executor, *testutils.SimpleLogrusHook,
) {
ctx, cancel := context.WithCancel(context.Background())
engineOut := make(chan metrics.SampleContainer, 100) // TODO: return this for more complicated tests?
logHook := testutils.NewLogHook(logrus.WarnLevel)
testLog := logrus.New()
testLog.AddHook(logHook)
testLog.SetOutput(io.Discard)
logEntry := logrus.NewEntry(testLog)
initVUFunc := func(_ context.Context, _ *logrus.Entry) (lib.InitializedVU, error) {
idl, idg := es.GetUniqueVUIdentifiers()
return es.Test.Runner.NewVU(ctx, idl, idg, engineOut)
}
es.SetInitVUFunc(initVUFunc)
maxPlannedVUs := lib.GetMaxPlannedVUs(config.GetExecutionRequirements(es.ExecutionTuple))
initializeVUs(ctx, t, logEntry, es, maxPlannedVUs, initVUFunc)
executor, err := config.NewExecutor(es, logEntry)
require.NoError(t, err)
err = executor.Init(ctx)
require.NoError(t, err)
return ctx, cancel, executor, logHook
}
func initializeVUs(
ctx context.Context, t testing.TB, logEntry *logrus.Entry, es *lib.ExecutionState, number uint64, initVU lib.InitVUFunc,
) {
// This is not how the local ExecutionScheduler initializes VUs, but should do the same job
for i := uint64(0); i < number; i++ {
// Not calling es.InitializeNewVU() here to avoid a double increment of initializedVUs,
// which is done in es.AddInitializedVU().
vu, err := initVU(ctx, logEntry)
require.NoError(t, err)
es.AddInitializedVU(vu)
}
}
type executorTest struct {
options lib.Options
state *lib.ExecutionState
ctx context.Context
cancel context.CancelFunc
executor lib.Executor
logHook *testutils.SimpleLogrusHook
}
//nolint:unparam
func setupExecutorTest(
t testing.TB, segmentStr, sequenceStr string, extraOptions lib.Options,
runner lib.Runner, config lib.ExecutorConfig,
) *executorTest {
var err error
var segment *lib.ExecutionSegment
if segmentStr != "" {
segment, err = lib.NewExecutionSegmentFromString(segmentStr)
require.NoError(t, err)
}
var sequence lib.ExecutionSegmentSequence
if sequenceStr != "" {
sequence, err = lib.NewExecutionSegmentSequenceFromString(sequenceStr)
require.NoError(t, err)
}
et, err := lib.NewExecutionTuple(segment, &sequence)
require.NoError(t, err)
options := lib.Options{
ExecutionSegment: segment,
ExecutionSegmentSequence: &sequence,
}.Apply(runner.GetOptions()).Apply(extraOptions)
testRunState := getTestRunState(t, options, runner)
execReqs := config.GetExecutionRequirements(et)
es := lib.NewExecutionState(testRunState, et, lib.GetMaxPlannedVUs(execReqs), lib.GetMaxPossibleVUs(execReqs))
ctx, cancel, executor, logHook := setupExecutor(t, config, es)
return &executorTest{
options: options,
state: es,
ctx: ctx,
cancel: cancel,
executor: executor,
logHook: logHook,
}
}