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

rarity-map.js « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cde945a4a5faae12851a82d4f86b9ad01fb53df4 (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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
var fixtures = require('webtorrent-fixtures')
var randombytes = require('randombytes')
var test = require('tape')
var Torrent = require('../lib/torrent')
var Wire = require('bittorrent-protocol')

test('Rarity map usage', function (t) {
  t.plan(16)

  var numPieces = 4
  var torrentId = Object.assign({}, fixtures.numbers.parsedTorrent, {
    pieces: Array(numPieces)
  })
  var client = {
    listening: true,
    peerId: randombytes(20).toString('hex'),
    torrentPort: 6889,
    dht: false,
    tracker: false,
    _remove: function () {}
  }
  var opts = {}
  var torrent = new Torrent(torrentId, client, opts)
  torrent.on('metadata', function () {
    torrent._onWire(new Wire())
    torrent._onWire(new Wire())

    var rarityMap = torrent._rarityMap

    // test initial / empty case
    validateInitial()

    rarityMap.recalculate()

    // test initial / empty case after recalc
    validateInitial()

    setPiece(torrent.wires[0], 0)
    setPiece(torrent.wires[1], 0)

    setPiece(torrent.wires[0], 1)
    setPiece(torrent.wires[1], 3)

    // test rarest piece after setting pieces and handling 'have' events
    var piece = rarityMap.getRarestPiece()
    t.equal(piece, 2)

    rarityMap.recalculate()

    // test rarest piece after recalc to ensure its the same
    piece = rarityMap.getRarestPiece()
    t.equal(piece, 2)

    addWire()
    addWire()

    // test rarest piece after adding wires
    piece = rarityMap.getRarestPiece()
    t.equal(piece, 3)

    rarityMap.recalculate()

    // test rarest piece after adding wires and recalc
    piece = rarityMap.getRarestPiece()
    t.equal(piece, 3)

    removeWire(3)
    removeWire(1)

    // test rarest piece after removing wires
    piece = rarityMap.getRarestPiece()
    t.equal(piece, 3)

    rarityMap.recalculate()

    // test rarest piece after removing wires and recalc
    piece = rarityMap.getRarestPiece()
    t.equal(piece, 3)

    // test piece filter func
    piece = rarityMap.getRarestPiece(function (i) { return i <= 1 })
    t.equal(piece, 0)

    piece = rarityMap.getRarestPiece(function (i) { return i === 1 || i === 2 })
    t.equal(piece, 2)

    function validateInitial () {
      // note that getRarestPiece will return a random piece since they're all equal
      // so repeat the test several times to reasonably ensure its correctness.
      var piece = rarityMap.getRarestPiece()
      t.ok(piece >= 0 && piece < numPieces)

      piece = rarityMap.getRarestPiece()
      t.ok(piece >= 0 && piece < numPieces)

      piece = rarityMap.getRarestPiece()
      t.ok(piece >= 0 && piece < numPieces)

      piece = rarityMap.getRarestPiece()
      t.ok(piece >= 0 && piece < numPieces)
    }

    function setPiece (wire, index) {
      wire.peerPieces.set(index)
      wire.emit('have', index)
    }

    function addWire () {
      var wire = new Wire()
      wire.peerPieces.set(1)
      wire.peerPieces.set(2)
      torrent._onWire(wire)
    }

    function removeWire (index) {
      var wire = torrent.wires.splice(index, 1)[0]
      wire.destroy()
    }
  })

  t.on('end', function () {
    torrent.wires.forEach(function (wire) {
      wire.destroy()
    })
    torrent.destroy()
  })
})