Skip to content

node:events once isn't working as expected in node-redis tests #54497

@sjpotter

Description

@sjpotter

Version

v18.19.1

Platform

Linux spotterT490 6.8.0-38-generic #38-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun  7 15:25:01 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

node:events

What steps will reproduce the bug?

In node-redis we fire events (such as a connection being made to a server 'connect', the connection being ready 'ready', and the connection being shutdown, 'end') amongst others. In practice, these are proxied to the user from an underlying object.

see - https://github.com/redis/node-redis/blob/949b944b0f01fa207980b5aa925590d7c32f6073/packages/client/lib/client/index.ts#L438-L464

We have a test to make sure these fires correctly, but that test is failing at the moment in a manner that we can't figure out (but can play around with to get to work, which makes us scratch our head even more)

test - https://github.com/redis/node-redis/blob/949b944b0f01fa207980b5aa925590d7c32f6073/packages/client/lib/client/index.spec.ts#L119-L133

If I rewrite the test to be

    const p1 = once(client, 'connect');
    const p2 = once(client, 'ready');
    const p3 = once(client, 'end');

    await Promise.all([
      p1,
      p2,
      client.connect()
    ]);

    await Promise.all([
      p3,
      client.close()
    ]);

the test reliably passes as the once receives the 'end' event that it expects. However, if I rewrite it to be

    const p1 = once(client, 'connect');
    const p2 = once(client, 'ready');

    await Promise.all([
      p1,
      p2,
      client.connect()
    ]);

    const p3 = once(client, 'end');

    await Promise.all([
      p3,
      client.close()
    ]);

the test now reliably fails again.

In the code in index.ts (that I linked to above) where we listen and refire the events, for 'end' I stuck in a console.log to indicate that the event is being received, and it prints out as expected always, i just don't receive the 'end' event that should be fired unless the test is structured as noted above.

I'm very confused.

How often does it reproduce? Is there a required condition?

always as noted

What is the expected behavior? Why is that the expected behavior?

the promise that events.once() returns should be satisfied.

What do you see instead?

event promise not being satisfied.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to the events subsystem / EventEmitter.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions