2011-09-11 22:30:01 +02:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
2012-10-05 02:44:48 +02:00
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
var util = require('util');
|
2011-09-11 22:30:01 +02:00
|
|
|
|
|
|
|
|
2012-10-05 02:44:48 +02:00
|
|
|
var filepath = path.join(common.tmpDir, 'write.txt');
|
|
|
|
var file;
|
2011-09-11 22:30:01 +02:00
|
|
|
|
|
|
|
var EXPECTED = '012345678910';
|
|
|
|
|
2012-10-05 02:44:48 +02:00
|
|
|
var cb_expected = 'write open drain write drain close error ';
|
|
|
|
var cb_occurred = '';
|
2011-09-11 22:30:01 +02:00
|
|
|
|
|
|
|
var countDrains = 0;
|
|
|
|
|
|
|
|
|
|
|
|
process.on('exit', function() {
|
|
|
|
removeTestFile();
|
2011-10-05 00:08:18 +02:00
|
|
|
if (cb_occurred !== cb_expected) {
|
2011-09-11 22:30:01 +02:00
|
|
|
console.log(' Test callback events missing or out of order:');
|
|
|
|
console.log(' expected: %j', cb_expected);
|
|
|
|
console.log(' occurred: %j', cb_occurred);
|
|
|
|
assert.strictEqual(cb_occurred, cb_expected,
|
2011-10-05 00:08:18 +02:00
|
|
|
'events missing or out of order: "' +
|
|
|
|
cb_occurred + '" !== "' + cb_expected + '"');
|
2012-10-05 02:44:48 +02:00
|
|
|
} else {
|
|
|
|
console.log('ok');
|
2011-09-11 22:30:01 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function removeTestFile() {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(filepath);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
removeTestFile();
|
|
|
|
|
2012-10-05 02:44:48 +02:00
|
|
|
// drain at 0, return false at 10.
|
|
|
|
file = fs.createWriteStream(filepath, {
|
|
|
|
highWaterMark: 11
|
|
|
|
});
|
2011-09-11 22:30:01 +02:00
|
|
|
|
|
|
|
file.on('open', function(fd) {
|
2012-10-05 02:44:48 +02:00
|
|
|
console.error('open');
|
2011-09-11 22:30:01 +02:00
|
|
|
cb_occurred += 'open ';
|
|
|
|
assert.equal(typeof fd, 'number');
|
|
|
|
});
|
|
|
|
|
|
|
|
file.on('drain', function() {
|
2012-10-05 02:44:48 +02:00
|
|
|
console.error('drain');
|
2011-09-11 22:30:01 +02:00
|
|
|
cb_occurred += 'drain ';
|
|
|
|
++countDrains;
|
|
|
|
if (countDrains === 1) {
|
2012-10-05 02:44:48 +02:00
|
|
|
console.error('drain=1, write again');
|
|
|
|
assert.equal(fs.readFileSync(filepath, 'utf8'), EXPECTED);
|
|
|
|
console.error('ondrain write ret=%j', file.write(EXPECTED));
|
2011-09-11 22:30:01 +02:00
|
|
|
cb_occurred += 'write ';
|
|
|
|
} else if (countDrains == 2) {
|
2012-10-05 02:44:48 +02:00
|
|
|
console.error('second drain, end');
|
|
|
|
assert.equal(fs.readFileSync(filepath, 'utf8'), EXPECTED + EXPECTED);
|
2011-09-11 22:30:01 +02:00
|
|
|
file.end();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
file.on('close', function() {
|
|
|
|
cb_occurred += 'close ';
|
|
|
|
assert.strictEqual(file.bytesWritten, EXPECTED.length * 2);
|
|
|
|
file.write('should not work anymore');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
file.on('error', function(err) {
|
|
|
|
cb_occurred += 'error ';
|
2012-10-05 02:44:48 +02:00
|
|
|
assert.ok(err.message.indexOf('write after end') >= 0);
|
2011-09-11 22:30:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < 11; i++) {
|
2012-10-05 02:44:48 +02:00
|
|
|
var ret = file.write(i + '');
|
|
|
|
console.error('%d %j', i, ret);
|
|
|
|
|
|
|
|
// return false when i hits 10
|
|
|
|
assert(ret === (i != 10));
|
2011-09-11 22:30:01 +02:00
|
|
|
}
|
|
|
|
cb_occurred += 'write ';
|