Created
January 22, 2015 03:52
-
-
Save jamesdavidson/f85c9eeeb12968d8de0e to your computer and use it in GitHub Desktop.
Some test cases for a TCP chat server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
var async = require('async'); | |
function produceClient (callback) { | |
var client = net.connect({port:1337, host:process.env['HOSTNAME']}); | |
client.on('connect', function(data) { callback(null,client) }); | |
client.on('error', function() { callback('failed') }); | |
}; | |
async.series([produceClient,produceClient,produceClient], | |
function(err, results){ | |
if(err) { | |
throw err; | |
} | |
else { | |
testCase(results); | |
} | |
}); | |
function testCase(sockets) { | |
var alice = sockets[0]; | |
var mary = sockets[1]; | |
var john = sockets[2]; | |
setTimeout(function() { alice.write('hello\n') },100); | |
async.parallel([ | |
function(callback) { | |
alice.on('data', function(data) { | |
if (data.toString() == 'Welcome to chat server\n'); | |
else if (data.toString() == 'Message Sent\n') callback(null,'ok'); | |
else callback('failed'); | |
}); | |
}, | |
function(callback) { | |
mary.on('data', function(data) { | |
if (data.toString() == 'Welcome to chat server\n'); | |
else if (data.toString() == 'hello\n') callback(null,'ok'); | |
else callback('failed'); | |
}); | |
}, | |
function(callback) { | |
john.on('data', function(data) { | |
if (data.toString() == 'Welcome to chat server\n'); | |
else if (data.toString() == 'hello\n') callback(null,'ok'); | |
else callback('failed'); | |
}); | |
} | |
], | |
function (err, results) { | |
if (err) { | |
throw err; | |
} | |
else { | |
console.log('tests passed'); | |
process.exit(0); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment