Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Fix race on eventloop start in tests
Browse files Browse the repository at this point in the history
The eventloop provided by k6 is meant to be run until completion
rather than trying to start multiple event loops. This change will
only allow a single eventloop to start until all callbacks have
completed, and only then allow the next eventloop to start. This
issue is only occurring in tests as far as i can tell since in
normal running it doesn't use Start.
  • Loading branch information
ankur22 committed Aug 9, 2022
1 parent 262eaee commit e63220b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion k6ext/k6test/vu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k6test

import (
"context"
"sync"
"testing"

"github.com/grafana/xk6-browser/k6ext"
Expand All @@ -22,14 +23,19 @@ import (
// VU is a k6 VU instance.
type VU struct {
*k6modulestest.VU
Loop *k6eventloop.EventLoop
Loop *k6eventloop.EventLoop
runLoopMu sync.Mutex
}

// ToGojaValue is a convenience method for converting any value to a goja value.
func (v *VU) ToGojaValue(i interface{}) goja.Value { return v.Runtime().ToValue(i) }

// RunLoop is a convenience method for running fn in the event loop.
func (v *VU) RunLoop(fn func() error) error {
v.runLoopMu.Lock()
defer v.runLoopMu.Unlock()

v.Loop.WaitOnRegistered()
return v.Loop.Start(fn)
}

Expand Down

0 comments on commit e63220b

Please sign in to comment.