• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25
26const path = require('path');
27const fs = require('fs');
28
29const tmpdir = require('../common/tmpdir');
30
31
32const filepath = path.join(tmpdir.path, 'write.txt');
33
34const EXPECTED = '012345678910';
35
36const cb_expected = 'write open drain write drain close ';
37let cb_occurred = '';
38
39let countDrains = 0;
40
41
42process.on('exit', function() {
43  removeTestFile();
44  if (cb_occurred !== cb_expected) {
45    console.log('  Test callback events missing or out of order:');
46    console.log(`    expected: ${cb_expected}`);
47    console.log(`    occurred: ${cb_occurred}`);
48    assert.strictEqual(
49      cb_occurred, cb_expected,
50      `events missing or out of order: "${cb_occurred}" !== "${cb_expected}"`);
51  } else {
52    console.log('ok');
53  }
54});
55
56function removeTestFile() {
57  try {
58    fs.unlinkSync(filepath);
59  } catch {}
60}
61
62
63tmpdir.refresh();
64
65// Drain at 0, return false at 10.
66const file = fs.createWriteStream(filepath, {
67  highWaterMark: 11
68});
69
70file.on('open', function(fd) {
71  console.error('open');
72  cb_occurred += 'open ';
73  assert.strictEqual(typeof fd, 'number');
74});
75
76file.on('drain', function() {
77  console.error('drain');
78  cb_occurred += 'drain ';
79  ++countDrains;
80  if (countDrains === 1) {
81    console.error('drain=1, write again');
82    assert.strictEqual(fs.readFileSync(filepath, 'utf8'), EXPECTED);
83    console.error(`ondrain write ret= ${file.write(EXPECTED)}`);
84    cb_occurred += 'write ';
85  } else if (countDrains === 2) {
86    console.error('second drain, end');
87    assert.strictEqual(fs.readFileSync(filepath, 'utf8'), EXPECTED + EXPECTED);
88    file.end();
89  }
90});
91
92file.on('close', function() {
93  cb_occurred += 'close ';
94  assert.strictEqual(file.bytesWritten, EXPECTED.length * 2);
95  file.write('should not work anymore', (err) => {
96    assert.ok(err.message.includes('write after end'));
97  });
98});
99
100for (let i = 0; i < 11; i++) {
101  const ret = file.write(String(i));
102  console.error(`${i} ${ret}`);
103
104  // Return false when i hits 10
105  assert.strictEqual(ret, i !== 10);
106}
107cb_occurred += 'write ';
108