-
Notifications
You must be signed in to change notification settings - Fork 395
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
Improve two-way communication test #621
base: main
Are you sure you want to change the base?
Conversation
This commit improves the test to capture clone errors that occured in v4.3.1 (or lower) when trying to do two-way communication. To do this, we expose a NonCloneable object that contains a MessagePort. In v4.3.1 when `proxy` is called, the listener attached by `Comlink.expose` also gets called by the remote and does not perform an early return. Instead it attempt to do a postMessage with the raw value set to the NonCloneable instance. This throw an unhandledrejection. With this new test we verify that this does not happen.
PROXY = "PROXY", | ||
THROW = "THROW", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed those because they are unused. If you'd rather keep them, let me know and I will update my PR.
expect(await proxy(1, 3)).to.equal(5); | ||
// await new Promise((res) => res()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to replace expect(await proxy(1, 3)).to.equal(5);
with a simple await on an empty promise but that's not enough to see the error. I'm not exactly sure why.
let called = false; | ||
let notcalled = true; | ||
let error = ""; | ||
window.addEventListener("unhandledrejection", (ev) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's how we get notified in this test that Comlink try to do a postMessage
(from the Comlink.expose
event listener) that it shouldn't (but it does in v4.3.0 because of the overlap between WireValueType
and MessageType
).
In order to get an exception, we're forcing here Comlink to do a postMessage
on an object that can't be cloned.
Hello! 👋
TLDR;
This PR improves the two-way communication test to fail if
WireValueType
andMessageType
would happen to overlap again in the future.Full story
First, I want to thank you for your work on Comlink. This is one of the most elegant, well written library I've ever come across ⭐ 👏
I recently noticed that the test for two-way communication does not do enough work to actually fail in versions of
Comlink
< 4.3.1. I encountered this issue while trying to port two-way communication to our fork which is stuck at 4.3.0.To see how this PR helps you can use this bash script:
If you run this script you'll see this error in your console (you might need to scroll up a bit 📜):
If you run the script again but replace
BRANCH=fix-two-way-iframe-test
byorigin/main
(or equivalent), you can see that the test pass (you'll need to scroll up again to view the result of the firstkarma start
):This shows that the test is missing a few bits.