Skip to content

Raise an error when WebSocket is not cleanly closed#80

Merged
baelter merged 2 commits intocloudamqp:mainfrom
ngbrown-forks:feature/error-on-websocket-close
Aug 23, 2023
Merged

Raise an error when WebSocket is not cleanly closed#80
baelter merged 2 commits intocloudamqp:mainfrom
ngbrown-forks:feature/error-on-websocket-close

Conversation

@ngbrown
Copy link
Copy Markdown
Contributor

@ngbrown ngbrown commented Aug 9, 2023

When a WebSocket connection is closed at the network level, there's no error raised by the AMQPWebSocketClient.

This change calls the onerror method when the connection is not cleanly closed. I switched to using addEventListener so the reject handler wouldn't be replaced by the new close handler before the openok message is received.

I can test this by restarting a websocket-relay container in the middle of a session.

I don't know how to automatically test this, as the WebSocket needs to be killed. Merely calling close on the socket would be considered a clean close.

Update: I updated both the WebSocket and Node.js socket clients to set closed to true when the underlying socket is closed.

@ngbrown ngbrown force-pushed the feature/error-on-websocket-close branch from 000c318 to 352a937 Compare August 10, 2023 16:13
@dentarg
Copy link
Copy Markdown
Member

dentarg commented Aug 10, 2023

I can test this by restarting a websocket-relay container in the middle of a session.

I don't know how to automatically test this, as the WebSocket needs to be killed. Merely calling close on the socket would be considered a clean close.

Perhaps it could be done with Toxiproxy: https://github.com/Shopify/toxiproxy#down

@ngbrown
Copy link
Copy Markdown
Contributor Author

ngbrown commented Aug 11, 2023

@dentarg Using Toxiproxy is a nice idea, but I ran into a couple issues.

  • The browser tests run entirely from the browser, and toxiproxy-node-client uses Node.js native methods, so that didn't work.
  • When I created a simple client in-test (copying from an existing branch), I was stopped by the lack of Access-Control-Allow-Origin headers.
  • Getting pass that, I would get blocked again because the Toxiproxy specifically blocks browsers from the API.
  • I don't see any obvious escape hatch in the implementation of @vitest/browser to run code on the Node.js side.

Saying all that, there is still playwright and a web app could be spun up, "clicked" through, and then shut down, but that would require even more files and setup. Or someone could take on creating and hosting a custom build of Toxiproxy.

I'm happy with just manually testing part of this change.

The branch with toxiproxy is here if anyone would like to play more with it: https://github.com/ngbrown-forks/amqp-client.js/tree/error-on-websocket-close-toxiproxy

@dentarg
Copy link
Copy Markdown
Member

dentarg commented Aug 11, 2023

@ngbrown Thanks for trying, I don't see a way either, not until vitest-dev/vitest#3758 has landed so we can use https://playwright.dev/docs/emulation#user-agent from vitest. Looks like it is possible to make requests with mode no-cors (picked that up from https://hackerone.com/reports/236349 linked in Shopify/toxiproxy#219). Oh well :)

@ngbrown
Copy link
Copy Markdown
Contributor Author

ngbrown commented Aug 18, 2023

Is there anything else that needs done for this pull request?

@baelter baelter merged commit db2d874 into cloudamqp:main Aug 23, 2023
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

Successfully merging this pull request may close these issues.

3 participants