• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var test = require('tape')
2var through = require('../')
3
4// must emit end before close.
5
6test('buffering', function(assert) {
7  var ts = through(function (data) {
8    this.queue(data)
9  }, function () {
10    this.queue(null)
11  })
12
13  var ended = false,  actual = []
14
15  ts.on('data', actual.push.bind(actual))
16  ts.on('end', function () {
17    ended = true
18  })
19
20  ts.write(1)
21  ts.write(2)
22  ts.write(3)
23  assert.deepEqual(actual, [1, 2, 3])
24  ts.pause()
25  ts.write(4)
26  ts.write(5)
27  ts.write(6)
28  assert.deepEqual(actual, [1, 2, 3])
29  ts.resume()
30  assert.deepEqual(actual, [1, 2, 3, 4, 5, 6])
31  ts.pause()
32  ts.end()
33  assert.ok(!ended)
34  ts.resume()
35  assert.ok(ended)
36  assert.end()
37})
38
39test('buffering has data in queue, when ends', function (assert) {
40
41  /*
42   * If stream ends while paused with data in the queue,
43   * stream should still emit end after all data is written
44   * on resume.
45   */
46
47  var ts = through(function (data) {
48    this.queue(data)
49  }, function () {
50    this.queue(null)
51  })
52
53  var ended = false,  actual = []
54
55  ts.on('data', actual.push.bind(actual))
56  ts.on('end', function () {
57    ended = true
58  })
59
60  ts.pause()
61  ts.write(1)
62  ts.write(2)
63  ts.write(3)
64  ts.end()
65  assert.deepEqual(actual, [], 'no data written yet, still paused')
66  assert.ok(!ended, 'end not emitted yet, still paused')
67  ts.resume()
68  assert.deepEqual(actual, [1, 2, 3], 'resumed, all data should be delivered')
69  assert.ok(ended, 'end should be emitted once all data was delivered')
70  assert.end();
71})
72