Skip to content

Commit

Permalink
fixup! Emit events in run command and js.VU.RunOnce()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Jun 5, 2023
1 parent 2c26bb3 commit ea15c54
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -2001,28 +2002,25 @@ func TestBadLogOutput(t *testing.T) {
}
}

// HACK: We need this so multiple tests can register differently named modules.
var uniqueModuleNumber uint64 //nolint:gochecknoglobals

// Tests that the appropriate events are emitted at the appropriate times.
func TestEventSystemOK(t *testing.T) {
t.Parallel()

ts := NewGlobalTestState(t)

// HACK: We need to be able to run multiple tests in parallel on the events
// module, but since it does event subscription in a sync.Once and the API
// addresses need to be different for separate tests, we register separate
// modules to workaround it. I experimented with passing the address via
// the script, but the sync.Once is still a problem.
// Note that this also forbids running these tests with more than -count=1,
// unless we dynamically increment the module name... /sigh
modules.Register("k6/x/testevents1", events.New(
moduleName := fmt.Sprintf("k6/x/testevents-%d", atomic.AddUint64(&uniqueModuleNumber, 1))
modules.Register(moduleName, events.New(
ts.GlobalState.DefaultFlags.Address, []event.Type{
event.Init, event.TestStart, event.IterStart, event.IterEnd,
event.TestEnd, event.Exit,
}))

ts.CmdArgs = []string{"k6", "--quiet", "run", "-"}
ts.Stdin = bytes.NewBuffer([]byte(`
import events from 'k6/x/testevents1';
ts.Stdin = bytes.NewBuffer([]byte(fmt.Sprintf(`
import events from '%s';
import { sleep } from 'k6';
export let options = {
Expand All @@ -2031,7 +2029,7 @@ func TestEventSystemOK(t *testing.T) {
}
export default function () { sleep(1); }
`))
`, moduleName)))

cmd.ExecuteWithGlobalState(ts.GlobalState)

Expand Down Expand Up @@ -2061,15 +2059,16 @@ func TestEventSystemAborted(t *testing.T) {

ts := NewGlobalTestState(t)

modules.Register("k6/x/testevents2", events.New(
moduleName := fmt.Sprintf("k6/x/testevents-%d", atomic.AddUint64(&uniqueModuleNumber, 1))
modules.Register(moduleName, events.New(
ts.GlobalState.DefaultFlags.Address, []event.Type{
event.Init, event.TestStart, event.TestEnd, event.Exit,
}))

ts.CmdArgs = []string{"k6", "--quiet", "run", "-"}
ts.ExpectedExitCode = int(exitcodes.ScriptAborted)
ts.Stdin = bytes.NewBuffer([]byte(`
import events from 'k6/x/testevents2';
ts.Stdin = bytes.NewBuffer([]byte(fmt.Sprintf(`
import events from '%s';
import { test } from 'k6/execution';
import { sleep } from 'k6';
Expand All @@ -2082,7 +2081,7 @@ func TestEventSystemAborted(t *testing.T) {
sleep(1);
test.abort('oops!');
}
`))
`, moduleName)))

cmd.ExecuteWithGlobalState(ts.GlobalState)

Expand Down

0 comments on commit ea15c54

Please sign in to comment.