diff --git a/cmd/tests/cmd_run_test.go b/cmd/tests/cmd_run_test.go index dbf777000c66..34faec1c229a 100644 --- a/cmd/tests/cmd_run_test.go +++ b/cmd/tests/cmd_run_test.go @@ -13,6 +13,7 @@ import ( "runtime" "strings" "sync" + "sync/atomic" "syscall" "testing" "time" @@ -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 = { @@ -2031,7 +2029,7 @@ func TestEventSystemOK(t *testing.T) { } export default function () { sleep(1); } - `)) + `, moduleName))) cmd.ExecuteWithGlobalState(ts.GlobalState) @@ -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'; @@ -2082,7 +2081,7 @@ func TestEventSystemAborted(t *testing.T) { sleep(1); test.abort('oops!'); } - `)) + `, moduleName))) cmd.ExecuteWithGlobalState(ts.GlobalState)