diff --git a/test/balanced-pool.js b/test/balanced-pool.js index eeec137590e..b677a68af1b 100644 --- a/test/balanced-pool.js +++ b/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) => { @@ -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) @@ -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') + } +})