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

fix: catch writeToStream errors #493

Merged
merged 2 commits into from May 25, 2020
Merged

fix: catch writeToStream errors #493

merged 2 commits into from May 25, 2020

Conversation

robertsLando
Copy link
Member

@mcollina this should fix unhandled expections when a non-valid packet is write to stream

@coveralls
Copy link

Pull Request Test Coverage Report for Build 111168815

  • -1 of 8 (87.5%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.09%) to 99.634%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/write.js 7 8 87.5%
Totals Coverage Status
Change from base Build 107989420: -0.09%
Covered Lines: 778
Relevant Lines: 779

💛 - Coveralls

@coveralls
Copy link

coveralls commented May 21, 2020

Pull Request Test Coverage Report for Build 114870214

  • 8 of 8 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0005%) to 99.725%

Totals Coverage Status
Change from base Build 107989420: 0.0005%
Covered Lines: 779
Relevant Lines: 779

💛 - Coveralls

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Adding a unit test (even with a mock/proxyquire) would be nice.

@robertsLando
Copy link
Member Author

robertsLando commented May 21, 2020

@mcollina I'm 👍 for the test but why this isn't working?

// Catch invalid packet writeToStream errors
test('reject client requested for unsupported protocol version', function (t) {
  t.plan(3)

  const broker = aedes()
  t.tearDown(broker.close.bind(broker))

  const s = setup(broker)

  var packet = [
    16, 22, 0, 4, 77, 81, 84, 84,
    4, 2, 0, 0, 0, 10, 109, 121,
    99, 108, 105, 101, 110, 116, 105, 100,
    85, 0
  ]

  s.inStream.write(Buffer.from(packet))

  s.outStream.on('data', function (packet) {
    t.fail('no data sent')
  })
  broker.on('connectionError', function (client, err) {
    t.equal(client.version, null)
    t.equal(err.message, 'Unknown command')
    t.equal(broker.connectedClients, 0)
  })
})

I mean, it works as test but it doesn't make that function throw

P.S: I asked to the guy of the email to try this fix and he told me that this has fixed the bug 🎉

@mcollina
Copy link
Collaborator

I mean require the function directly and mock everything.

@robertsLando
Copy link
Member Author

@mcollina I tried even that way and I'm not able to reproduce the problem, maybe I'm missing something

@mcollina
Copy link
Collaborator

You should use proxyquire to mock writeToStream.

@robertsLando
Copy link
Member Author

@mcollina I saw that if there are errors or client is no connecting/connected the write never calls done is this correct or should we also call done(err)?

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Collaborator

I saw that if there are errors or client is no connecting/connected the write never calls done is this correct or should we also call done(err)?

I do not know.

@robertsLando robertsLando merged commit 8d34ee5 into master May 25, 2020
@robertsLando robertsLando deleted the fixWriteBug branch May 25, 2020 15:29
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.

None yet

3 participants