Skip to content

Commit

Permalink
js: skip minIterationDuration in setup/teardown
Browse files Browse the repository at this point in the history
Close #1003
  • Loading branch information
cuonglm committed Sep 26, 2019
1 parent 731e55b commit 9197621
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 14 deletions.
68 changes: 68 additions & 0 deletions core/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,3 +968,71 @@ func TestMinIterationDuration(t *testing.T) {
// But we expect the custom counter to be added to 4 times
assert.Equal(t, 4.0, getMetricSum(collector, "testcounter"))
}

//nolint: funlen
func TestMinIterationDurationInSetupTeardownStage(t *testing.T) {
t.Parallel()
setupScript := `
import { sleep } from "k6";
export function setup() {
sleep(1);
}
export let options = {
minIterationDuration: "2s",
duration: "2s",
setupTimeout: "2s",
};
export default function () {
};`
teardownScript := `
import { sleep } from "k6";
export let options = {
minIterationDuration: "2s",
duration: "2s",
teardownTimeout: "2s",
};
export default function () {
};
export function teardown() {
sleep(1);
}
`
tests := []struct {
name, script string
}{
{"Test setup", setupScript},
{"Test teardown", teardownScript},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
runner, err := js.New(
&loader.SourceData{URL: &url.URL{Path: "/script.js"}, Data: []byte(tc.script)},
nil,
lib.RuntimeOptions{},
)
require.NoError(t, err)
engine, err := NewEngine(local.New(runner), runner.GetOptions())
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
errC := make(chan error)
go func() { errC <- engine.Run(ctx) }()
select {
case <-time.After(10 * time.Second):
cancel()
t.Fatal("Test timed out")
case err := <-errC:
cancel()
require.NoError(t, err)
require.False(t, engine.IsTainted())
}
})
}
}
29 changes: 15 additions & 14 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (r *Runner) runPart(ctx context.Context, out chan<- stats.SampleContainer,
return goja.Undefined(), err
}

v, _, err := vu.runFn(ctx, group, fn, vu.Runtime.ToValue(arg))
v, _, _, _, err := vu.runFn(ctx, group, fn, vu.Runtime.ToValue(arg))

// deadline is reached so we have timeouted but this might've not been registered correctly
if deadline, ok := ctx.Deadline(); ok && time.Now().After(deadline) {
Expand Down Expand Up @@ -418,16 +418,26 @@ func (u *VU) RunOnce(ctx context.Context) error {
}

// Call the default function.
_, _, err := u.runFn(ctx, u.Runner.defaultGroup, u.Default, u.setupData)
_, _, isFullIteration, totalTime, err := u.runFn(ctx, u.Runner.defaultGroup, u.Default, u.setupData)

// If MinIterationDuration is specified and the iteration wasn't cancelled
// and was less than it, sleep for the remainder
if isFullIteration && u.Runner.Bundle.Options.MinIterationDuration.Valid {
durationDiff := time.Duration(u.Runner.Bundle.Options.MinIterationDuration.Duration) - totalTime
if durationDiff > 0 {
time.Sleep(durationDiff)
}
}

return err
}

func (u *VU) runFn(
ctx context.Context, group *lib.Group, fn goja.Callable, args ...goja.Value,
) (goja.Value, *lib.State, error) {
) (goja.Value, *lib.State, bool, time.Duration, error) {
cookieJar, err := cookiejar.New(nil)
if err != nil {
return goja.Undefined(), nil, err
return goja.Undefined(), nil, false, time.Duration(0), err
}

if u.Runner.Bundle.Options.NoCookiesReset.Valid && u.Runner.Bundle.Options.NoCookiesReset.Bool {
Expand Down Expand Up @@ -486,14 +496,5 @@ func (u *VU) runFn(

state.Samples <- u.Dialer.GetTrail(startTime, endTime, isFullIteration, stats.IntoSampleTags(&tags))

// If MinIterationDuration is specified and the iteration wasn't cancelled
// and was less than it, sleep for the remainder
if isFullIteration && state.Options.MinIterationDuration.Valid {
durationDiff := time.Duration(state.Options.MinIterationDuration.Duration) - endTime.Sub(startTime)
if durationDiff > 0 {
time.Sleep(durationDiff)
}
}

return v, state, err
return v, state, isFullIteration, endTime.Sub(startTime), err
}

0 comments on commit 9197621

Please sign in to comment.