<commit_before>var test = require('tape')
var _ = require('icebreaker')
var listen = require('../../').listen
var connect = require('../../').connect
var map = require('../../').map
var on = require('../../').on
var os = require('os')
var serializer = require('pull-serializer')
var goodbye = require('pull-goodbye')
try {
  require('utp-native')
}
catch (err) {
  return
}

function onConnection(l, t) {
  return function (c) {
    _(c, serializer(goodbye({
      source: _('world'),
      sink: _.drain(function (d) {
        t.equal(d.toString(), 'hello', 'equal')
      }, l.end)
    }, 'GoodBye')), c)
    return c
  }
}

function onConnect(l, t) {
  return function (connection) {
    _(
      connection,
      serializer(goodbye({
        source: _('hello'),
        sink: _.drain(function (d) {
          t.equal(d.toString(), 'world', 'equals')
        }, function (err) {
          t.notOk(err)
          l.end()
        })
      }, 'GoodBye')),
      connection
    )
    return connection
  }
}

test('utp', function (t) {
  t.plan(15)

  var ziggy = listen('udp://127.0.0.1:8090')
  var lastInfo
  var c = 0

  var ce = 0
  _(ziggy, on({
    ready: function (e) {
      t.equal(e.localPort, 8090)
      var handle = {
        end: function (err) {
          t.notOk(err)
          ++c
          if (c === 4) {
            alice.end()
            bob.end()
          }
        }
      }

      var handle2 = {
        end: function (err) {
          t.notOk(err)
          ++ce
          if (ce === 2) ziggy.end()
        }
      }

      function createClient(port, e, t) {
        var l = listen('utp://127.0.0.1:' + port)
        var s = _(l, map({
          ready: function (e2) {
            l.push(Buffer('ping'), e.localPort, e.localAddress)
            t.equal(e2.localPort, port)
            return e2
          },
          message: function (msg) {
            var d = JSON.parse(msg.data)
            connect('utp://' + d.host + ':' + d.port, onConnect(handle, t))
            return msg
          },
          connection: onConnection(handle, t)
        }))
        s.end = l.end
        return s
      }

      var alice = createClient(8091, e, t)
      _(alice, on(handle2))

      var bob = createClient(8092, e, t)
      _(bob, on(handle2))
      
    },
    message: function (msg) {
      t.equal(msg.data.toString(), 'ping')
      if (lastInfo) {
        ziggy.push(JSON.stringify(lastInfo), msg.remotePort, msg.remoteAddress)
        ziggy.push(JSON.stringify({ port: msg.remotePort, host: msg.remoteAddress }), lastInfo.port, lastInfo.host)
      }
      lastInfo = { port: msg.remotePort, host: msg.remoteAddress }
    },
    end: t.notOk
  }))
})<commit_msg>Fixes for utp test<commit_after>var test = require('tape')
var _ = require('icebreaker')
var listen = require('../../').listen
var connect = require('../../').connect
var map = require('../../').map
var on = require('../../').on
var os = require('os')
var serializer = require('pull-serializer')
var goodbye = require('pull-goodbye')
try {
  require('utp-native')
}
catch (err) {
  return
}

function onConnection(l, t) {
  return function (c) {
    _(c, serializer(goodbye({
      source: _('world'),
      sink: _.drain(function (d) {
        t.equal(d.toString(), 'hello', 'equal')
      }, l.end)
    }, 'GoodBye')), c)
    return c
  }
}

function onConnect(l, t) {
  return function (connection) {
    _(
      connection,
      serializer(goodbye({
        source: _('hello'),
        sink: _.drain(function (d) {
          t.equal(d.toString(), 'world', 'equals')
        }, l.end)
      }, 'GoodBye')),
      connection
    )
    return connection
  }
}

test('utp', function (t) {
  t.plan(16)

  var ziggy = listen('udp://127.0.0.1:8091')
  var lastInfo
  var c = 0

  var ce = 0
  _(ziggy, on({
    ready: function (e) {
      t.equal(e.localPort, 8091)
      var handle = {
        end: function (err) {
          t.notOk(err)
          ++c
          if (c === 4) {
            alice.end()
            bob.end()
          }
        }
      }

      var handle2 = {
        end: function (err) {
          t.notOk(err)
          ++ce
          if (ce === 2) ziggy.end()
        }
      }

      function createClient(port, e, t) {
        var l = listen('utp://127.0.0.1:' + port)
        var s = _(l, map({
          ready: function (e2) {
            l.push(Buffer('ping'), e.localPort, e.localAddress)
            t.equal(e2.localPort, port)
            return e2
          },
          message: function (msg) {
            var d = JSON.parse(msg.data)
            connect('utp://' + d.host + ':' + d.port, onConnect(handle, t))
            return msg
          },
          connection: onConnection(handle, t)
        }))
        s.end = l.end
        return s
      }

      var alice = createClient(8092, e, t)
      _(alice, on(handle2))

      var bob = createClient(8093, e, t)
      _(bob, on(handle2))

    },
    message: function (msg) {
      t.equal(msg.data.toString(), 'ping')
      if (lastInfo) {
        ziggy.push(JSON.stringify(lastInfo), msg.remotePort, msg.remoteAddress)
        ziggy.push(JSON.stringify({ port: msg.remotePort, host: msg.remoteAddress }), lastInfo.port, lastInfo.host)
      }
      lastInfo = { port: msg.remotePort, host: msg.remoteAddress }
    },
    end: t.notOk
  }))
})
{'fraction_non_alphanumeric': 0.16801201426694198, 'fraction_numerical': 0.015017833677492022, 'mean_word_length': 1.8310308182784272, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '1728031', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1782, 'n_words': 402}