~yolanda.robla/ubuntu/trusty/nodejs/add_distribution

« back to all changes in this revision

Viewing changes to test/simple/test-stream2-basic.js

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
23
var common = require('../common.js');
 
24
var R = require('_stream_readable');
 
25
var assert = require('assert');
 
26
 
 
27
var util = require('util');
 
28
var EE = require('events').EventEmitter;
 
29
 
 
30
function TestReader(n) {
 
31
  R.apply(this);
 
32
  this._buffer = new Buffer(n || 100);
 
33
  this._buffer.fill('x');
 
34
  this._pos = 0;
 
35
  this._bufs = 10;
 
36
}
 
37
 
 
38
util.inherits(TestReader, R);
 
39
 
 
40
TestReader.prototype.read = function(n) {
 
41
  if (n === 0) return null;
 
42
  var max = this._buffer.length - this._pos;
 
43
  n = n || max;
 
44
  n = Math.max(n, 0);
 
45
  var toRead = Math.min(n, max);
 
46
  if (toRead === 0) {
 
47
    // simulate the read buffer filling up with some more bytes some time
 
48
    // in the future.
 
49
    setTimeout(function() {
 
50
      this._pos = 0;
 
51
      this._bufs -= 1;
 
52
      if (this._bufs <= 0) {
 
53
        // read them all!
 
54
        if (!this.ended) {
 
55
          this.emit('end');
 
56
          this.ended = true;
 
57
        }
 
58
      } else {
 
59
        this.emit('readable');
 
60
      }
 
61
    }.bind(this), 10);
 
62
    return null;
 
63
  }
 
64
 
 
65
  var ret = this._buffer.slice(this._pos, this._pos + toRead);
 
66
  this._pos += toRead;
 
67
  return ret;
 
68
};
 
69
 
 
70
/////
 
71
 
 
72
function TestWriter() {
 
73
  EE.apply(this);
 
74
  this.received = [];
 
75
  this.flush = false;
 
76
}
 
77
 
 
78
util.inherits(TestWriter, EE);
 
79
 
 
80
TestWriter.prototype.write = function(c) {
 
81
  this.received.push(c.toString());
 
82
  this.emit('write', c);
 
83
  return true;
 
84
};
 
85
 
 
86
TestWriter.prototype.end = function(c) {
 
87
  if (c) this.write(c);
 
88
  this.emit('end', this.received);
 
89
};
 
90
 
 
91
////////
 
92
 
 
93
// tiny node-tap lookalike.
 
94
var tests = [];
 
95
var count = 0;
 
96
 
 
97
function test(name, fn) {
 
98
  count++;
 
99
  tests.push([name, fn]);
 
100
}
 
101
 
 
102
function run() {
 
103
  var next = tests.shift();
 
104
  if (!next)
 
105
    return console.error('ok');
 
106
 
 
107
  var name = next[0];
 
108
  var fn = next[1];
 
109
  console.log('# %s', name);
 
110
  fn({
 
111
    same: assert.deepEqual,
 
112
    ok: assert,
 
113
    equal: assert.equal,
 
114
    end: function () {
 
115
      count--;
 
116
      run();
 
117
    }
 
118
  });
 
119
}
 
120
 
 
121
// ensure all tests have run
 
122
process.on("exit", function () {
 
123
  assert.equal(count, 0);
 
124
});
 
125
 
 
126
process.nextTick(run);
 
127
 
 
128
 
 
129
test('a most basic test', function(t) {
 
130
  var r = new TestReader(20);
 
131
 
 
132
  var reads = [];
 
133
  var expect = [ 'x',
 
134
                 'xx',
 
135
                 'xxx',
 
136
                 'xxxx',
 
137
                 'xxxxx',
 
138
                 'xxxxx',
 
139
                 'xxxxxxxx',
 
140
                 'xxxxxxxxx',
 
141
                 'xxx',
 
142
                 'xxxxxxxxxxxx',
 
143
                 'xxxxxxxx',
 
144
                 'xxxxxxxxxxxxxxx',
 
145
                 'xxxxx',
 
146
                 'xxxxxxxxxxxxxxxxxx',
 
147
                 'xx',
 
148
                 'xxxxxxxxxxxxxxxxxxxx',
 
149
                 'xxxxxxxxxxxxxxxxxxxx',
 
150
                 'xxxxxxxxxxxxxxxxxxxx',
 
151
                 'xxxxxxxxxxxxxxxxxxxx',
 
152
                 'xxxxxxxxxxxxxxxxxxxx' ];
 
153
 
 
154
  r.on('end', function() {
 
155
    t.same(reads, expect);
 
156
    t.end();
 
157
  });
 
158
 
 
159
  var readSize = 1;
 
160
  function flow() {
 
161
    var res;
 
162
    while (null !== (res = r.read(readSize++))) {
 
163
      reads.push(res.toString());
 
164
    }
 
165
    r.once('readable', flow);
 
166
  }
 
167
 
 
168
  flow();
 
169
});
 
