Skip to content

reopened server show more detail error msg#2415

Merged
mcollina merged 2 commits intofastify:masterfrom
zhangwinning:bug/reopenServer
Jul 16, 2020
Merged

reopened server show more detail error msg#2415
mcollina merged 2 commits intofastify:masterfrom
zhangwinning:bug/reopenServer

Conversation

@zhangwinning
Copy link
Copy Markdown
Contributor

@zhangwinning zhangwinning commented Jul 16, 2020

Checklist

Fix #2411

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

@zhangwinning
Copy link
Copy Markdown
Contributor Author

cc @mcollina, thanks

Copy link
Copy Markdown
Member

@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.

Good work! Just a nit.

t.is(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
t.ok(err)
})
})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

would you mind to use async/await in the test?

Copy link
Copy Markdown
Contributor Author

@zhangwinning zhangwinning Jul 16, 2020

Choose a reason for hiding this comment

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

thanks @mcollina.
I try use async/await, while when I modify code below it, show error for it tapError: incorrect number of tests, plan test is 2, I dig it for a while, but not solved. please told me where is errors.


test('Cannot be reopened the closed server has listen callback', async t => {
  t.plan(2)
  const fastify = Fastify()

  await fastify.listen(0)
  await fastify.close()

  fastify.listen(0, (err) => {
    t.is(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
    t.ok(err)
  })
})

Copy link
Copy Markdown
Member

@Eomm Eomm Jul 16, 2020

Choose a reason for hiding this comment

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

you must write

try{
  await fastify.listen(0)
}catch(err){
    t.is(err.code, 'FST_ERR_REOPENED_CLOSE_SERVER')
    t.ok(err)
}

(the promise of the test ends, so it is resolved as undefined and the callback of the fastify.listen is ignored even if it is executed)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hey @Eomm thanks for the answer.

The test unit is listen has callback . if I follow the code about 👆,I don't test code logic. https://github.com/fastify/fastify/pull/2415/files#diff-c945a46d13b34fcaff544d966cffcabaR138.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ops 😅

Then you can just wrap the callback in a new Promise 👍🏼

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks @Eomm, I got it

Copy link
Copy Markdown
Member

@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

@zhangwinning zhangwinning requested a review from mcollina July 16, 2020 14:12
@Eomm Eomm added backport 2.x Issue or pr that should be backported to Fastify v2 bugfix Issue or PR that should land as semver patch labels Jul 16, 2020
@mcollina mcollina merged commit e53abd1 into fastify:master Jul 16, 2020
@zhangwinning zhangwinning deleted the bug/reopenServer branch July 16, 2020 15:15
@github-actions
Copy link
Copy Markdown

github-actions bot commented Feb 9, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

backport 2.x Issue or pr that should be backported to Fastify v2 bugfix Issue or PR that should land as semver patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot listen on a port after closing the server. Error is misleading.

3 participants