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

[ QUESTION ] - Behavior of event's callbacks on the main event loop #6

Open
saniyar-dev opened this issue Feb 17, 2025 · 1 comment
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested

Comments

@saniyar-dev
Copy link
Owner

Description:
I have a question for myself that i don't know the answer yet, so i decided to make an issues for it so everyone can share their point of view and ideas on it.

The implementation now enable users to listen on events and have their callbacks to a specific event, for example responseReceived is an event which fires its associated callbacks when a Client Object make a Request and receives a Response in order to take an action on response asynchronously.
This particular event responseReceived and all other types of events, when fires, they actually queue their callbacks on the main event loop that VU has and running on using this code:

	enqCallback := c.Vu.RegisterCallback()

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

The real question is that we need to have a different event loop for events on each Object like Client from the event loop used for VU or we don't need this.

Current behavior

import { Client } from 'k6/x/net/http';

export default async function () {
  const client = new Client({
    proxy: 'http://127.0.0.1:1080',
    headers: { 'User-Agent': 'saniyar' },  // set some global headers
  });


  client.on('responseReceived', async e => {
    // This happens next
    console.log(await e.json())
  });

  await client.get('https://httpbin.test.k6.io/get');
  // This happens first
  console.log("first")
}

In this example even though we do await for client execution to be finished. The rest of code would occur first and then the callbacks on events that happened on client would be called.

Opposite behavior
Should we have this?

import { Client } from 'k6/x/net/http';

export default async function () {
  const client = new Client({
    proxy: 'http://127.0.0.1:1080',
    headers: { 'User-Agent': 'saniyar' },  // set some global headers
  });


  client.on('responseReceived', async e => {
    // This happens first
    console.log(await e.json())
  });

  await client.get('https://httpbin.test.k6.io/get');
  // This happens next
  console.log("second")
}

In this example it shows that finishing the execution of client.get() means that finishing the executions of its events callbacks too!

Additional context
If you have an idea for the behavior feel free to share it here in comments. Also it would be great if you have an idea for implementing the desired behavior you mentioning.

@saniyar-dev saniyar-dev added bug Something isn't working help wanted Extra attention is needed question Further information is requested labels Feb 17, 2025
@saniyar-dev
Copy link
Owner Author

The implementation process of event based handling response and etc would be tracked on issue #4, I highly recommend to read this issue first and then think about this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant