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

Advice regarding indeterminate callbacks #1964

Closed
natzcam opened this issue Oct 22, 2018 · 6 comments
Closed

Advice regarding indeterminate callbacks #1964

natzcam opened this issue Oct 22, 2018 · 6 comments

Comments

@natzcam
Copy link

natzcam commented Oct 22, 2018

How best to approach this?

test.cb('2 callbacks', t => {
    t.plan(2);

    const alice = new Alice();
    alice.on('connect', () => {
        t.pass();
        t.end(); //?
    });

    const bob = new Bob();
    bob.on('connect', () => {
        t.pass();
        t.end(); //?
    });
});

There is no guarantee which connect callback will be called first, but I need to assert that both got called. Promise.all could solve this but are there any alternatives?

@havenchyk
Copy link

@natzcam I don't know the answer except using Promise.all, but the best place for asking such questions is https://spectrum.chat/ava or stackoverflow.

Btw, if you could split this logic into 2 tests it would be easier to track/manipulate from the first sight.

@novemberborn
Copy link
Member

You could also count how many connects are still pending, decrement on each connect and within that callback if you hit zero, call t.end().

None of this helps if a callback is invoked twice though.

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

@novemberborn
Copy link
Member

You could also count how many connects are still pending, decrement on each connect and within that callback if you hit zero, call t.end().

None of this helps if a callback is invoked twice though.

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

@sindresorhus
Copy link
Member

You could also use p-event for this:

import test from 'ava';
import pEvent from 'p-event';

test('2 callbacks', async t => {
	const alice = new Alice();
	await pEvent(alice, 'connect');

	const bob = new Bob();
	await pEvent(bob, 'connect');

	t.pass();
});

@natzcam
Copy link
Author

natzcam commented Oct 23, 2018

Tried above, but the test would not complete because connection is initiated on the constructor and await pEvent(alice, 'connect'); would block new Bob(). Changing the order did not help either, because 'connect' might have been fired already.

  const alice = new Alice();
  const bob = new Bob();
  await pEvent(alice, 'connect');
  await pEvent(bob, 'connect');
  t.pass();

Using Promise.all would solve it though, but it would circle back to my original question. :)

  await Promise.all([pEvent(alice, 'connect'), pEvent(bob, 'connect')]);
  t.pass();

It would be nice, for callbacks, there is a 'countdown' functionality. Wherein, the test would end when the countdown = 0

test.cb('2 callbacks', t => {
    t.plan(2);
    t.countdown(2);

    const alice = new Alice();
    alice.on('connect', () => {
        t.pass();
        t.end(1); //decrement 1
    });

    const bob = new Bob();
    bob.on('connect', () => {
        t.pass();
        t.end(1); //decrement 1
    });
});

@novemberborn
Copy link
Member

I think that's functionality one could add atop of AVA once #1692 lands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants