Skip to content
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

refactor: move handlers to lib/handlers #1636

Merged
merged 17 commits into from
Jul 18, 2023
Merged

refactor: move handlers to lib/handlers #1636

merged 17 commits into from
Jul 18, 2023

Conversation

robertsLando
Copy link
Member

@robertsLando robertsLando commented Jul 14, 2023

  • Created client.log function that by default uses debug but can be overriden using options.log
  • Created client.nop
  • Moved all client._handle in lib/handlers
  • Create MqttClient.defaultId static method
  • Ensure shiftPingInterval is called every time a packet is written to the stream. Could fix MQTT keepalive less than 60s and chrome 88 intensive timer throttling #1257. An alternative could also to shift the ping interval every time a pingresp is received from server

@robertsLando robertsLando marked this pull request as draft July 14, 2023 10:04
@robertsLando robertsLando marked this pull request as ready for review July 14, 2023 11:52
@codecov
Copy link

codecov bot commented Jul 14, 2023

Codecov Report

Patch coverage: 89.33% and project coverage change: +0.10 🎉

Comparison is base (79b23a8) 86.28% compared to head (c26d1dd) 86.39%.

❗ Current head c26d1dd differs from pull request most recent head b2c88ab. Consider uploading reports for the commit b2c88ab to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1636      +/-   ##
==========================================
+ Coverage   86.28%   86.39%   +0.10%     
==========================================
  Files          13       19       +6     
  Lines        1254     1264      +10     
==========================================
+ Hits         1082     1092      +10     
  Misses        172      172              
Impacted Files Coverage Δ
lib/handlers/auth.js 5.26% <5.26%> (ø)
lib/handlers/index.js 83.33% <83.33%> (ø)
lib/handlers/publish.js 91.66% <91.66%> (ø)
lib/handlers/connack.js 93.10% <93.10%> (ø)
lib/client.js 93.29% <93.95%> (+2.12%) ⬆️
lib/handlers/ack.js 97.95% <97.95%> (ø)
lib/handlers/pubrel.js 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines +620 to 628
const opts = { host: 'localhost', port: ports.PORTAND115, protocolVersion: 5, properties: { authenticationMethod: 'json' }, authPacket: {} }
const client = mqtt.connect(opts)
server.once('client', function (c) {
console.log('server received client')
serverClient.on('auth', function (packet) {
c.on('auth', function (packet) {
console.log('serverClient received auth: packet %o', packet)
serverClient.end(done)
client.end(done)
})
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BertKleewein By checking all failed actions seems the only test that occasionally keeps failing was this one. Let's see if this change fixes that too

Copy link
Member Author

@robertsLando robertsLando Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope that doesn't fix the test neighter... I have a feel this is caused by a race condition, the auth packet could be sent even before the connack is received and the c.on('auth') listener may be set after the auth is received causing the test to fail

Maybe placing the writePacket call of authPacket inside a setImmediate or a nextTick could make it more reliable. What do you think?

robertsLando

This comment was marked as duplicate.

@robertsLando
Copy link
Member Author

@vishnureddy17 @BertKleewein Could you tell me your thoughts about this?

Once this is merged I will continue with #1637. Standard doesn't highlight errors like eslint, like undefined vars and others, that's absolutely bad and difficult to see at first sight expecially when refactoring things...

Copy link
Member

@vishnureddy17 vishnureddy17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, much needed refactor.

@BertKleewein
Copy link
Contributor

I think this is a great step toward breaking up the monolith that is client.js. I see a bunch of good changes in here and I'm curious about where you're going to take this. I especially like that you're taking small steps and keeping all of the functionality working as you move things around.

My big concern is about passing MqttClient as the first parameter of handlePacket -- You're physically separating the code by splitting it into multiple files, but all of those handlers still have access to everything inside MqttClient, including internals. This makes it hard to truly "separate" this code for verification. For example, I would love it if something like MqttClient.outgoing access was somehow scoped to the code that needs access to this list. That would allow us to easily mock this list for unit testing without needing to mock the entire MqttClient object. Heck, I would love to be able to unit test any of the handle*.js modules without needing to mock the client.

I realize that I'm talking about major changes here. My example is just one way that the code might evolve, if you happen to think the way that I do. My question is more curiosity about future direction and the sort of "architectural principles" that might be implied by this change.

@robertsLando
Copy link
Member Author

robertsLando commented Jul 18, 2023

@BertKleewein My idea right now is to make the code cleaner and easier to work with without breaking to much stuff. I understand what you mean and I think we can reach that goal, anyway mocking the client to unit test those handlers wouldn't be that hard right now, but I understand you would like to isolate the access to the client instance, like passing client.handlersScope instead of client in first arg, that's possible too but we can keep that for another PR :)

My next steps will be to remove standard with eslint+prettier (sure this will highlight lot of other bugs in code) and then thinking about moving to Typescript. For the second part what scares me most is rewriting tests... Converting all the files to typescript could require me a day but the tests are a lot. Anyone that could help me with that when I start?

The TS conversion should be clean, no breaking change, just keep the code as is but typed :) That will make much easier to work with the code base

@robertsLando robertsLando merged commit 8382a32 into main Jul 18, 2023
6 checks passed
@robertsLando robertsLando deleted the refactor-handlers branch July 18, 2023 06:33
@vishnureddy17
Copy link
Member

If tests need to be re-written, it might be worth considering using the built-in Node test runner instead of a third party one.

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.

MQTT keepalive less than 60s and chrome 88 intensive timer throttling
3 participants