diff --git a/Gruntfile.js b/Gruntfile.js index 4aafc20..030f9b6 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -11,9 +11,8 @@ module.exports = function(grunt) { eslint: { src: [ '*.js', + 'lib/**/*.js', 'test/**/*.js', - 'test_mocks/**/*.js', - 'arduinoTest/**/*.js', 'bin/*.js' ] } diff --git a/arduinoTest/stress.js b/arduinoTest/stress.js deleted file mode 100644 index 643139a..0000000 --- a/arduinoTest/stress.js +++ /dev/null @@ -1,64 +0,0 @@ -'use strict'; - -var chai = require('chai'); -var util = require('util'); -var serialPort = require('../serialport'); -require('colors'); // this modifies String.prototype -var fs = require('fs'); -var memwatch = require('memwatch'); - -describe('stress', function() { - describe('long running', function() { - it('opens a port and sends data, repeated indefinitely', function (done) { - var hd = new memwatch.HeapDiff(); - - memwatch.on('leak', function(info) { - fs.appendFile('leak.log', util.inspect(info)); - console.log(util.inspect(info).red); - - var diff = hd.end(); - fs.appendFile('heapdiff.log', util.inspect(diff)); - hd = new memwatch.HeapDiff(); - }); - - memwatch.on('stats', function (stats) { - fs.appendFile('stats.log', util.inspect(stats)); - console.log(util.inspect(stats).green); - }); - - serialPort.list(function(err, ports) { - chai.assert.isUndefined(err, util.inspect(err)); - chai.assert.isDefined(ports, 'ports is not defined'); - chai.assert.isTrue(ports.length > 0, 'no ports found'); - - var data = new Buffer('hello'); - - var port = new serialPort.SerialPort(ports.slice(-1)[0].comName, null, false); - port.on('error', function(err) { - chai.assert.fail(util.inspect(err)); - }); - - port.on('data', function () {}); - - port.open(function(err) { - chai.assert.isUndefined(err, util.inspect(err)); - - var intervalId = setInterval(function () { - port.write(data); - }, 20); - - setTimeout(function() { - clearInterval(intervalId); - - var diff = hd.end(); - fs.appendFile('heapdiff.log', util.inspect(diff)); - - console.log(util.inspect(diff).green); - - done(); - }, 3590000); - }); - }); - }); - }); -}); diff --git a/package.json b/package.json index 3377459..5f3c9f3 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,8 @@ "license": "MIT", "scripts": { "install": "node-pre-gyp install --fallback-to-build", - "rebuild": "node-pre-gyp rebuild", + "rebuild": "npm rebuild && node-pre-gyp rebuild", + "stress": "mocha --no-timeouts test/arduinoTest/stress.js", "test": "grunt --verbose" } } diff --git a/arduinoTest/arduinoEcho/arduinoEcho.ino b/test/arduinoTest/arduinoEcho/arduinoEcho.ino similarity index 100% rename from arduinoTest/arduinoEcho/arduinoEcho.ino rename to test/arduinoTest/arduinoEcho/arduinoEcho.ino diff --git a/arduinoTest/serialDuplexTest.js b/test/arduinoTest/serialDuplexTest.js similarity index 52% rename from arduinoTest/serialDuplexTest.js rename to test/arduinoTest/serialDuplexTest.js index 9acb7ad..5c7edd9 100755 --- a/arduinoTest/serialDuplexTest.js +++ b/test/arduinoTest/serialDuplexTest.js @@ -4,24 +4,37 @@ serialDuplexTest.js Tests the functionality of the serial port library. -To be used in conjunction with the Arduino sketch called ArduinoEcho.ino +To be used in conjunction with the Arduino sketch ArduinoEcho.ino */ 'use strict'; +var SerialPort = require('../../serialport').SerialPort; +var optimist = require('optimist'); -// serial port initialization: -var serialport = require('../serialport'); // include the serialport library -var SerialPort = serialport.SerialPort; // make a local instance of serial -var portName = process.argv[2]; // get the port name from the command line -var myPort = new SerialPort(portName); // open the serial port: +var args = optimist + .alias('h', 'help') + .alias('h', '?') + .usage('Run printable characters through the serial port\n Usage: $0') + .options('p', { + describe: 'Name of serial port. See serialportlist for available serial ports.' + }) + .demand(['p']) + .argv; + +if (args.help) { + optimist.showHelp(); + return process.exit(0); +} + +var port = new SerialPort(args.p); // open the serial port: var output = 32; // ASCII space; lowest printable character var byteCount = 0; // number of bytes read function onOpen() { - console.log('port open'); - console.log('baud rate: ' + myPort.options.baudRate); + console.log('Port Open'); + console.log('Baud Rate: ' + port.options.baudRate); var outString = String.fromCharCode(output); console.log('Sent:\t\t' + outString); - myPort.write(outString); + port.write(outString); } function onData(data) { @@ -34,7 +47,7 @@ function onData(data) { console.log('Read Events:\t' + byteCount); byteCount++; var outString = String.fromCharCode(output); - myPort.write(outString); + port.write(outString); console.log('Sent:\t\t' + outString); } @@ -48,7 +61,7 @@ function onError(error) { process.exit(1); } -myPort.on('open', onOpen); // called when the serial port opens -myPort.on('data', onData); // called when data comes in -myPort.on('close', onClose); // called when the serial port closes -myPort.on('error', onError); // called when there's an error with the serial port +port.on('open', onOpen); +port.on('data', onData); +port.on('close', onClose); +port.on('error', onError); diff --git a/test/arduinoTest/stress.js b/test/arduinoTest/stress.js new file mode 100644 index 0000000..25d5048 --- /dev/null +++ b/test/arduinoTest/stress.js @@ -0,0 +1,128 @@ +'use strict'; + +// `npm run stress` to run these tests + +var assert = require('chai').assert; +var util = require('util'); +var serialPort = require('../../serialport'); +require('colors'); // this modifies String.prototype +// var fs = require('fs'); + +// Installing memwatch-next via package.json fails on node 10 on linux on our CI; +// Pending https://github.com/marcominetti/node-memwatch/issues/15 +try { + var memwatch = require('memwatch-next'); +} catch (e) { + console.error('Please install memwatch-next to use the stress tests'); + process.exit(-1); +} + +describe('the stress', function() { + var testPort = process.env.TEST_PORT; + + if (!testPort) { + it('cannot be tested as we have no test ports'); + return; + } + + describe('of 2 minutes of running 1k writes', function() { + if (process.version.match('v0.10.')) { + it('either leaks like a siv or memwatch is broken on v10'); + return; + } + + it("doesn't leak memory", function (done) { + var data = new Buffer(1024); + var hd = new memwatch.HeapDiff(); + var port = new serialPort.SerialPort(testPort, {}, false); + port.on('close', done); + + var leaks = 0; + memwatch.on('leak', function(info) { + // fs.appendFile('leak.log', util.inspect(info)); + console.log(util.inspect(info, {depth: 5}).red); + leaks++; + }); + + // memwatch.on('stats', function (stats) { + // fs.appendFile('stats.log', util.inspect(stats)); + // }); + + port.on('error', function(err) { + assert.fail(util.inspect(err)); + done(); + }); + + port.on('data', function () {}); + + var writing = true; + var write = function() { + if (!writing) { return } + port.write(data, write); + }; + + port.open(function(err) { + assert.isUndefined(err); + write(); + + setTimeout(function() { + console.log('cleaning up'); + // var diff = hd.end(); + // fs.appendFile('heapdiff.log', util.inspect(diff)); + // console.log(util.inspect(diff, {depth: 5}).blue); + writing = false; + + if (leaks > 0) { + var diff = hd.end(); + // fs.appendFile('heapdiff.log', util.inspect(diff, {depth: 5})); + console.log(util.inspect(diff, {depth: 5}).red); + assert.fail('leak detected'); + } + port.close(); + }, 1000 * 60 * 2); + }); + }); + }); + + // describe('of opening and closing ports', function() { + // it("doesn't leak memory", function(done) { + // var hd = new memwatch.HeapDiff(); + // var port = new serialPort.SerialPort(testPort, {}, false); + + // memwatch.on('leak', function(info) { + // // fs.appendFile('leak.log', util.inspect(info)); + // console.log(util.inspect(info, {depth: 5}).red); + + // var diff = hd.end(); + // // fs.appendFile('heapdiff.log', util.inspect(diff, {depth: 5})); + // console.log(util.inspect(diff, {depth: 5}).red); + // assert.fail('leak detected'); + // port.close(); + // }); + + // var open, close; + // open = function() { + // process.nextTick(function() { + // port.open(close); + // }); + // }; + + // var looping = true; + // close = function() { + // if (looping) { + // process.nextTick(function() { + // port.close(open); + // }); + // } else { + // port.close(); + // } + // }; + // setTimeout(function() { + // looping = false; + // port.on('close', done); + // }, 1000 * 10); + + // open(); + // }); + // }); +});