-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Add sockets.js tests and minor fixes #50
Conversation
Nice work @vvmnnnkv! I noticed that the tests on TravisCI never actually finish. See the above screenshot. I believe this is from the socket never being cleaned up and thus, it continues to "ping". The screenshot doesn't show it, but the Travis test kept going for 40 minutes with that same message every 20 seconds until I shut it down. Can you clean that up and I'll take another look? |
# Conflicts: # src/sockets.js
Codecov Report
@@ Coverage Diff @@
## grid-syft #50 +/- ##
============================================
Coverage ? 30.76%
============================================
Files ? 2
Lines ? 143
Branches ? 13
============================================
Hits ? 44
Misses ? 89
Partials ? 10
Continue to review full report at Codecov.
|
src/sockets.js
Outdated
} | ||
|
||
send(type, data = {}) { | ||
return new Promise((resolve, reject) => { | ||
data.instanceId = this.instanceId; | ||
// shallow copy + instanceId | ||
let _data = Object.assign( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do it like this @vvmnnnkv? What was wrong with the old way this was being assigned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It had side effect of adding instanceId
property on the data
object passed into .send().
Another way could be sending instanceId
outside of data
?
{type, instanceId, data}
?
test/sockets.test.js
Outdated
const onOpen = event => console.log('NEW OPEN', event); | ||
const onClose = event => console.log('NEW CLOSE', event); | ||
const onError = event => console.log('NEW ERROR', event); | ||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not using JSDoc, feel free to throw these comments out for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
test/sockets.test.js
Outdated
* @param event Event name | ||
* @returns {Promise} | ||
*/ | ||
function makeEventPromise(emitter, event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always declare function in ES6 fashion:
const makeEventPromise = (emitter, event) { ... };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
test/sockets.test.js
Outdated
* @param event Event name | ||
* @returns {Promise} | ||
*/ | ||
function makeEventPromise(emitter, event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing: what does this function actually do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for convenience. It wraps event callback in a promise. The promise is resolved when the event is triggered.
E.g. when we want to await for established connection on the mock server:
const socket = await makeEventPromise(mockServer, 'connection');
test/sockets.test.js
Outdated
*/ | ||
function makeEventPromise(emitter, event) { | ||
let resolver; | ||
let promise = new Promise(resolve => resolver = resolve); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always use const
if you can. If you cannot, use let
. In this case, you should prefer const
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
test/sockets.test.js
Outdated
|
||
describe('Sockets', () => { | ||
// common test objects |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments should always start capitalized. They should also be fairly descriptive. To me, I don't think you need any comments in this file at all. It's a test file - so it should be "strictly business". Go ahead and remove them unless they're absolutely critical to the understanding of something complex. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, removed some comments
test/sockets.test.js
Outdated
jest.clearAllMocks(); | ||
}); | ||
|
||
test('can communicate with a socket server', async () => { | ||
const message = { message: 'This is a test' }; | ||
test('keep-alive messages', async() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test messages ('keep-alive messages'
) should read as a full sentence with the description. Our description (written above) is 'Sockets'
, so a full sentence should be 'Sockets can have a custom keep-alive timeout'
with 'Sockets'
being the description message (from the describe()
function) and 'can have a custom keep-alive timeout'
being the test message (from the test()
function).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
test/sockets.test.js
Outdated
}); | ||
let serverSocket = await mockServer.connected; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again... use const
, everywhere. This will be the last comment I write about in my review, but please change all references that can be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
test/sockets.test.js
Outdated
// one message is sent right after establishing the connection hence +1 | ||
// (do we really need that?) | ||
expect(messages).toHaveLength(expectedMessagesCount + 1); | ||
let expectedTypes =[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This spacing is incorrect. Is prettier running for you? Normally Prettier will fix things like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought prettier would run automatically :) Fixed
src/sockets.js
Outdated
onOpen, | ||
onClose, | ||
onMessage, | ||
keepAliveTimeout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it would be better if we did:
onClose,
onMessage,
keepAliveTimeout = 20000
You can assign defaults to your arguments this way, rather than doing it below (on line 18, which you can now remove).
src/sockets.js
Outdated
|
||
const socket = new WebSocket(url); | ||
|
||
const keepAlive = () => { | ||
const timeout = 20000; | ||
const timeout = keepAliveTimeout || 20000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this, see above comment.
Fixes #47