Refactor WebSocketClient in order to be more unit testable
Currently the WebSocketClient
unit tests are very dependent on timing rather than dependent on the state of the object. This is dangerous because running tests on different machines (like the build machines) vs local vs anywhere else might produce erroneous results.
We should refactor this class for the purposes of being able to unit test these better to ensure it is doing what it is supposed to be doing.
AC:
- Refactor
WebSocketClient
and associated unit tests inWebSocketAwsProductReceiverTest
to not be time dependent
Notes:
I think the single responsibility principle is important to keep in mind during this -> Is this class doing too much?
- It IS a @ClientEndpoint which means it handles websocket lifecycle methods (@onMessage, @onClose, etc)
- It contains a scheduled executor that runs its own
run()
method to ping/pong a remote server. If no response is given back in a certain amount of time it will close and try to reopen- Can this be taken out into a separate class responsible for that?
- This somewhat sounds like a variation of the Observer pattern
- A
WebSocketClient
(the publisher) can have-a PingPongMonitor (previously the Runnable part ofWebSocketClient
, now the subscriber) that can handle the internal states of the ping/pong responses. This way all of the logic for it is isolated into its own class for testing purposes rather than into theWebSocketClient
class
- Can this be taken out into a separate class responsible for that?
- The scheduled executor also is responsible for running a private
AnyMessageRunner
class that will disconnect/reconnect when it hasn't gotten any messages in a certain amount of time- In order to unit test this we send messages to the
@onMessage
handler and check to see what methods were called after a certain amount of time (Thread.wait()
), can this be changed to check state of the object instead of a time based approach? - Potentially a similar solution to the Ping/Pong checker above as well
- In order to unit test this we send messages to the
Edited by Hawthorne, Bryan D