Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
test maintaince
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Dec 11, 2023
1 parent b7ad6bc commit 7c7882a
Showing 1 changed file with 29 additions and 53 deletions.
82 changes: 29 additions & 53 deletions timers/timers_test.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,60 @@
package timers

import (
"context"
"testing"
"time"

"github.com/dop251/goja"
"github.com/stretchr/testify/require"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/eventloop"
"go.k6.io/k6/js/modulestest"
)

func TestSetTimeout(t *testing.T) {
t.Parallel()
rt := goja.New()
vu := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: context.Background(),
StateField: nil,
}
runtime := modulestest.NewRuntime(t)
err := runtime.SetupModuleSystem(map[string]any{"k6/x/timers": New()}, nil, nil)
require.NoError(t, err)

m, ok := New().NewModuleInstance(vu).(*Timers)
require.True(t, ok)
rt := runtime.VU.Runtime()
var log []string
require.NoError(t, rt.Set("timers", m.Exports().Named))
require.NoError(t, rt.Set("print", func(s string) { log = append(log, s) }))
loop := eventloop.New(vu)
vu.RegisterCallbackField = loop.RegisterCallback

err := loop.Start(func() error {
_, err := vu.Runtime().RunString(`
timers.setTimeout(()=> {
print("in setTimeout")
})
print("outside setTimeout")
`)
return err
})
_, err = runtime.RunOnEventLoop(`
let timers = require("k6/x/timers");
timers.setTimeout(()=> {
print("in setTimeout")
})
print("outside setTimeout")
`)
require.NoError(t, err)
require.Equal(t, []string{"outside setTimeout", "in setTimeout"}, log)
}

func TestSetInterval(t *testing.T) {
t.Parallel()
rt := goja.New()
vu := &modulestest.VU{
RuntimeField: rt,
InitEnvField: &common.InitEnvironment{},
CtxField: context.Background(),
StateField: nil,
}
runtime := modulestest.NewRuntime(t)
err := runtime.SetupModuleSystem(map[string]any{"k6/x/timers": New()}, nil, nil)
require.NoError(t, err)

m, ok := New().NewModuleInstance(vu).(*Timers)
require.True(t, ok)
rt := runtime.VU.Runtime()
var log []string
require.NoError(t, rt.Set("timers", m.Exports().Named))
require.NoError(t, rt.Set("print", func(s string) { log = append(log, s) }))
require.NoError(t, rt.Set("sleep10", func() { time.Sleep(10 * time.Millisecond) }))
loop := eventloop.New(vu)
vu.RegisterCallbackField = loop.RegisterCallback

err := loop.Start(func() error {
_, err := vu.Runtime().RunString(`
var i = 0;
let s = timers.setInterval(()=> {
sleep10();
if (i>1) {
print("in setInterval");
timers.clearInterval(s);
}
i++;
}, 1);
print("outside setInterval")
`)
return err
})
_, err = runtime.RunOnEventLoop(`
let timers = require("k6/x/timers");
var i = 0;
let s = timers.setInterval(()=> {
sleep10();
if (i>1) {
print("in setInterval");
timers.clearInterval(s);
}
i++;
}, 1);
print("outside setInterval")
`)
require.NoError(t, err)
require.True(t, len(log) > 2)
require.Greater(t, len(log), 2)
require.Equal(t, "outside setInterval", log[0])
for i, l := range log[1:] {
require.Equal(t, "in setInterval", l, i)
Expand Down

0 comments on commit 7c7882a

Please sign in to comment.