Skip to content

Commit

Permalink
Move MiniRunner to testutils and simplify executor test setup
Browse files Browse the repository at this point in the history
MiniRunner was also slightly improved to not use random VU IDs, so it matches the JS runner more closely.
  • Loading branch information
na-- committed Dec 18, 2019
1 parent 15df310 commit f6be35c
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 191 deletions.
3 changes: 2 additions & 1 deletion api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/core/local"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/testutils"
)

func testHTTPHandler(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestLogger(t *testing.T) {
}

func TestWithEngine(t *testing.T) {
execScheduler, err := local.NewExecutionScheduler(&lib.MiniRunner{}, logrus.StandardLogger())
execScheduler, err := local.NewExecutionScheduler(&testutils.MiniRunner{}, logrus.StandardLogger())
require.NoError(t, err)
engine, err := core.NewEngine(execScheduler, lib.Options{}, logrus.StandardLogger())
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion api/v1/group_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/core/local"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/testutils"
"github.com/manyminds/api2go/jsonapi"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand All @@ -43,7 +44,7 @@ func TestGetGroups(t *testing.T) {
g2, err := g1.Group("group 2")
assert.NoError(t, err)

execScheduler, err := local.NewExecutionScheduler(&lib.MiniRunner{Group: g0}, logrus.StandardLogger())
execScheduler, err := local.NewExecutionScheduler(&testutils.MiniRunner{Group: g0}, logrus.StandardLogger())
require.NoError(t, err)
engine, err := core.NewEngine(execScheduler, lib.Options{}, logrus.StandardLogger())
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions api/v1/metric_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import (
"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/core/local"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/stats"
"github.com/manyminds/api2go/jsonapi"
"github.com/stretchr/testify/assert"
null "gopkg.in/guregu/null.v3"
)

func TestGetMetrics(t *testing.T) {
execScheduler, err := local.NewExecutionScheduler(&lib.MiniRunner{}, logrus.StandardLogger())
execScheduler, err := local.NewExecutionScheduler(&testutils.MiniRunner{}, logrus.StandardLogger())
require.NoError(t, err)
engine, err := core.NewEngine(execScheduler, lib.Options{}, logrus.StandardLogger())
require.NoError(t, err)
Expand Down Expand Up @@ -81,7 +82,7 @@ func TestGetMetrics(t *testing.T) {
}

func TestGetMetric(t *testing.T) {
execScheduler, err := local.NewExecutionScheduler(&lib.MiniRunner{}, logrus.StandardLogger())
execScheduler, err := local.NewExecutionScheduler(&testutils.MiniRunner{}, logrus.StandardLogger())
require.NoError(t, err)
engine, err := core.NewEngine(execScheduler, lib.Options{}, logrus.StandardLogger())
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion api/v1/status_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ import (
"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/core/local"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/testutils"
"github.com/manyminds/api2go/jsonapi"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetStatus(t *testing.T) {
execScheduler, err := local.NewExecutionScheduler(&lib.MiniRunner{}, logrus.StandardLogger())
execScheduler, err := local.NewExecutionScheduler(&testutils.MiniRunner{}, logrus.StandardLogger())
require.NoError(t, err)
engine, err := core.NewEngine(execScheduler, lib.Options{}, logrus.StandardLogger())
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/config_consolidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func runTestCase(

var runner lib.Runner
if testCase.options.runner != nil {
runner = &lib.MiniRunner{Options: *testCase.options.runner}
runner = &testutils.MiniRunner{Options: *testCase.options.runner}
}
if testCase.options.fs == nil {
t.Logf("Creating an empty FS for this test")
Expand Down
6 changes: 3 additions & 3 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const isWindows = runtime.GOOS == "windows"
// Wrapper around NewEngine that applies a logger and manages the options.
func newTestEngine(t *testing.T, ctx context.Context, runner lib.Runner, opts lib.Options) *Engine { //nolint: golint
if runner == nil {
runner = &lib.MiniRunner{}
runner = &testutils.MiniRunner{}
}
if ctx == nil {
ctx = context.Background()
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestEngineRun(t *testing.T) {

signalChan := make(chan interface{})

runner := &lib.MiniRunner{Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
runner := &testutils.MiniRunner{Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
stats.PushIfNotDone(ctx, out, stats.Sample{Metric: testMetric, Time: time.Now(), Value: 1})
close(signalChan)
<-ctx.Done()
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestEngineAtTime(t *testing.T) {
func TestEngineCollector(t *testing.T) {
testMetric := stats.New("test_metric", stats.Trend)

runner := &lib.MiniRunner{Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
runner := &testutils.MiniRunner{Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
out <- stats.Sample{Metric: testMetric}
return nil
}}
Expand Down
50 changes: 25 additions & 25 deletions core/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newTestExecutionScheduler(
t *testing.T, runner lib.Runner, logger *logrus.Logger, opts lib.Options, //nolint: golint
) (ctx context.Context, cancel func(), execScheduler *ExecutionScheduler, samples chan stats.SampleContainer) {
if runner == nil {
runner = &lib.MiniRunner{}
runner = &testutils.MiniRunner{}
}
ctx, cancel = context.WithCancel(context.Background())
newOpts, err := executor.DeriveExecutionFromShortcuts(lib.Options{
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestExecutionSchedulerSetupTeardownRun(t *testing.T) {
t.Run("Normal", func(t *testing.T) {
setupC := make(chan struct{})
teardownC := make(chan struct{})
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
SetupFn: func(ctx context.Context, out chan<- stats.SampleContainer) ([]byte, error) {
close(setupC)
return nil, nil
Expand All @@ -120,7 +120,7 @@ func TestExecutionSchedulerSetupTeardownRun(t *testing.T) {
assert.NoError(t, <-err)
})
t.Run("Setup Error", func(t *testing.T) {
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
SetupFn: func(ctx context.Context, out chan<- stats.SampleContainer) ([]byte, error) {
return nil, errors.New("setup error")
},
Expand All @@ -130,7 +130,7 @@ func TestExecutionSchedulerSetupTeardownRun(t *testing.T) {
assert.EqualError(t, execScheduler.Run(ctx, samples), "setup error")
})
t.Run("Don't Run Setup", func(t *testing.T) {
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
SetupFn: func(ctx context.Context, out chan<- stats.SampleContainer) ([]byte, error) {
return nil, errors.New("setup error")
},
Expand All @@ -148,7 +148,7 @@ func TestExecutionSchedulerSetupTeardownRun(t *testing.T) {
})

t.Run("Teardown Error", func(t *testing.T) {
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
SetupFn: func(ctx context.Context, out chan<- stats.SampleContainer) ([]byte, error) {
return nil, nil
},
Expand All @@ -165,7 +165,7 @@ func TestExecutionSchedulerSetupTeardownRun(t *testing.T) {
assert.EqualError(t, execScheduler.Run(ctx, samples), "teardown error")
})
t.Run("Don't Run Teardown", func(t *testing.T) {
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
SetupFn: func(ctx context.Context, out chan<- stats.SampleContainer) ([]byte, error) {
return nil, nil
},
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestExecutionSchedulerStages(t *testing.T) {
data := data
t.Run(name, func(t *testing.T) {
t.Parallel()
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
time.Sleep(100 * time.Millisecond)
return nil
Expand All @@ -231,7 +231,7 @@ func TestExecutionSchedulerStages(t *testing.T) {

func TestExecutionSchedulerEndTime(t *testing.T) {
t.Parallel()
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
time.Sleep(100 * time.Millisecond)
return nil
Expand All @@ -256,7 +256,7 @@ func TestExecutionSchedulerEndTime(t *testing.T) {

func TestExecutionSchedulerRuntimeErrors(t *testing.T) {
t.Parallel()
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
time.Sleep(10 * time.Millisecond)
return errors.New("hi")
Expand Down Expand Up @@ -294,7 +294,7 @@ func TestExecutionSchedulerEndErrors(t *testing.T) {
exec.Duration = types.NullDurationFrom(1 * time.Second)
exec.GracefulStop = types.NullDurationFrom(0 * time.Second)

runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
<-ctx.Done()
return errors.New("hi")
Expand Down Expand Up @@ -332,7 +332,7 @@ func TestExecutionSchedulerEndIterations(t *testing.T) {
require.Empty(t, options.Validate())

var i int64
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestExecutionSchedulerEndIterations(t *testing.T) {

func TestExecutionSchedulerIsRunning(t *testing.T) {
t.Parallel()
runner := &lib.MiniRunner{
runner := &testutils.MiniRunner{
Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
<-ctx.Done()
return nil
Expand Down Expand Up @@ -404,7 +404,7 @@ func TestExecutionSchedulerSetVUs(t *testing.T) {
})
t.Run("Raise", func(t *testing.T) {
e := New(&lib.MiniRunner{Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
e := New(&testutils.MiniRunner{Fn: func(ctx context.Context, out chan<- stats.SampleContainer) error {
return nil
}})
e.ctx = context.Background()
Expand All @@ -416,7 +416,7 @@ func TestExecutionSchedulerSetVUs(t *testing.T) {
for i, handle := range e.vus {
num++
if assert.NotNil(t, handle.vu, "vu %d lacks impl", i) {
assert.Equal(t, int64(0), handle.vu.(*lib.MiniRunnerVU).ID)
assert.Equal(t, int64(0), handle.vu.(*testutils.MiniRunnerVU).ID)
}
assert.Nil(t, handle.ctx, "vu %d has ctx", i)
assert.Nil(t, handle.cancel, "vu %d has cancel", i)
Expand All @@ -431,11 +431,11 @@ func TestExecutionSchedulerSetVUs(t *testing.T) {
for i, handle := range e.vus {
if i < 50 {
assert.NotNil(t, handle.cancel, "vu %d lacks cancel", i)
assert.Equal(t, int64(i+1), handle.vu.(*lib.MiniRunnerVU).ID)
assert.Equal(t, int64(i+1), handle.vu.(*testutils.MiniRunnerVU).ID)
num++
} else {
assert.Nil(t, handle.cancel, "vu %d has cancel", i)
assert.Equal(t, int64(0), handle.vu.(*lib.MiniRunnerVU).ID)
assert.Equal(t, int64(0), handle.vu.(*testutils.MiniRunnerVU).ID)
}
}
assert.Equal(t, 50, num)
Expand All @@ -447,7 +447,7 @@ func TestExecutionSchedulerSetVUs(t *testing.T) {
num := 0
for i, handle := range e.vus {
assert.NotNil(t, handle.cancel, "vu %d lacks cancel", i)
assert.Equal(t, int64(i+1), handle.vu.(*lib.MiniRunnerVU).ID)
assert.Equal(t, int64(i+1), handle.vu.(*testutils.MiniRunnerVU).ID)
num++
}
assert.Equal(t, 100, num)
Expand All @@ -465,7 +465,7 @@ func TestExecutionSchedulerSetVUs(t *testing.T) {
} else {
assert.Nil(t, handle.cancel, "vu %d has cancel", i)
}
assert.Equal(t, int64(i+1), handle.vu.(*lib.MiniRunnerVU).ID)
assert.Equal(t, int64(i+1), handle.vu.(*testutils.MiniRunnerVU).ID)
}
assert.Equal(t, 50, num)
}
Expand All @@ -477,9 +477,9 @@ func TestExecutionSchedulerSetVUs(t *testing.T) {
for i, handle := range e.vus {
assert.NotNil(t, handle.cancel, "vu %d lacks cancel", i)
if i < 50 {
assert.Equal(t, int64(i+1), handle.vu.(*lib.MiniRunnerVU).ID)
assert.Equal(t, int64(i+1), handle.vu.(*testutils.MiniRunnerVU).ID)
} else {
assert.Equal(t, int64(50+i+1), handle.vu.(*lib.MiniRunnerVU).ID)
assert.Equal(t, int64(50+i+1), handle.vu.(*testutils.MiniRunnerVU).ID)
}
}
}
Expand Down Expand Up @@ -658,7 +658,7 @@ func (p pausableExecutor) SetPaused(bool) error {

func TestSetPaused(t *testing.T) {
t.Run("second pause is an error", func(t *testing.T) {
var runner = &lib.MiniRunner{}
var runner = &testutils.MiniRunner{}
logger := logrus.New()
logger.SetOutput(testutils.NewTestOutput(t))
var sched, err = NewExecutionScheduler(runner, logger)
Expand All @@ -672,7 +672,7 @@ func TestSetPaused(t *testing.T) {
})

t.Run("unpause at the start is an error", func(t *testing.T) {
var runner = &lib.MiniRunner{}
var runner = &testutils.MiniRunner{}
logger := logrus.New()
logger.SetOutput(testutils.NewTestOutput(t))
var sched, err = NewExecutionScheduler(runner, logger)
Expand All @@ -684,7 +684,7 @@ func TestSetPaused(t *testing.T) {
})

t.Run("second unpause is an error", func(t *testing.T) {
var runner = &lib.MiniRunner{}
var runner = &testutils.MiniRunner{}
logger := logrus.New()
logger.SetOutput(testutils.NewTestOutput(t))
var sched, err = NewExecutionScheduler(runner, logger)
Expand All @@ -698,7 +698,7 @@ func TestSetPaused(t *testing.T) {
})

t.Run("an error on pausing is propagated", func(t *testing.T) {
var runner = &lib.MiniRunner{}
var runner = &testutils.MiniRunner{}
logger := logrus.New()
logger.SetOutput(testutils.NewTestOutput(t))
var sched, err = NewExecutionScheduler(runner, logger)
Expand All @@ -711,7 +711,7 @@ func TestSetPaused(t *testing.T) {
})

t.Run("can't pause unpausable executor", func(t *testing.T) {
var runner = &lib.MiniRunner{}
var runner = &testutils.MiniRunner{}
options, err := executor.DeriveExecutionFromShortcuts(lib.Options{
Iterations: null.IntFrom(2),
VUs: null.IntFrom(1),
Expand Down
24 changes: 15 additions & 9 deletions lib/executor/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package executor
import (
"context"
"io/ioutil"
"math/rand"
"testing"

"github.com/loadimpact/k6/lib"
Expand All @@ -13,23 +12,29 @@ import (
"github.com/stretchr/testify/require"
)

func setupExecutor(
t *testing.T, config lib.ExecutorConfig,
vuFn func(context.Context, chan<- stats.SampleContainer) error,
) (context.Context, context.CancelFunc, lib.Executor, *testutils.SimpleLogrusHook) {
func simpleRunner(vuFn func(context.Context) error) lib.Runner {
return &testutils.MiniRunner{
Fn: func(ctx context.Context, _ chan<- stats.SampleContainer) error {
return vuFn(ctx)
},
}
}

func setupExecutor(t *testing.T, config lib.ExecutorConfig, runner lib.Runner) (
context.Context, context.CancelFunc, lib.Executor, *testutils.SimpleLogrusHook,
) {
ctx, cancel := context.WithCancel(context.Background())
engineOut := make(chan stats.SampleContainer, 100) //TODO: return this for more complicated tests?

logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.WarnLevel}}
testLog := logrus.New()
testLog.AddHook(logHook)
testLog.SetOutput(ioutil.Discard)
logEntry := logrus.NewEntry(testLog)
es := lib.NewExecutionState(lib.Options{}, 10, 50)
runner := lib.MiniRunner{
Fn: vuFn,
}

es.SetInitVUFunc(func(_ context.Context, logger *logrus.Entry) (lib.VU, error) {
return &lib.MiniRunnerVU{R: runner, ID: rand.Int63()}, nil
return runner.NewVU(engineOut)
})

initializeVUs(ctx, t, logEntry, es, 10)
Expand All @@ -44,6 +49,7 @@ func setupExecutor(
func initializeVUs(
ctx context.Context, t testing.TB, logEntry *logrus.Entry, es *lib.ExecutionState, number int,
) {
// This is not how the local ExecutionScheduler initializes VUs, but should do the same job
for i := 0; i < number; i++ {
require.EqualValues(t, i, es.GetInitializedVUsCount())
vu, err := es.InitializeNewVU(ctx, logEntry)
Expand Down
Loading

0 comments on commit f6be35c

Please sign in to comment.