Skip to content

Commit

Permalink
Handling undefined function in timers
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Jun 6, 2024
1 parent bd114fd commit 5819f62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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 {
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

0 comments on commit 5819f62

Please sign in to comment.