<commit_before>/* Copyright (c) 2012-2013 LevelUP contributors
 * See list at <https://github.com/rvagg/node-levelup#contributing>
 * MIT +no-false-attribs License <https://github.com/rvagg/node-levelup/blob/master/LICENSE>
 */

var levelup    = require('../lib/levelup.js')
  , common     = require('./common')
  , SlowStream = require('slow-stream')
  , delayed    = require('delayed')
  , rimraf     = require('rimraf')
  , async      = require('async')
  , msgpack    = require('msgpack-js')

  , assert  = require('referee').assert
  , refute  = require('referee').refute
  , buster  = require('bustermove')

  , bigBlob    = Array.apply(null, Array(1024 * 100)).map(function () { return 'aaaaaaaaaa' }).join('')

buster.testCase('ReadStream', {
    'setUp': common.readStreamSetUp

  , 'tearDown': common.commonTearDown

  //TODO: test various encodings

  , 'test simple ReadStream': function (done) {
      this.openTestDatabase(function (db) {
        // execute
        db.batch(this.sourceData.slice(), function (err) {
          refute(err)

          var rs = db.createReadStream()
          assert.isFalse(rs.writable)
          assert.isTrue(rs.readable)
          rs.on('data' , this.dataSpy)
          rs.on('end'  , this.endSpy)
          rs.on('close', this.verify.bind(this, rs, done))
        }.bind(this))
      }.bind(this))
    }

  , 'test pausing': function (done) {
      var calls = 0
        , rs
        , pauseVerify = function () {
            // NOTE: another one *will* slip through because the stream triggers an async read before triggering the event
            assert.equals(calls, 6, 'stream should still be paused')
            rs.resume()
            pauseVerify.called = true
          }
        , onData = function () {
            if (++calls == 5) {
              rs.pause()
              setTimeout(pauseVerify, 50)
            }
          }
        , verify = function () {
            assert.equals(calls, this.sourceData.length, 'onData was used in test')
            assert(pauseVerify.called, 'pauseVerify was used in test')
            this.verify(rs, done)
          }.bind(this)

      this.dataSpy = this.spy(onData) // so we can still verify

      this.openTestDatabase(function (db) {
        // execute
        db.batch(this.sourceData.slice(), function (err) {
          refute(err)

          rs = db.createReadStream()
          assert.isFalse(rs.writable)
          assert.isTrue(rs.readable)
          rs.on('data' , this.dataSpy)
          rs.on('end'  , this.endSpy)
          rs.on('end', verify.bind(this))<commit_msg>another data event no longer slips through with .pause(), remove the special case in the tests<commit_after>/* Copyright (c) 2012-2013 LevelUP contributors
 * See list at <https://github.com/rvagg/node-levelup#contributing>
 * MIT +no-false-attribs License <https://github.com/rvagg/node-levelup/blob/master/LICENSE>
 */

var levelup    = require('../lib/levelup.js')
  , common     = require('./common')
  , SlowStream = require('slow-stream')
  , delayed    = require('delayed')
  , rimraf     = require('rimraf')
  , async      = require('async')
  , msgpack    = require('msgpack-js')

  , assert  = require('referee').assert
  , refute  = require('referee').refute
  , buster  = require('bustermove')

  , bigBlob    = Array.apply(null, Array(1024 * 100)).map(function () { return 'aaaaaaaaaa' }).join('')

buster.testCase('ReadStream', {
    'setUp': common.readStreamSetUp

  , 'tearDown': common.commonTearDown

  //TODO: test various encodings

  , 'test simple ReadStream': function (done) {
      this.openTestDatabase(function (db) {
        // execute
        db.batch(this.sourceData.slice(), function (err) {
          refute(err)

          var rs = db.createReadStream()
          assert.isFalse(rs.writable)
          assert.isTrue(rs.readable)
          rs.on('data' , this.dataSpy)
          rs.on('end'  , this.endSpy)
          rs.on('close', this.verify.bind(this, rs, done))
        }.bind(this))
      }.bind(this))
    }

  , 'test pausing': function (done) {
      var calls = 0
        , rs
        , pauseVerify = function () {
            assert.equals(calls, 5, 'stream should still be paused')
            rs.resume()
            pauseVerify.called = true
          }
        , onData = function () {
            if (++calls == 5) {
              rs.pause()
              setTimeout(pauseVerify, 50)
            }
          }
        , verify = function () {
            assert.equals(calls, this.sourceData.length, 'onData was used in test')
            assert(pauseVerify.called, 'pauseVerify was used in test')
            this.verify(rs, done)
          }.bind(this)

      this.dataSpy = this.spy(onData) // so we can still verify

      this.openTestDatabase(function (db) {
        // execute
        db.batch(this.sourceData.slice(), function (err) {
          refute(err)

          rs = db.createReadStream()
          assert.isFalse(rs.writable)
          assert.isTrue(rs.readable)
          rs.on('data' , this.dataSpy)
          rs.on('end'  , this.endSpy)
          rs.on('end', verify.bind(this))
{'fraction_non_alphanumeric': 0.14171539961013646, 'fraction_numerical': 0.007797270955165692, 'mean_word_length': 2.181029138251705, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2850497', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1578, 'n_words': 327}