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

BOT-296/Server Modules #313

Merged
merged 81 commits into from
Aug 23, 2017
Merged

Conversation

Mecharyry
Copy link
Contributor

Problem

A majority, if not all, of the Server logic is stored in a single file. This made testing the logic difficult because certain scenarios had to be built from a sequence of user actions.

Solution

Split the Server into testable modules that use mocks for all dependencies.

  • Router determines whether a given client, human or bot, is able to connect to the server. A human client is passed off to the BotLocator. Conversely, a bot is granted immediate access.
  • BotLocator finds and assigns a human to a bot if it is not already attached to another bot for a given room.
  • Mover sends a direction to all rooms that the given client belongs. In the case of a human, this will be the bot room directly.
  • ServerCreator handles the creation of the server and mainly passes off to collaborators given certain events.
  • Server entry point for node server, just used to create a default server using the ServerCreator.
  • ClientType js version of an enum that is used by the router to properly encapsulate the ClientType that is passed through the socket.query.
  • Observer to notify tests in scenarios where the ordinary socket lifecycle reporting cannot be used i.e. disconnection.

Additional changes

  • Each test that requires the use of a server creates and destroys as part of testing. Before you would manually run the server via the command line and then run the tests on top.
  • A mixture of single quotes and double quotes in js part, this has been aligned.
  • Dropped the use of prototype in favour of scoping correctly in the constructor of each "object", removing the confusing use of this everywhere.
  • Use mocha.expect BDD style assertions.

Ryan Feline added 30 commits July 8, 2017 20:25
@blundell
Copy link
Contributor

blundell commented Aug 4, 2017

what about @simonbrowndotje's Structurizer, i believe its free for open source projects?

or as step 1 just draw it (but dont' let S Brown see you 👁 )

}

module.exports = function(rooms, connectedClients) {
return new Disconnector(rooms, connectedClients);
Copy link
Contributor

@ouchadam ouchadam Aug 4, 2017

Choose a reason for hiding this comment

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

since you aren't retaining any state you don't reallly need to new these up.

If you want to keep and treat them like classes, you should probably use classes 👻

module.exports = class Disconnector {

    disconnectRoom(roomName) {
        this._anInternalMethod()
    }

    _anInternalMethod() {
    }

}

and then the usage convention is

const Disconnector = require('./disconnector')
const disconnector = new Disconnector()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah cool, I figured I'd learn something in the PR 😄

var roomsClientIsIn = Object.keys(clientsAndRooms[clientId]);

for(var i = 0; i < roomsClientIsIn.length; i++) {
emitter.to(roomsClientIsIn[i]).emit('direction', direction);
Copy link
Contributor

@ouchadam ouchadam Aug 4, 2017

Choose a reason for hiding this comment

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

this looks a little confusing, the room ids are keys in clientsAndRooms[clientId]?

Also because we're in JS land we can start dropping fori loops in favour of .map/.forEach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

roomsClientIsIn are the keys for the rooms that a given client belongs too. Yes this is confusing, I'll look into making this easier to read 👍

@Mecharyry
Copy link
Contributor Author

I'm going to make some changes as recommended by @ouchadam and will reopen after 😄

@Mecharyry Mecharyry closed this Aug 14, 2017
@Mecharyry Mecharyry reopened this Aug 23, 2017
@blundell
Copy link
Contributor

reopened = looks for diagrams...

@Mecharyry
Copy link
Contributor Author

Sorry, @blundell I don't have time to dedicate to looking into structurizer atm, I have a number of PRs that I would like to open following this one to improve the stability of the app. I've created an issue and this will be created before we do any sort of release of TpBot.

#319

@Mecharyry
Copy link
Contributor Author

Adam has spoken, down with vars!

@Mecharyry Mecharyry closed this Aug 23, 2017
@ouchadam
Copy link
Contributor

happy to ship after that!

@Mecharyry Mecharyry reopened this Aug 23, 2017
@Mecharyry
Copy link
Contributor Author

💥 @ouchadam

@ouchadam ouchadam merged commit d43a56b into tpbot-feature-branch Aug 23, 2017
@ouchadam ouchadam deleted the BOT-296/socket_io_client branch August 23, 2017 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants