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

torrent-events.js « node « test - github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be2adbc8c4e529905612d5c937d78cc825de8eda (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
const fixtures = require('webtorrent-fixtures')
const test = require('tape')
const WebTorrent = require('../../')
const MemoryChunkStore = require('memory-chunk-store')

test('client.add: emit torrent events in order', function (t) {
  t.plan(6)

  const client1 = new WebTorrent({ dht: false, tracker: false, lsd: false })
  const client2 = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client1.on('error', function (err) { t.fail(err) })
  client1.on('warning', function (err) { t.fail(err) })

  client2.on('error', function (err) { t.fail(err) })
  client2.on('warning', function (err) { t.fail(err) })

  // Start seeding
  client2.seed(fixtures.leaves.content, {
    name: 'Leaves of Grass by Walt Whitman.epub',
    announce: []
  })

  client2.on('listening', function () {
    // Start downloading
    const torrent = client1.add(fixtures.leaves.parsedTorrent.infoHash, { store: MemoryChunkStore })

    // Manually connect peers
    torrent.addPeer('127.0.0.1:' + client2.address().port)

    let order = 0

    torrent.on('infoHash', function () {
      t.equal(++order, 1)
    })

    torrent.on('metadata', function () {
      t.equal(++order, 2)
    })

    torrent.on('ready', function () {
      t.equal(++order, 3)
    })

    torrent.on('done', function () {
      t.equal(++order, 4)

      client1.destroy(function (err) { t.error(err, 'client 1 destroyed') })
      client2.destroy(function (err) { t.error(err, 'client 2 destroyed') })
    })
  })
})

test('client.seed: emit torrent events in order', function (t) {
  t.plan(5)

  const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

  client.on('error', function (err) { t.fail(err) })
  client.on('warning', function (err) { t.fail(err) })

  const torrent = client.seed(fixtures.leaves.content)

  let order = 0

  torrent.on('infoHash', function () {
    t.equal(++order, 1)
  })

  torrent.on('metadata', function () {
    t.equal(++order, 2)
  })

  torrent.on('ready', function () {
    t.equal(++order, 3)
  })

  torrent.on('done', function () {
    t.equal(++order, 4)

    client.destroy(function (err) { t.error(err, 'client destroyed') })
  })
})