Repository: fastify/fastify
File: test/listen.3.test.js (JavaScript)

Code before:
    const fastify = Fastify()
    t.after(() => fastify.close())

    const sockFile = `\\\\.\\pipe\\${(Math.random().toString(16) + '0000000').slice(2, 10)}-server-sock`

    await fastify.listen({ path: sockFile })
    t.assert.deepStrictEqual(fastify.addresses(), [sockFile])
    t.assert.strictEqual(fastify.server.address(), sockFile)
  })
}

test('listen without callback with (address)', async t => {
  t.plan(1)
  const fastify = Fastify()
  t.after(() => fastify.close())
  await fastify.listen({ port: 0 })
    .then(address => {
      t.assert.strictEqual(address, `http://${localhostForURL}:${fastify.server.address().port}`)
    })
})

test('double listen without callback rejects', async t => {
  t.plan(1)
  const fastify = Fastify()
  t.after(() => fastify.close())
  await fastify.listen({ port: 0 })
    .then(() => {
      fastify.listen({ port: 0 })
        .catch(err => {
          t.assert.ok(err)
        })
    })
    .catch(err => t.assert.ifError(err))
})

test('double listen without callback with (address)', async t => {
  t.plan(2)
  const fastify = Fastify()
  t.after(() => fastify.close())
  await fastify.listen({ port: 0 })
    .then(address => {
      t.assert.strictEqual(address, `http://${localhostForURL}:${fastify.server.address().port}`)
      fastify.listen({ port: 0 })
        .catch(err => {
          t.assert.ok(err)
        })
    })
    .catch(err => t.assert.ifError(err))
})


Review comment (bug):
This could become a flaky test:

- the inner `fastify.listen({ port: 0 })` does not return the promise to the parent promise chain (`await fastify.listen({ port: 0 })`) so we may hit a (rare) case where the first `listen` resolve the promise and the test fails because the `t.assert.ok(err)` is not yet executed.

To simplify it we should use the `done` callback instead

Code after:
    const fastify = Fastify()
    t.after(() => fastify.close())

    const sockFile = `\\\\.\\pipe\\${(Math.random().toString(16) + '0000000').slice(2, 10)}-server-sock`

    await fastify.listen({ path: sockFile })
    t.assert.deepStrictEqual(fastify.addresses(), [sockFile])
    t.assert.strictEqual(fastify.server.address(), sockFile)
  })
}

test('listen without callback with (address)', async t => {
  t.plan(1)
  const fastify = Fastify()
  t.after(() => fastify.close())
  const address = await fastify.listen({ port: 0 })
  t.assert.strictEqual(address, `http://${localhostForURL}:${fastify.server.address().port}`)
})

test('double listen without callback rejects', (t, done) => {
  t.plan(1)
  const fastify = Fastify()
  t.after(() => fastify.close())
  fastify.listen({ port: 0 })
    .then(() => {
      fastify.listen({ port: 0 })
        .catch(err => {
          t.assert.ok(err)
          done()
        })
    })
    .catch(err => t.assert.ifError(err))
})

test('double listen without callback with (address)', (t, done) => {
  t.plan(2)
  const fastify = Fastify()
  t.after(() => fastify.close())
  fastify.listen({ port: 0 })
    .then(address => {
      t.assert.strictEqual(address, `http://${localhostForURL}:${fastify.server.address().port}`)
      fastify.listen({ port: 0 })
        .catch(err => {
          t.assert.ok(err)
          done()
        })
    })
    .catch(err => t.assert.ifError(err))
})