<commit_before>var Client = require('../')
var common = require('./common')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var test = require('tape')

var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
var parsedTorrent = parseTorrent(torrent)
var peerId1 = new Buffer('01234567890123456789')
var peerId2 = new Buffer('12345678901234567890')
var peerId3 = new Buffer('23456789012345678901')
var port = 6881

function testClientStart (t, serverType) {
  t.plan(5)
  common.createServer(t, serverType, function (server, announceUrl) {
    parsedTorrent.announce = [ announceUrl ]
    var client = new Client(peerId1, port, parsedTorrent)

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

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

    client.once('update', function (data) {
      t.equal(data.announce, announceUrl)
      t.equal(typeof data.complete, 'number')
      t.equal(typeof data.incomplete, 'number')
    })

    client.once('peer', function () {
      t.pass('there is at least one peer')
      client.stop()

      client.once('update', function () {
        t.pass('got response to stop')
        server.close()
        client.destroy()
      })
    })

    client.start()
  })
}

test('http: client.start()', function (t) {
  testClientStart(t, 'http')
})

test('udp: client.start()', function (t) {
  testClientStart(t, 'udp')
})

function testClientStop (t, serverType) {
  t.plan(3)
  common.createServer(t, serverType, function (server, announceUrl) {
    parsedTorrent.announce = [ announceUrl ]
    var client = new Client(peerId1, port, parsedTorrent)

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

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

    client.start()

    setTimeout(function () {
      client.stop()

      client.once('update', function (data) {
        // receive one final update after calling stop
        t.equal(data.announce, announceUrl)
        t.equal(typeof data.complete, 'number')
        t.equal(typeof data.incomplete, 'number')

        server.close()
        client.destroy()
      })
    }, 1000)
  })
}

test('http: client.stop()', function (t) {
  testClientStop(t, 'http')
})

test('udp: client.stop()', function (t) {
  testClientStop(t, 'udp')
})

function testClientUpdate (t, serverType) {
  t.plan(4)
  common.createServer(t, serverType, function (server, announceUrl) {
    parsedTorrent.announce = [ announceUrl ]
    var client = new Client(peerId1, port, parsedTorrent)
    client.setInterval(2000)

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

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

    client.start()

    client.once('update', function () {
      // after interval (2s), we should get another update
      client.once('update', function (data) {
        // received an update!
        t.equal(data.announce, announceUrl)<commit_msg>fix test for setInterval() change<commit_after>var Client = require('../')
var common = require('./common')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var test = require('tape')

var torrent = fs.readFileSync(__dirname + '/torrents/bitlove-intro.torrent')
var parsedTorrent = parseTorrent(torrent)
var peerId1 = new Buffer('01234567890123456789')
var peerId2 = new Buffer('12345678901234567890')
var peerId3 = new Buffer('23456789012345678901')
var port = 6881

function testClientStart (t, serverType) {
  t.plan(5)
  common.createServer(t, serverType, function (server, announceUrl) {
    parsedTorrent.announce = [ announceUrl ]
    var client = new Client(peerId1, port, parsedTorrent)

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

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

    client.once('update', function (data) {
      t.equal(data.announce, announceUrl)
      t.equal(typeof data.complete, 'number')
      t.equal(typeof data.incomplete, 'number')
    })

    client.once('peer', function () {
      t.pass('there is at least one peer')
      client.stop()

      client.once('update', function () {
        t.pass('got response to stop')
        server.close()
        client.destroy()
      })
    })

    client.start()
  })
}

test('http: client.start()', function (t) {
  testClientStart(t, 'http')
})

test('udp: client.start()', function (t) {
  testClientStart(t, 'udp')
})

function testClientStop (t, serverType) {
  t.plan(3)
  common.createServer(t, serverType, function (server, announceUrl) {
    parsedTorrent.announce = [ announceUrl ]
    var client = new Client(peerId1, port, parsedTorrent)

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

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

    client.start()

    setTimeout(function () {
      client.stop()

      client.once('update', function (data) {
        // receive one final update after calling stop
        t.equal(data.announce, announceUrl)
        t.equal(typeof data.complete, 'number')
        t.equal(typeof data.incomplete, 'number')

        server.close()
        client.destroy()
      })
    }, 1000)
  })
}

test('http: client.stop()', function (t) {
  testClientStop(t, 'http')
})

test('udp: client.stop()', function (t) {
  testClientStop(t, 'udp')
})

function testClientUpdate (t, serverType) {
  t.plan(4)
  common.createServer(t, serverType, function (server, announceUrl) {
    parsedTorrent.announce = [ announceUrl ]
    var client = new Client(peerId1, port, parsedTorrent)
    client.setInterval(2000)

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

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

    client.start()

    client.once('update', function () {
      client.setInterval(2000)

      // after interval (2s), we should get another update
      client.once('update', function (data) {
        // received an update!
        t.equal(data.announce, announceUrl)
{'fraction_non_alphanumeric': 0.14966777408637874, 'fraction_numerical': 0.027906976744186046, 'mean_word_length': 3.1524137931034484, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5674282', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1924, 'n_words': 428}