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

« back to all changes in this revision

Viewing changes to test/simple/test-stream2-readable-wrap.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
var common = require('../common');
 
23
var assert = require('assert');
 
24
 
 
25
var Readable = require('_stream_readable');
 
26
var Writable = require('_stream_writable');
 
27
var EE = require('events').EventEmitter;
 
28
 
 
29
var testRuns = 0, completedRuns = 0;
 
30
function runTest(highWaterMark, objectMode, produce) {
 
31
  testRuns++;
 
32
 
 
33
  var old = new EE;
 
34
  var r = new Readable({ highWaterMark: highWaterMark, objectMode: objectMode });
 
35
  assert.equal(r, r.wrap(old));
 
36
 
 
37
  var ended = false;
 
38
  r.on('end', function() {
 
39
    ended = true;
 
40
  });
 
41
 
 
42
  var pauses = 0;
 
43
  var resumes = 0;
 
44
 
 
45
  old.pause = function() {
 
46
    pauses++;
 
47
    old.emit('pause');
 
48
    flowing = false;
 
49
  };
 
50
 
 
51
  old.resume = function() {
 
52
    resumes++;
 
53
    old.emit('resume');
 
54
    flow();
 
55
  };
 
56
 
 
57
  var flowing;
 
58
  var chunks = 10;
 
59
  var oldEnded = false;
 
60
  var expected = [];
 
61
  function flow() {
 
62
    flowing = true;
 
63
    while (flowing && chunks-- > 0) {
 
64
      var item = produce();
 
65
      expected.push(item);
 
66
      console.log('emit', chunks);
 
67
      old.emit('data', item);
 
68
    }
 
69
    if (chunks <= 0) {
 
70
      oldEnded = true;
 
71
      console.log('old end', chunks, flowing);
 
72
      old.emit('end');
 
73
    }
 
74
  }
 
75
 
 
76
  var w = new Writable({ highWaterMark: highWaterMark * 2, objectMode: objectMode });
 
77
  var written = [];
 
78
  w._write = function(chunk, encoding, cb) {
 
79
    console.log(chunk);
 
80
    written.push(chunk);
 
81
    setTimeout(cb);
 
82
  };
 
83
 
 
84
  w.on('finish', function() {
 
85
    completedRuns++;
 
86
    performAsserts();
 
87
  });
 
88
 
 
89
  r.pipe(w);
 
90
 
 
91
  flow();
 
92
 
 
93
  function performAsserts() { 
 
94
    assert(ended);
 
95
    assert(oldEnded);
 
96
    assert.deepEqual(written, expected);
 
97
    assert.equal(pauses, 10);
 
98
    assert.equal(resumes, 9);
 
99
  }
 
100
}
 
101
 
 
102
runTest(10, false, function(){ return new Buffer('xxxxxxxxxx'); });
 
103
runTest(1, true, function(){ return { foo: 'bar' }; });
 
104
 
 
105
process.on('exit', function() {
 
106
  assert.equal(testRuns, completedRuns);
 
107
  console.log('ok');
 
108
});