Skip to content

Commit

Permalink
Test double listen on the same fastify instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Sep 12, 2017
1 parent e87fe8d commit a8ec8ea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
3 changes: 3 additions & 0 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ function build (options) {
const _cb = (hasAddress) ? cb : address
fastify.ready(function (err) {
if (err) return _cb(err)
if (listening) {
return _cb(new Error('Fastify is already listening'))
}
if (hasAddress) {
server.listen(port, address, _cb)
} else {
Expand Down
18 changes: 17 additions & 1 deletion test/listen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@

const test = require('tap').test
const Fastify = require('..')
const fastify = Fastify()

test('listen accepts a port and a callback', t => {
t.plan(2)
const fastify = Fastify()
fastify.listen(0, (err) => {
fastify.server.unref()
t.error(err)
t.pass()
fastify.close()
})
})

test('listen accepts a port, address, and callback', t => {
t.plan(2)
const fastify = Fastify()
fastify.listen(0, '127.0.0.1', (err) => {
fastify.server.unref()
t.error(err)
t.pass()
fastify.close()
})
})

Expand All @@ -31,6 +34,7 @@ test('listen after Promise.resolve()', t => {
f.server.unref()
t.error(err)
t.pass()
f.close()
})
})
})
Expand All @@ -56,3 +60,15 @@ test('register after listen using Promise.resolve()', t => {
})
})
})

test('double listen errors', t => {
t.plan(2)
const fastify = Fastify()
fastify.listen(0, (err) => {
t.error(err)
fastify.listen(fastify.server.address().port, (err) => {
t.ok(err)
fastify.close()
})
})
})
9 changes: 2 additions & 7 deletions test/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,9 @@ fastify.listen(0, function (err) {
t.deepEqual(JSON.parse(body), { hello: 'world' })
})
})
})

test('path can be specified in place of uri', t => {
t.plan(3)

fastify.listen(0, function (err) {
if (err) t.error(err)
fastify.server.unref()
test('path can be specified in place of uri', t => {
t.plan(3)

fastify.route({
method: 'GET',
Expand Down

0 comments on commit a8ec8ea

Please sign in to comment.