Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve coverage for balanced-pool #1296

Merged
merged 1 commit into from Mar 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
}
})