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

Close server when rtm.stopServer() is invoked #26

Merged
merged 4 commits into from Mar 16, 2018

Conversation

finferflu
Copy link
Contributor

@finferflu finferflu commented Mar 6, 2018

Fixes issue #25.
I’m not sure this is the most appropriate way to fix the issue, but it does the job!

@coveralls
Copy link

coveralls commented Mar 7, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 5a21342 on finferflu:fix-rtm-socket into 6bee3a0 on Skellington-Closet:master.

@colestrode
Copy link
Member

Thanks for finding the issue and submitting the PR! This looks like the right solution, I added a few comments to tighten things up, but this looks like a great start!

@@ -85,6 +85,7 @@ rtm.stopServer = function (token) {
return new Promise((resolve, reject) => {
const wss = wssServers.get(token)
if (!wss) {
server.close()
Copy link
Member

Choose a reason for hiding this comment

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

server.close is async, we should resolveinside a callback to server.close. The callback will be invoked when all connections are closed. According to the docs:

[server.close] will be called with an Error as its only argument if the server was not open when it was closed.

so I think it's safe to always resolve regardless of errors.

Something like return server.close(() => resolve()) should be fine

@@ -96,6 +97,7 @@ rtm.stopServer = function (token) {

logger.debug(`server ${token} closed`)
wssServers.delete(token)
server.close()
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as above about resolving as a callback.


expressAppMock = {
listen: sinon.stub().returns(serverMock)
listen: sinon.stub().returns(serverMock),
close: serverMock.close()
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need this mock, close is only called on the server returned by listen, not on the expressApp.

@@ -33,10 +33,13 @@ describe('mocker: rtm', function () {
Server: sinon.stub()
}

serverMock = 'server'
serverMock = {
close: sinon.stub().returns()
Copy link
Member

Choose a reason for hiding this comment

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

let's add some assertions to make sure this is called correctly. You'll need to use yields since it is an async function invoking a callback.

@finferflu
Copy link
Contributor Author

Ah yes, I missed that server.close is async, my bad!

As for your other comments, I've never really used Mocha or Sinon, and you can tell that my solution was a bit rudimental :)

Anyway, thanks for the feedback, and I'm glad my solution helped a little bit :)

@finferflu
Copy link
Contributor Author

Ok, I've had another go, please let me know if you have any more feedback for improving things. Thanks!

Copy link
Member

@colestrode colestrode left a comment

Choose a reason for hiding this comment

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

I added a few assertions to the tests so we could confirm the server gets closed correctly. Gonna try to get this published today. Thanks for the filing the issue and the PR!

@colestrode
Copy link
Member

colestrode commented Mar 16, 2018

closes #25

@colestrode colestrode merged commit 3d04beb into Skellington-Closet:master Mar 16, 2018
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