Skip to content

Commit

Permalink
[ FIX ] queue response on the main event loop which the VU has
Browse files Browse the repository at this point in the history
  • Loading branch information
saniyar-dev committed Feb 17, 2025
1 parent 910cce7 commit b49fc94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 1 addition & 3 deletions examples/eventlistener.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ export default async function () {
// });

client.on('responseReceived', async e => {
console.log("event ran")
console.log(await e.json())
// const resp = await e.json()
// console.log(resp)
// const response = event.data;
// if (requestID && response.request.id == requestID) {
// // Change the request duration metric to any value
Expand All @@ -36,4 +33,5 @@ export default async function () {
});

await client.get('https://httpbin.test.k6.io/get');
console.log("first")
}
18 changes: 11 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ func (c *Client) callEventListeners(t string, obj *sobek.Object) error {
return nil
}

// func (c *Client) queueResponse(resp *response.Response) *sobek.Promise {
// }
// this function queueResponse for handling on the main event loop that the VU has
func (c *Client) queueResponse(resp *response.Response) {
enqCallback := c.Vu.RegisterCallback()

go func() {
enqCallback(func() error {
return c.callEventListeners(RESPONSE, resp.Obj)
})
}()
}

// this function would handle any type of request and do the actuall job of requesting
func (c *Client) do(req *request.Request) (*response.Response, error) {
Expand All @@ -95,11 +103,7 @@ func (c *Client) do(req *request.Request) (*response.Response, error) {
resp.Response = httpResp
helpers.Must(rt, resp.Define())

// TODO: make calling event listeners async
err = c.callEventListeners(RESPONSE, resp.Obj)
if err != nil {
return resp, err
}
c.queueResponse(resp)

return resp, nil
}
Expand Down

0 comments on commit b49fc94

Please sign in to comment.