l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

index.js (3881B)


      1 var from = require('..')
      2 var spec = require('stream-spec')
      3 var a = require('assertions')
      4 
      5 function read(stream, callback) {
      6   var actual = []
      7   stream.on('data', function (data) {
      8     actual.push(data)
      9   })
     10   stream.once('end', function () {
     11     callback(null, actual)
     12   })
     13   stream.once('error', function (err) {
     14     callback(err)
     15   })
     16 }
     17 
     18 function pause(stream) {
     19   stream.on('data', function () {
     20     if(Math.random() > 0.1) return
     21     stream.pause()
     22     process.nextTick(function () {
     23       stream.resume()
     24     })
     25   })
     26 }
     27 
     28 exports['inc'] = function (test) {
     29 
     30   var fs = from(function (i) {
     31     this.emit('data', i)
     32     if(i >= 99)
     33       return this.emit('end')
     34     return true
     35   })
     36 
     37   spec(fs).readable().validateOnExit() 
     38 
     39   read(fs, function (err, arr) {
     40     test.equal(arr.length, 100)
     41     test.done()
     42   })
     43 }
     44 
     45 exports['inc - async'] = function (test) {
     46 
     47   var fs = from(function (i, next) {
     48     this.emit('data', i)
     49     if(i >= 99)
     50       return this.emit('end')
     51 		next();
     52   })
     53 
     54   spec(fs).readable().validateOnExit() 
     55 
     56   read(fs, function (err, arr) {
     57     test.equal(arr.length, 100)
     58     test.done()
     59   })
     60 }
     61 
     62 exports['large stream - from an array'] = function (test) {
     63 
     64   var l = 100000
     65     , expected = [] 
     66 
     67   while(l--) expected.push(l * Math.random())
     68 
     69   var fs = from(expected.slice())
     70 
     71   spec(fs).readable().validateOnExit() 
     72 
     73   read(fs, function (err, arr) {
     74 		a.deepEqual(arr, expected)
     75     test.done()
     76   })
     77 }
     78 
     79 exports['large stream - callback return true'] = function (test) {
     80 
     81   var fs = from(function (i, next) {
     82     this.emit('data', i)
     83     if(i >= 99999)
     84       return this.emit('end')
     85 		return true;
     86   })
     87 
     88   spec(fs).readable().validateOnExit() 
     89 
     90   read(fs, function (err, arr) {
     91     test.equal(arr.length, 100000)
     92     test.done()
     93   })
     94 }
     95 
     96 exports['large stream - callback call next()'] = function (test) {
     97 
     98   var fs = from(function (i, next) {
     99     this.emit('data', i)
    100     if(i >= 99999)
    101       return this.emit('end')
    102 		next();
    103   })
    104 
    105   spec(fs).readable().validateOnExit() 
    106 
    107   read(fs, function (err, arr) {
    108     test.equal(arr.length, 100000)
    109     test.done()
    110   })
    111 }
    112 
    113 exports['simple'] = function (test) {
    114 
    115   var l = 1000
    116     , expected = [] 
    117 
    118   while(l--) expected.push(l * Math.random())
    119 
    120   var t = from(expected.slice())
    121 
    122   spec(t)
    123     .readable()
    124     .pausable({strict: true})
    125     .validateOnExit()
    126 
    127   read(t, function (err, actual) {
    128     if(err) test.error(err) //fail
    129     a.deepEqual(actual, expected)
    130     test.done()
    131   })
    132 
    133 }
    134 
    135 exports['simple pausable'] = function (test) {
    136 
    137   var l = 1000
    138     , expected = [] 
    139 
    140   while(l--) expected.push(l * Math.random())
    141 
    142   var t = from(expected.slice())
    143 
    144   spec(t)
    145     .readable()
    146     .pausable({strict: true})
    147     .validateOnExit()
    148 
    149   pause(t)
    150 
    151   read(t, function (err, actual) {
    152     if(err) test.error(err) //fail
    153     a.deepEqual(actual, expected)
    154     test.done()
    155   })
    156 
    157 }
    158 
    159 exports['simple (not strictly pausable) setTimeout'] = function (test) {
    160 
    161   var l = 10
    162     , expected = [] 
    163   while(l--) expected.push(l * Math.random())
    164 
    165 
    166   var _expected = expected.slice()
    167   var t = from(function (i, n) {
    168     var self = this
    169     setTimeout(function () {
    170       if(_expected.length)
    171         self.emit('data', _expected.shift())
    172       else
    173         if(!self.ended)
    174           self.emit('end')
    175       n()
    176     }, 3)
    177   })
    178 
    179   /*
    180     using from in this way will not be strictly pausable.
    181     it could be extended to buffer outputs, but I think a better
    182     way would be to use a PauseStream that implements strict pause.
    183   */
    184 
    185   spec(t)
    186     .readable()
    187     .pausable({strict: false })
    188     .validateOnExit()
    189 
    190   //pause(t)
    191   var paused = false
    192   var i = setInterval(function () {
    193     if(!paused) t.pause()
    194     else t.resume()
    195     paused = !paused
    196   }, 2)
    197 
    198   t.on('end', function () {
    199     clearInterval(i)
    200   })
    201 
    202   read(t, function (err, actual) {
    203     if(err) test.error(err) //fail
    204     a.deepEqual(actual, expected)
    205     test.done()
    206   })
    207 
    208 }
    209 
    210