Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling undefined function in timers #3776

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions js/modules/k6/timers/timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
package timers

import (
"fmt"
"time"

"github.com/mstoykov/k6-taskqueue-lib/taskqueue"
"github.com/sirupsen/logrus"

"github.com/grafana/sobek"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
)

Expand Down Expand Up @@ -126,6 +128,15 @@ func (e *Timers) timerInitialization(
timeout = 0
}

name := setTimeoutName
if repeat {
name = setIntervalName
}

if callback == nil {
joanlopez marked this conversation as resolved.
Show resolved Hide resolved
common.Throw(e.vu.Runtime(), fmt.Errorf("%s's callback isn't a callable function", name))
}

task := func() error {
// Specification 8.1: If id does not exist in global's map of active timers, then abort these steps.
if _, exist := e.timers[id]; !exist {
Expand All @@ -147,11 +158,6 @@ func (e *Timers) timerInitialization(
return err
}

name := setTimeoutName
if repeat {
name = setIntervalName
}

e.runAfterTimeout(&timer{
id: id,
task: task,
Expand Down
11 changes: 11 additions & 0 deletions js/modules/k6/timers/timers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ func TestSetTimeout(t *testing.T) {
require.Equal(t, []string{"outside setTimeout", "in setTimeout"}, log)
}

func TestSetUndefinedFunction(t *testing.T) {
t.Parallel()

runtime := newRuntime(t)
_, err := runtime.RunOnEventLoop(`
let timers = require("k6/x/timers");
timers.setTimeout(undefined)
`)
require.Error(t, err, "setTimeout's callback isn't a callable function")
}

func TestSetInterval(t *testing.T) {
t.Parallel()
runtime := newRuntime(t)
Expand Down
Loading