Skip to content

Commit

Permalink
chore: improve coverage for balanced-pool (nodejs#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
is2ei authored and KhafraDev committed Jun 23, 2022
1 parent b5077b2 commit 0e4dccb
Showing 1 changed file with 59 additions and 38 deletions.
97 changes: 59 additions & 38 deletions test/balanced-pool.js
@@ -1,35 +1,54 @@
'use strict'

const { test } = require('tap')
const { BalancedPool, Client, errors, Pool } = require('..')
const { createServer } = require('http')
const { promisify } = require('util')
const { test } = require('tap')
const {
BalancedPool,
Client,
errors,
Pool
} = require('..')

test('throws when factory is not a function', (t) => {
t.plan(2)

test('upstream add/remove/get', async (t) => {
const client = new BalancedPool()
t.same(client.upstreams, [])
client.addUpstream('http://localhost:4242')
t.same(client.upstreams, ['http://localhost:4242'])
client.addUpstream('http://localhost:2424')
client.addUpstream('http://localhost:2424')
t.same(client.upstreams, ['http://localhost:4242', 'http://localhost:2424'])
client.removeUpstream('http://localhost:4242')
t.same(client.upstreams, ['http://localhost:2424'])
client.removeUpstream('http://localhost:2424')
t.same(client.upstreams, [])

client.addUpstream('http://localhost:80')
client.addUpstream('http://localhost:80')
client.addUpstream(new URL('http://localhost:80'))
t.same(client.upstreams, ['http://localhost'])
client.removeUpstream('http://localhost:80')
t.same(client.upstreams, [])

t.throws(() => client.dispatch())

const p = client.close()
t.ok(p instanceof Promise)
await p
try {
new BalancedPool(null, { factory: '' }) // eslint-disable-line
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'factory must be a function.')
}
})

test('add/remove upstreams', (t) => {
t.plan(7)

const upstream01 = 'http://localhost:1'
const upstream02 = 'http://localhost:2'

const pool = new BalancedPool()
t.same(pool.upstreams, [])

// try to remove non-existent upstream
pool.removeUpstream(upstream01)
t.same(pool.upstreams, [])

pool.addUpstream(upstream01)
t.same(pool.upstreams, [upstream01])

// try to add the same upstream
pool.addUpstream(upstream01)
t.same(pool.upstreams, [upstream01])

pool.addUpstream(upstream02)
t.same(pool.upstreams, [upstream01, upstream02])

pool.removeUpstream(upstream02)
t.same(pool.upstreams, [upstream01])

pool.removeUpstream(upstream01)
t.same(pool.upstreams, [])
})

test('basic get', async (t) => {
Expand Down Expand Up @@ -171,17 +190,6 @@ test('busy', (t) => {
})
})

test('invalid options throws', (t) => {
t.plan(2)

try {
new BalancedPool(null, { factory: '' }) // eslint-disable-line
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'factory must be a function.')
}
})

test('factory option with basic get request', async (t) => {
t.plan(12)

Expand Down Expand Up @@ -229,3 +237,16 @@ test('factory option with basic get request', async (t) => {
t.equal(client.destroyed, true)
t.equal(client.closed, true)
})

test('throws when upstream is missing', async (t) => {
t.plan(2)

const pool = new BalancedPool()

try {
await pool.request({ path: '/', method: 'GET' })
} catch (e) {
t.type(e, errors.BalancedPoolMissingUpstreamError)
t.equal(e.message, 'No upstream has been added to the BalancedPool')
}
})

0 comments on commit 0e4dccb

Please sign in to comment.