Skip to content

Add sockets.js tests and minor fixes#50

Merged
cereallarceny merged 8 commits intoOpenMined:grid-syftfrom
vvmnnnkv:socket-tests
Sep 4, 2019
Merged

Add sockets.js tests and minor fixes#50
cereallarceny merged 8 commits intoOpenMined:grid-syftfrom
vvmnnnkv:socket-tests

Conversation

@vvmnnnkv
Copy link
Member

@vvmnnnkv vvmnnnkv commented Sep 2, 2019

Fixes #47

@cereallarceny cereallarceny self-requested a review September 2, 2019 11:40
@cereallarceny
Copy link
Member

Screen Shot 2019-09-02 at 12 41 22 PM

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?

@codecov
Copy link

codecov bot commented Sep 2, 2019

Codecov Report

❗ No coverage uploaded for pull request base (grid-syft@da31141). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             grid-syft      #50   +/-   ##
============================================
  Coverage             ?   30.76%           
============================================
  Files                ?        2           
  Lines                ?      143           
  Branches             ?       13           
============================================
  Hits                 ?       44           
  Misses               ?       89           
  Partials             ?       10
Impacted Files Coverage Δ
src/sockets.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update da31141...9346128. Read the comment docs.

src/sockets.js Outdated
return new Promise((resolve, reject) => {
data.instanceId = this.instanceId;
// shallow copy + instanceId
let _data = Object.assign(
Copy link
Member

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?

Copy link
Member Author

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}?

const onOpen = event => console.log('NEW OPEN', event);
const onClose = event => console.log('NEW CLOSE', event);
const onError = event => console.log('NEW ERROR', event);
/**
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK

* @param event Event name
* @returns {Promise}
*/
function makeEventPromise(emitter, event) {
Copy link
Member

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) { ... };

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

* @param event Event name
* @returns {Promise}
*/
function makeEventPromise(emitter, event) {
Copy link
Member

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?

Copy link
Member Author

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');

*/
function makeEventPromise(emitter, event) {
let resolver;
let promise = new Promise(resolve => resolver = resolve);
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

}

describe('Sockets', () => {
// common test objects
Copy link
Member

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. :)

Copy link
Member Author

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('can communicate with a socket server', async () => {
const message = { message: 'This is a test' };
test('keep-alive messages', async() => {
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

onError: event => onError(event)
keepAliveTimeout
});
let serverSocket = await mockServer.connected;
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

// one message is sent right after establishing the connection hence +1
// (do we really need that?)
expect(messages).toHaveLength(expectedMessagesCount + 1);
let expectedTypes =[];
Copy link
Member

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.

Copy link
Member Author

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
Copy link
Member

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 keepAlive = () => {
const timeout = 20000;
const timeout = keepAliveTimeout || 20000;
Copy link
Member

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.

@cereallarceny cereallarceny merged commit 533ab48 into OpenMined:grid-syft Sep 4, 2019
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.

2 participants