Skip to content

Commit

Permalink
Reduce usage of some k6 API and Sobek directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jan 20, 2025
1 parent 5c328d4 commit 6f22885
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
toolchain go1.21.13

require (
github.com/grafana/sobek v0.0.0-20240808084414-f7ac208544fe
github.com/stretchr/testify v1.9.0
go.k6.io/k6 v0.53.0
)
Expand All @@ -20,6 +19,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6 // indirect
github.com/grafana/sobek v0.0.0-20240808084414-f7ac208544fe // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down
42 changes: 15 additions & 27 deletions taskqueue/queuer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import (
"testing"
"time"

"github.com/grafana/sobek"
"github.com/stretchr/testify/require"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/eventloop"
"go.k6.io/k6/js/modulestest"

"github.com/mstoykov/k6-taskqueue-lib/taskqueue"
Expand All @@ -17,17 +14,10 @@ import (
func TestTaskQueue(t *testing.T) {
// really basic test
t.Parallel()
rt := sobek.New()
vu := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: context.Background(),
StateField: nil,
}
loop := eventloop.New(vu)
fq := taskqueue.New(loop.RegisterCallback)
tr := modulestest.NewRuntime(t)
fq := taskqueue.New(tr.EventLoop.RegisterCallback)
var i int
require.NoError(t, rt.Set("a", func() {
require.NoError(t, tr.VU.Runtime().Set("a", func() {
fq.Queue(func() error {
fq.Queue(func() error {
fq.Queue(func() error {
Expand All @@ -43,8 +33,8 @@ func TestTaskQueue(t *testing.T) {
})
}))

err := loop.Start(func() error {
_, err := vu.Runtime().RunString(`a()`)
err := tr.EventLoop.Start(func() error {
_, err := tr.VU.Runtime().RunString(`a()`)
return err
})
require.NoError(t, err)
Expand All @@ -54,16 +44,14 @@ func TestTaskQueue(t *testing.T) {
func TestTwoTaskQueues(t *testing.T) {
// try to find any kind of races through running multiple queues and having them race with each other
t.Parallel()
rt := sobek.New()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
tr := modulestest.NewRuntime(t)
ctx, cancel := context.WithTimeout(tr.VU.Context(), time.Millisecond*100)
t.Cleanup(cancel)
vu := &modulestest.VU{
RuntimeField: rt,
CtxField: ctx,
}
loop := eventloop.New(vu)
fq := taskqueue.New(loop.RegisterCallback)
fq2 := taskqueue.New(loop.RegisterCallback)
tr.VU.CtxField = ctx

rt := tr.VU.Runtime()
fq := taskqueue.New(tr.EventLoop.RegisterCallback)
fq2 := taskqueue.New(tr.EventLoop.RegisterCallback)
var i int
incrimentI := func() { i++ }
var j int
Expand Down Expand Up @@ -112,12 +100,12 @@ func TestTwoTaskQueues(t *testing.T) {
fq2.Close()
}()

err := loop.Start(func() error {
_, err := vu.Runtime().RunString(`a()`)
err := tr.EventLoop.Start(func() error {
_, err := tr.VU.Runtime().RunString(`a()`)
return err
})
require.NoError(t, err)
loop.WaitOnRegistered()
tr.EventLoop.WaitOnRegistered()
require.Equal(t, i, k+j)
require.Greater(t, k, 100)
require.Greater(t, j, 100)
Expand Down

0 comments on commit 6f22885

Please sign in to comment.