Skip to content

Commit

Permalink
Address feedback from PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Mirić committed Jun 30, 2023
1 parent f2e4a52 commit 91fc97e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
15 changes: 7 additions & 8 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) {
}

defer func() {
emitEvent(&event.Event{
waitExitDone := emitEvent(&event.Event{
Type: event.Exit,
Data: &event.ExitData{Error: err},
})()
})
waitExitDone()
c.gs.Events.UnsubscribeAll()
}()

Expand Down Expand Up @@ -179,10 +180,6 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) {
}()
}

// TODO: Subscribe all initialization processes (outputs, VUs and executors)
// to the Init event. This would allow running them concurrently, and they
// could be synchronized by waiting for the event processing to complete.
// This could later be expanded to also initialize browser processes.
waitInitDone := emitEvent(&event.Event{Type: event.Init})

// Create and start the outputs. We do it quite early to get any output URLs
Expand Down Expand Up @@ -334,13 +331,15 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) (err error) {

waitInitDone()

emitEvent(&event.Event{Type: event.TestStart})()
waitTestStartDone := emitEvent(&event.Event{Type: event.TestStart})
waitTestStartDone()

// Start the test! However, we won't immediately return if there was an
// error, we still have things to do.
err = execScheduler.Run(globalCtx, runCtx, samples)

defer emitEvent(&event.Event{Type: event.TestEnd})()
waitTestEndDone := emitEvent(&event.Event{Type: event.TestEnd})
defer waitTestEndDone()

// Init has passed successfully, so unless disabled, make sure we send a
// usage report after the context is done.
Expand Down
7 changes: 4 additions & 3 deletions js/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type VU interface {

// Events allows subscribing to global k6 execution events, such as Init and
// Exit, and to local (per-VU) events, such as IterStart and IterEnd.
// NOTE: This API is EXPERIMENTAL and may be changed, renamed or
// completely removed in a later k6 release.
// FIXME: Subscribing to global events shouldn't be part of this VU (local)
// interface.
Events() common.Events

// InitEnv returns common.InitEnvironment instance if present
Expand All @@ -59,9 +63,6 @@ type VU interface {
// on the event loop *at a later point in time*. See the documentation for
// `EventLoop.RegisterCallback()` in the `k6/js/eventloop` Go module for
// the very important details on its usage and restrictions.
//
// Notice: This API is EXPERIMENTAL and may be changed, renamed or
// completely removed in a later k6 release.
RegisterCallback() (enqueueCallback func(func() error))

// sealing field will help probably with pointing users that they just need to embed this in their Instance
Expand Down
16 changes: 7 additions & 9 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,18 +773,16 @@ func (u *ActiveVU) RunOnce() error {
ScenarioName: u.scenarioName,
}

emitEvent := func(evt *event.Event) func() {
emitAndWaitEvent := func(evt *event.Event) {
waitDone := u.moduleVUImpl.events.local.Emit(evt)
return func() {
waitCtx, waitCancel := context.WithTimeout(u.RunContext, 30*time.Minute)
defer waitCancel()
if werr := waitDone(waitCtx); werr != nil {
u.state.Logger.WithError(werr).Warn()
}
waitCtx, waitCancel := context.WithTimeout(u.RunContext, 30*time.Minute)
defer waitCancel()
if werr := waitDone(waitCtx); werr != nil {
u.state.Logger.WithError(werr).Warn()
}
}

emitEvent(&event.Event{Type: event.IterStart, Data: eventIterData})()
emitAndWaitEvent(&event.Event{Type: event.IterStart, Data: eventIterData})

// Call the exported function.
_, isFullIteration, totalTime, err := u.runFn(ctx, true, fn, cancel, u.setupData)
Expand All @@ -799,7 +797,7 @@ func (u *ActiveVU) RunOnce() error {
eventIterData.Error = err
}

emitEvent(&event.Event{Type: event.IterEnd, Data: eventIterData})()
emitAndWaitEvent(&event.Event{Type: event.IterEnd, Data: eventIterData})

// If MinIterationDuration is specified and the iteration wasn't canceled
// and was less than it, sleep for the remainder
Expand Down

0 comments on commit 91fc97e

Please sign in to comment.