170
 
 
171
test('pipe', function(t) {
 
172
  var r = new TestReader(5);
 
173
 
 
174
  var expect = [ 'xxxxx',
 
175
                 'xxxxx',
 
176
                 'xxxxx',
 
177
                 'xxxxx',
 
178
                 'xxxxx',
 
179
                 'xxxxx',
 
180
                 'xxxxx',
 
181
                 'xxxxx',
 
182
                 'xxxxx',
 
183
                 'xxxxx' ]
 
184
 
 
185
  var w = new TestWriter;
 
186
  var flush = true;
 
187
 
 
188
  w.on('end', function(received) {
 
189
    t.same(received, expect);
 
190
    t.end();
 
191
  });
 
192
 
 
193
  r.pipe(w);
 
194
});
 
195
 
 
196
 
 
197
 
 
198
[1,2,3,4,5,6,7,8,9].forEach(function(SPLIT) {
 
199
  test('unpipe', function(t) {
 
200
    var r = new TestReader(5);
 
201
 
 
202
    // unpipe after 3 writes, then write to another stream instead.
 
203
    var expect = [ 'xxxxx',
 
204
                   'xxxxx',
 
205
                   'xxxxx',
 
206
                   'xxxxx',
 
207
                   'xxxxx',
 
208
                   'xxxxx',
 
209
                   'xxxxx',
 
210
                   'xxxxx',
 
211
                   'xxxxx',
 
212
                   'xxxxx' ];
 
213
    expect = [ expect.slice(0, SPLIT), expect.slice(SPLIT) ];
 
214
 
 
215
    var w = [ new TestWriter(), new TestWriter() ];
 
216
 
 
217
    var writes = SPLIT;
 
218
    w[0].on('write', function() {
 
219
      if (--writes === 0) {
 
220
        r.unpipe();
 
221
        t.equal(r._readableState.pipes, null);
 
222
        w[0].end();
 
223
        r.pipe(w[1]);
 
224
        t.equal(r._readableState.pipes, w[1]);
 
225
      }
 
226
    });
 
227
 
 
228
    var ended = 0;
 
229
 
 
230
    var ended0 = false;
 
231
    var ended1 = false;
 
232
    w[0].on('end', function(results) {
 
233
      t.equal(ended0, false);
 
234
      ended0 = true;
 
235
      ended++;
 
236
      t.same(results, expect[0]);
 
237
    });
 
238
 
 
239
    w[1].on('end', function(results) {
 
240
      t.equal(ended1, false);
 
241
      ended1 = true;
 
242
      ended++;
 
243
      t.equal(ended, 2);
 
244
      t.same(results, expect[1]);
 
245
      t.end();
 
246
    });
 
247
 
 
248
    r.pipe(w[0]);
 
249
  });
 
250
});
 
251
 
 
252
 
 
253
// both writers should get the same exact data.
 
254
test('multipipe', function(t) {
 
255
  var r = new TestReader(5);
 
256
  var w = [ new TestWriter, new TestWriter ];
 
257
 
 
258
  var expect = [ 'xxxxx',
 
259
                 'xxxxx',
 
260
                 'xxxxx',
 
261
                 'xxxxx',
 
262
                 'xxxxx',
 
263
                 'xxxxx',
 
264
                 'xxxxx',
 
265
                 'xxxxx',
 
266
                 'xxxxx',
 
267
                 'xxxxx' ];
 
268
 
 
269
  var c = 2;
 
270
  w[0].on('end', function(received) {
 
271
    t.same(received, expect, 'first');
 
272
    if (--c === 0) t.end();
 
273
  });
 
274
  w[1].on('end', function(received) {
 
275
    t.same(received, expect, 'second');
 
276
    if (--c === 0) t.end();
 
277
  });
 
278
 
 
279
  r.pipe(w[0]);
 
280
  r.pipe(w[1]);
 
281
});
 
282
 
 
283
 
 
284
[1,2,3,4,5,6,7,8,9].forEach(function(SPLIT) {
 
285
  test('multi-unpipe', function(t) {
 
286
    var r = new TestReader(5);
 
287
 
 
288
    // unpipe after 3 writes, then write to another stream instead.
 
289
    var expect = [ 'xxxxx',
 
290
                   'xxxxx',
 
291
                   'xxxxx',
 
292
                   'xxxxx',
 
293
                   'xxxxx',
 
294
                   'xxxxx',
 
295
                   'xxxxx',
 
296
                   'xxxxx',
 
297
                   'xxxxx',
 
298
                   'xxxxx' ];
 
299
    expect = [ expect.slice(0, SPLIT), expect.slice(SPLIT) ];
 
300
 
 
301
    var w = [ new TestWriter(), new TestWriter(), new TestWriter() ];
 
302
 
 
303
    var writes = SPLIT;
 
304
    w[0].on('write', function() {
 
305
      if (--writes === 0) {
 
306
        r.unpipe();
 
307
        w[0].end();
 
308
        r.pipe(w[1]);
 
309
      }
 
310
    });
 
311
 
 
312
    var ended = 0;
 
313
 
 
314
    w[0].on('end', function(results) {
 
315
      ended++;
 
316
      t.same(results, expect[0]);
 
317
    });
 
318
 
 
319
    w[1].on('end', function(results) {
 
320
      ended++;
 
321
      t.equal(ended, 2);
 
322
      t.same(results, expect[1]);
 
323
      t.end();
 
324
    });
 
325
 
 
326
    r.pipe(w[0]);
 
327
    r.pipe(w[2]);
 
328
  });
 
329
});
 
