avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Restrict shared workers to only work when tests are executing in worker threads #2705

Closed novemberborn closed 3 years ago

novemberborn commented 3 years ago

Now that https://github.com/avajs/ava/pull/2690 has landed, we should update our shared worker implementation to only work when worker threads are used. It'll be faster, since the shared worker can talk directly to the test worker; we can transfer array buffers and even use shared memory.

novemberborn commented 3 years ago

@dnlup maybe you'd be interested in this one? 😉

dnlup commented 3 years ago

What strategy should we implement?

novemberborn commented 3 years ago

Ah yes. Let's throw an error, like we already do when there is a version mismatch:

https://github.com/avajs/ava/blob/0e5cc7dc2b1512d0abe9bfba57b26888eedea0cf/lib/worker/plugin.js#L86

dnlup commented 3 years ago

I'll try to work on it

novemberborn commented 3 years ago

Awesome!

In terms of shared memory, I was thinking of a chaining API like .sharing(buffer1, buffer2).publish({buffer1, buffer2}). That would work for .broadcast() as well.

When transferring memory that isn't sensible when broadcasting, so there could be a .transferring(buffer1, buffer2).publish({buffer1, buffer2}) API.

See:

https://github.com/avajs/ava/blob/0e5cc7dc2b1512d0abe9bfba57b26888eedea0cf/plugin.d.ts#L15

https://github.com/avajs/ava/blob/0e5cc7dc2b1512d0abe9bfba57b26888eedea0cf/plugin.d.ts#L35

https://github.com/avajs/ava/blob/0e5cc7dc2b1512d0abe9bfba57b26888eedea0cf/plugin.d.ts#L41

But perhaps that could be follow-up work.

dnlup commented 3 years ago

Awesome!

In terms of shared memory, I was thinking of a chaining API like .sharing(buffer1, buffer2).publish({buffer1, buffer2}). That would work for .broadcast() as well.

When transferring memory that isn't sensible when broadcasting, so there could be a .transferring(buffer1, buffer2).publish({buffer1, buffer2}) API.

See:

https://github.com/avajs/ava/blob/0e5cc7dc2b1512d0abe9bfba57b26888eedea0cf/plugin.d.ts#L15

https://github.com/avajs/ava/blob/0e5cc7dc2b1512d0abe9bfba57b26888eedea0cf/plugin.d.ts#L35

https://github.com/avajs/ava/blob/0e5cc7dc2b1512d0abe9bfba57b26888eedea0cf/plugin.d.ts#L41

But perhaps that could be follow-up work.

I'll see if I can put something together. I have to document myself a bit about shared memory 😉

dnlup commented 3 years ago

Is the sharing/transferring memory operation stateful? Like, once called, should the memory be stored in the object (so only a single call to sharing/transfering is necessary) or has it to be coupled just with the single publish operation?

novemberborn commented 3 years ago

Regular array buffers can only be transferred once. Shared array buffers can be shared multiple times.

I might be overthinking it, we're really exposing https://nodejs.org/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist but it'd be easier to communicate the restrictions through a chaining based API.