-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add basic event loop with some API to be used by modules #2228
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eb97f4b
Add basic event loop with an API to be used by modules
mstoykov bc6346a
setTimeout: check the first argument is present and callable
mstoykov 29af305
Move setTimeout out of the global scope
mstoykov 8052e3a
Add k6/experimental module and move setTimeout to it
mstoykov 494e006
Copy experimental godoc notice from grpc on RegisterCallback
mstoykov 893fed9
Don't mark iterations with exceptions as not full
mstoykov d148447
Always cancel the iteration context and wait on loop to empty
mstoykov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Package experimental includes experimental module features | ||
package experimental | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/dop251/goja" | ||
"go.k6.io/k6/js/common" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
type ( | ||
// RootModule is the root experimental module | ||
RootModule struct{} | ||
// ModuleInstance represents an instance of the experimental module | ||
ModuleInstance struct { | ||
vu modules.VU | ||
} | ||
) | ||
|
||
var ( | ||
_ modules.Module = &RootModule{} | ||
_ modules.Instance = &ModuleInstance{} | ||
) | ||
|
||
// NewModuleInstance implements modules.Module interface | ||
func (*RootModule) NewModuleInstance(m modules.VU) modules.Instance { | ||
return &ModuleInstance{vu: m} | ||
} | ||
|
||
// New returns a new RootModule. | ||
func New() *RootModule { | ||
return &RootModule{} | ||
} | ||
|
||
// Exports returns the exports of the experimental module | ||
func (mi *ModuleInstance) Exports() modules.Exports { | ||
return modules.Exports{ | ||
Named: map[string]interface{}{ | ||
"setTimeout": mi.setTimeout, | ||
}, | ||
} | ||
} | ||
|
||
func (mi *ModuleInstance) setTimeout(f goja.Callable, t float64) { | ||
if f == nil { | ||
common.Throw(mi.vu.Runtime(), errors.New("setTimeout requires a function as first argument")) | ||
} | ||
// TODO maybe really return something to use with `clearTimeout | ||
// TODO support arguments ... maybe | ||
runOnLoop := mi.vu.RegisterCallback() | ||
go func() { | ||
timer := time.NewTimer(time.Duration(t * float64(time.Millisecond))) | ||
select { | ||
case <-timer.C: | ||
runOnLoop(func() error { | ||
_, err := f(goja.Undefined()) | ||
return err | ||
}) | ||
case <-mi.vu.Context().Done(): | ||
// TODO log something? | ||
|
||
timer.Stop() | ||
runOnLoop(func() error { return nil }) | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we have it as an explicit experiment could we add the expected
timeoutID
as returned value and set it always to zero?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I plan to make a PR with my PoC from #2228 (comment) tomorrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if we leave this for the next release. Given that this won't be used by anyone.
We can have this changes in :
but I would argue we would probably want them globally available by next release.
Adding unnecessary changes days before cutoff IMO just adds more ways for this to break. We currently only care about this call in order to test the event loop - anyone actually wanting to use it - shouldn't