330
 
 
331
test('back pressure respected', function (t) {
 
332
  function noop() {}
 
333
 
 
334
  var r = new R({ objectMode: true });
 
335
  r._read = noop;
 
336
  var counter = 0;
 
337
  r.push(["one"]);
 
338
  r.push(["two"]);
 
339
  r.push(["three"]);
 
340
  r.push(["four"]);
 
341
  r.push(null);
 
342
 
 
343
  var w1 = new R();
 
344
  w1.write = function (chunk) {
 
345
    assert.equal(chunk[0], "one");
 
346
    w1.emit("close");
 
347
    process.nextTick(function () {
 
348
      r.pipe(w2);
 
349
      r.pipe(w3);
 
350
    })
 
351
  };
 
352
  w1.end = noop;
 
353
 
 
354
  r.pipe(w1);
 
355
 
 
356
  var expected = ["two", "two", "three", "three", "four", "four"];
 
357
 
 
358
  var w2 = new R();
 
359
  w2.write = function (chunk) {
 
360
    assert.equal(chunk[0], expected.shift());
 
361
    assert.equal(counter, 0);
 
362
 
 
363
    counter++;
 
364
 
 
365
    if (chunk[0] === "four") {
 
366
      return true;
 
367
    }
 
368
 
 
369
    setTimeout(function () {
 
370
      counter--;
 
371
      w2.emit("drain");
 
372
    }, 10);
 
373
 
 
374
    return false;
 
375
  }
 
376
  w2.end = noop;
 
377
 
 
378
  var w3 = new R();
 
379
  w3.write = function (chunk) {
 
380
    assert.equal(chunk[0], expected.shift());
 
381
    assert.equal(counter, 1);
 
382
 
 
383
    counter++;
 
384
 
 
385
    if (chunk[0] === "four") {
 
386
      return true;
 
387
    }
 
388
 
 
389
    setTimeout(function () {
 
390
      counter--;
 
391
      w3.emit("drain");
 
392
    }, 50);
 
393
 
 
394
    return false;
 
395
  };
 
396
  w3.end = function () {
 
397
    assert.equal(counter, 2);
 
398
    assert.equal(expected.length, 0);
 
399
    t.end();
 
400
  };
 
401
});
 
402
 
 
403
test('read(0) for ended streams', function (t) {
 
404
  var r = new R();
 
405
  var written = false;
 
406
  var ended = false;
 
407
  r._read = function (n) {};
 
408
 
 
409
  r.push(new Buffer("foo"));
 
410
  r.push(null);
 
411
 
 
412
  var v = r.read(0);
 
413
 
 
414
  assert.equal(v, null);
 
415
 
 
416
  var w = new R();
 
417
 
 
418
  w.write = function (buffer) {
 
419
    written = true;
 
420
    assert.equal(ended, false);
 
421
    assert.equal(buffer.toString(), "foo")
 
422
  };
 
423
 
 
424
  w.end = function () {
 
425
    ended = true;
 
426
    assert.equal(written, true);
 
427
    t.end();
 
428
  };
 
429
 
 
430
  r.pipe(w);
 
431
})
 
432
 
 
433
test('sync _read ending', function (t) {
 
434
  var r = new R();
 
435
  var called = false;
 
436
  r._read = function (n) {
 
437
    r.push(null);
 
438
  };
 
439
 
 
440
  r.once('end', function () {
 
441
    called = true;
 
442
  })
 
443
 
 
444
  r.read();
 
445
 
 
446
  process.nextTick(function () {
 
447
    assert.equal(called, true);
 
448
    t.end();
 
449
  })
 
450
});
 
451
 
 
452
test('adding readable triggers data flow', function(t) {
 
453
  var r = new R({ highWaterMark: 5 });
 
454
  var onReadable = false;
 
455
  var readCalled = 0;
 
456
 
 
457
  r._read = function(n) {
 
458
    if (readCalled++ === 2)
 
459
      r.push(null);
 
460
    else
 
461
      r.push(new Buffer('asdf'));
 
462
  };
 
463
 
 
464
  var called = false;
 
465
  r.on('readable', function() {
 
466
    onReadable = true;
 
467
    r.read();
 
468
  });
 
469
 
 
470
  r.on('end', function() {
 
471
    t.equal(readCalled, 3);
 
472
    t.ok(onReadable);
 
473
    t.end();
 
474
  });
 
475
});