const tape = require('tape')
const utp = require('../')

tape('bind', function (t) {
  const sock = utp()

  sock.bind(function () {
    const { port, address } = sock.address()
    t.same(address, '0.0.0.0')
    t.same(typeof port, 'number')
    t.ok(port > 0 && port < 65536)
    sock.close(() => t.end())
  })
})

tape('bind, close, bind', function (t) {
  const sock = utp()

  sock.bind(function () {
    const { port, address } = sock.address()
    t.same(address, '0.0.0.0')
    t.same(typeof port, 'number')
    t.ok(port > 0 && port < 65536)
    sock.close(function () {
      const otherSock = utp()

      otherSock.bind(port, function () {
        const addr = otherSock.address()
        t.same(addr.port, port)
        t.same(addr.address, address)
        otherSock.close(() => t.end())
      })
    })
  })
})

tape('send message', function (t) {
  const sock = utp()

  sock.bind(0, '127.0.0.1', function () {
    const addr = sock.address()

    sock.on('message', function (message, rinfo) {
      t.same(rinfo, addr)
      t.same(message, Buffer.from('hello'))
      sock.close(() => t.end())
    })

    sock.send(Buffer.from('hello'), 0, 5, addr.port, addr.address, function (err) {
      t.error(err, 'no error')
    })
  })
})

@@ -1228,24 +1228,477 @@
  error')%0A    %7D)%0A  %7D)%0A%7D)%0A
+%0Atape('send after close', function (t) %7B%0A  const sock = utp()%0A%0A  sock.bind(0, '127.0.0.1', function () %7B%0A    const %7B port, address %7D = sock.address()%0A    sock.send(Buffer.from('hello'), 0, 5, port, address, function (err) %7B%0A      t.error(err, 'no error')%0A      sock.close(function () %7B%0A        sock.send(Buffer.from('world'), 0, 5, port, address, function (err) %7B%0A          t.ok(err, 'should error')%0A          t.end()%0A        %7D)%0A      %7D)%0A    %7D)%0A  %7D)%0A%7D)%0A

add send after close test