Welcome to mirror list, hosted at ThFree Co, Russian Federation.

swarm-reconnect.js « node « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4fad7d03db8f1c7b4467621a8b8a8e174cbec349 (plain)
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
var hat = require('hat')
var Swarm = require('../../lib/swarm')
var test = require('tape')

var infoHash = 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36'
var peerId1 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex')
var peerId2 = new Buffer('-WW0001-' + hat(48), 'utf8').toString('hex')

test('reconnect when peer disconnects', function (t) {
  t.plan(10)

  var swarm1 = new Swarm(infoHash, peerId1)
  swarm1.listen(0, function () {
    var swarm2 = new Swarm(infoHash, peerId2)

    var time1 = 0
    swarm1.on('wire', function (wire) {
      if (time1 === 0) {
        t.ok(wire, 'Peer joined via listening port')
        t.equal(swarm1.wires.length, 1)

        // at some point in future, end wire
        setTimeout(function () {
          wire.destroy()
        }, 100)

        // ...and prevent reconnect
        swarm1._drain = function () {}
      } else if (time1 === 1) {
        t.ok(wire, 'Remote peer reconnected')
        t.equal(swarm1.wires.length, 1)
      } else {
        throw new Error('too many wire events (1)')
      }
      time1 += 1
    })

    var time2 = 0
    swarm2.on('wire', function (wire) {
      if (time2 === 0) {
        t.ok(wire, 'Joined swarm, got wire')
        t.equal(swarm2.wires.length, 1)

        wire.on('end', function () {
          t.pass('Wire ended by remote peer')
          t.equal(swarm1.wires.length, 0)
        })
      } else if (time2 === 1) {
        t.ok(wire, 'Reconnected to remote peer')
        t.equal(swarm2.wires.length, 1)

        swarm1.destroy()
        swarm2.destroy()
      } else {
        throw new Error('too many wire events (2)')
      }
      time2 += 1
    })

    swarm2.addPeer('127.0.0.1:' + swarm1.address().port)
  })
})