Skip to content

Commit

Permalink
Updated README, added test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
rajivramv committed Apr 7, 2016
1 parent 4e5b8e4 commit 3321e7f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ A simple Node.js chat application
4. You can now connect to the server using the command `nc <host-ip> 3000`.

## Client
1. Instead of netcat, you can connect to the server using the chatter-box client using the command `node chat-client.js`
1. Instead of netcat, you can connect to the server using the chatter-box client using the command `node chat-client.js`.
6 changes: 3 additions & 3 deletions chat-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ function broadcastUserJoining(newUserNickName){
}

function sendUserNickNameList(){
var nickNames = [],
keysIter = users.keys(),
i = 0;
var nickNames = [],
keysIter = users.keys(),
i = 0;
for (;i<users.size;i++){
nickNames.push(keysIter.next().value);
}
Expand Down
59 changes: 59 additions & 0 deletions test/load-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var net = require('net');

// Some parameters, good practice to declare them as const
const parallelConnections = 1000,
timeout = 30000;

// describe is a function made available in the global scope when you run this test as 'mocha load-test.js'
describe('Chat server: load test', function(done){

// This ensures that the mocha tests don't timeout
this.timeout = 30000;

// Some variables that we will be using
var clients = new Map(), connectedClients = 0;

// This before hook lets you run a piece of code before all the test cases in this describe block
before('Make parallel connections', function(done){

// Iterate as many times as the number of parallel connections to make
for (var i = parallelConnections; i > 0; i--)

// Connect to the server
var client = net.connect(...);

// Usual stuff, *once* the client connects, handle the initial handshake and login protocol
client.once('connect', handleChatLoginProtocol);

// I have just chosen this namespaced (generally important) custom event to notify completion of protocol
// Also note that this 'subscriber pattern' allows me to avoid complex code to call done() once ALL the clients have connected
client.once('GQMe-login-protocol-completed', handleLogin);

// You can use an array instead of a Map object.
clients.set(i, client);

});

// Remember that you need to call done() only after we know for certain that all the clients have completed their login protocol.
function handleLogin(){
if (++connectedClients === parallelConnections) done();
}

// When you reach here, you should be having clients variable filled with logged in clients. Go ahead and have fun!
// I have added a few test cases to help you get started
it('should be able to send messages from many clients to a single client');
it('should be able to send a message to a client when that client is sending a message');

})

function handleChatLoginProtocol(){

// Note that the keyword 'this' in this function when evoked as a 'connect' event handler
// will refer to the socket which is a stream as well as an event emitter. Hence you can do this.write(..), this.on(...), this.once(...), this.emit(...) etc.
// to help with handling the protcol.

// IMPORTANT: Once you complete handling the handshake and login, remember to emit 'GQMe-login-protocol-completed'

...

}

0 comments on commit 3321e7f

Please sign in to comment.