• 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    // Continue regardless of error.
61  }
62}
63
64
65tmpdir.refresh();
66
67// Drain at 0, return false at 10.
68const file = fs.createWriteStream(filepath, {
69  highWaterMark: 11
70});
71
72file.on('open', function(fd) {
73  console.error('open');
74  cb_occurred += 'open ';
75  assert.strictEqual(typeof fd, 'number');
76});
77
78file.on('drain', function() {
79  console.error('drain');
80  cb_occurred += 'drain ';
81  ++countDrains;
82  if (countDrains === 1) {
83    console.error('drain=1, write again');
84    assert.strictEqual(fs.readFileSync(filepath, 'utf8'), EXPECTED);
85    console.error(`ondrain write ret= ${file.write(EXPECTED)}`);
86    cb_occurred += 'write ';
87  } else if (countDrains === 2) {
88    console.error('second drain, end');
89    assert.strictEqual(fs.readFileSync(filepath, 'utf8'), EXPECTED + EXPECTED);
90    file.end();
91  }
92});
93
94file.on('close', function() {
95  cb_occurred += 'close ';
96  assert.strictEqual(file.bytesWritten, EXPECTED.length * 2);
97  file.write('should not work anymore', (err) => {
98    assert.ok(err.message.includes('write after end'));
99  });
100});
101
102for (let i = 0; i < 11; i++) {
103  const ret = file.write(String(i));
104  console.error(`${i} ${ret}`);
105
106  // Return false when i hits 10
107  assert.strictEqual(ret, i !== 10);
108}
109cb_occurred += 'write ';
110