Blame view

node_modules/mux-demux/test/disconnections.js 2.59 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  /*
    connect two streams.
  
    on a disconnect, both streams should emit 'close'
  */
  
  var consistent = require('./consistent')
  var MuxDemux = require('../')
  var es = require('event-stream')
  var test = require('tape')
  
  module.exports = function (wrapper) {
  
  function randomNumberStream (max, count) {
    count = count || 20
    max   = max   || 10
    return es.readable(function (i, cb) {
      this.emit('data', Math.random() * max)
      if(i > count)
      this.emit('end')
      cb()
    })
  }
  
  test('disconnections 1', function (a) {
  
    var master = consistent(a)
    var slave = master.createSlave()
  
    var client = MuxDemux({error: true, wrapper: wrapper})
    var server = MuxDemux({error: true, wrapper: wrapper})
  
    client.pipe(server).pipe(client)
  
    var count = 0, dCount = 1
    server.on('connection', function (stream) {
      a.equal(stream.meta, 'disconnect1')
      stream
        .on('error', function () {
          console.log('<< ERROR')
        })
       .pipe(slave)
        .pipe(es.log('<<'))//.pipe(stream)
        .on('data', function () {
          dCount ++
        })
       .on('end', function () {
          a.equal(count, dCount, 'each stream should see the same items')
          console.log('<< END')
        })
    })
    var rns = randomNumberStream()
    rns
      .on('data', function (data) {
        if(++ count < 12) return
        if(client.writable) {
          client.destroy()
          console.log('DISCONNECT')
        }
        console.log('DATA', data, count)
      })
      .pipe(master)
      .pipe(es.log('>>'))
      .pipe(client.createWriteStream('disconnect1')
        .on('error', function () {rns.destroy(); console.log('>> ERROR')})
        .on('close', function () {
          a.equal(count, dCount, 'each stream should see the same items')
          console.log('>> END')
        //not all the events are emitted, 
        //but since the streams are destroyed,
        //and piping stops then they end up with 
        //the same data through them.
          slave.validate()
          a.end()
        }))
  
    /*
    THERE are some problems with streams that close.
    or rather, SHOULD close.   
    */ 
   
  });
  
  test('simple', function simple (a) {
  
    var client = MuxDemux({error: true, wrapper: wrapper})
    var server = MuxDemux({error: true, wrapper: wrapper})
  
    client.pipe(server).pipe(client)
    client.resume()
    server.resume()
    var r1 = Math.random()
    server.on('connection', function (stream) {
      stream.on('data', function (data) {
        a.equal(data, r1)
        console.log('data')
      })
      stream.on('end', function () {
        console.log('end')
        a.end()
      })
    })
  
    c = client.createWriteStream()
    c.write(r1)
    c.end()
  
  });
  
  
  }
  
  if(!module.parent)
    module.exports()