Skip to content

Commit

Permalink
Move WaitForEventOption parsing to mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Nov 5, 2024
1 parent e2fb899 commit 738de64
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 53 deletions.
50 changes: 45 additions & 5 deletions browser/browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"time"

"github.com/grafana/sobek"

Expand Down Expand Up @@ -111,11 +112,11 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
},
"waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) {
ctx := vu.Context()
popts := common.NewWaitForEventOptions(
bc.Timeout(),
)
if err := popts.Parse(ctx, optsOrPredicate); err != nil {
return nil, fmt.Errorf("parsing waitForEvent options: %w", err)
rt := k6ext.Runtime(ctx)

popts, err := parseWaitForEventOptions(rt, optsOrPredicate, bc.Timeout())
if err != nil {
return nil, fmt.Errorf("parsing wait for event options: %w", err)
}

return k6ext.Promise(ctx, func() (result any, reason error) {
Expand Down Expand Up @@ -207,3 +208,42 @@ func parseGrantPermissionOptions(ctx context.Context, opts sobek.Value) (common.

return g, nil
}

// parseWaitForEventOptions parses optsOrPredicate into a WaitForEventOptions.
// It returns a WaitForEventOptions with the default timeout if optsOrPredicate is nil,
// or not a callable predicate function.
// It can parse only a callable predicate function or an object which contains a
// callable predicate function and a timeout.
func parseWaitForEventOptions(
rt *sobek.Runtime, optsOrPredicate sobek.Value, defaultTime time.Duration,
) (*common.WaitForEventOptions, error) {
w := &common.WaitForEventOptions{
Timeout: defaultTime,
}

if !sobekValueExists(optsOrPredicate) {
return w, nil
}
var isCallable bool
w.PredicateFn, isCallable = sobek.AssertFunction(optsOrPredicate)
if isCallable {
return w, nil
}

opts := optsOrPredicate.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "predicate":
w.PredicateFn, isCallable = sobek.AssertFunction(opts.Get(k))
if !isCallable {
return nil, errors.New("predicate function is not callable")
}
case "timeout":
w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
default:
return nil, fmt.Errorf("unknown option: %s", k)
}
}

return w, nil
}
10 changes: 5 additions & 5 deletions browser/sync_browser_context_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func syncMapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //n
"setOffline": bc.SetOffline,
"waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) {
ctx := vu.Context()
popts := common.NewWaitForEventOptions(
bc.Timeout(),
)
if err := popts.Parse(ctx, optsOrPredicate); err != nil {
return nil, fmt.Errorf("parsing waitForEvent options: %w", err)
rt := k6ext.Runtime(ctx)

popts, err := parseWaitForEventOptions(rt, optsOrPredicate, bc.Timeout())
if err != nil {
return nil, fmt.Errorf("parsing wait for event options: %w", err)
}

return k6ext.Promise(ctx, func() (result any, reason error) {
Expand Down
43 changes: 0 additions & 43 deletions common/browser_context_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package common

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -104,48 +103,6 @@ type WaitForEventOptions struct {
PredicateFn sobek.Callable
}

// NewWaitForEventOptions created a new instance of WaitForEventOptions with a
// default timeout.
func NewWaitForEventOptions(defaultTimeout time.Duration) *WaitForEventOptions {
return &WaitForEventOptions{
Timeout: defaultTimeout,
}
}

// Parse will parse the options or a callable predicate function. It can parse
// only a callable predicate function or an object which contains a callable
// predicate function and a timeout.
func (w *WaitForEventOptions) Parse(ctx context.Context, optsOrPredicate sobek.Value) error {
if !sobekValueExists(optsOrPredicate) {
return nil
}

var (
isCallable bool
rt = k6ext.Runtime(ctx)
)

w.PredicateFn, isCallable = sobek.AssertFunction(optsOrPredicate)
if isCallable {
return nil
}

opts := optsOrPredicate.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "predicate":
w.PredicateFn, isCallable = sobek.AssertFunction(opts.Get(k))
if !isCallable {
return errors.New("predicate function is not callable")
}
case "timeout": //nolint:goconst
w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
}
}

return nil
}

// GrantPermissionsOptions is used by BrowserContext.GrantPermissions.
type GrantPermissionsOptions struct {
Origin string
Expand Down

0 comments on commit 738de64

Please sign in to comment.