From 53d91dbc2839f920a8f17120f0aab5e54dbad940 Mon Sep 17 00:00:00 2001 From: Issei Horie Date: Mon, 21 Mar 2022 22:23:08 +0900 Subject: [PATCH] chore: improve coverage for pool --- test/pool.js | 113 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 38 deletions(-) diff --git a/test/pool.js b/test/pool.js index 80ffc4baa2c..8cf7195bb21 100644 --- a/test/pool.js +++ b/test/pool.js @@ -1,16 +1,78 @@ 'use strict' -const proxyquire = require('proxyquire') -const { test } = require('tap') -const { Client, Pool, errors } = require('..') -const { createServer } = require('http') const { EventEmitter } = require('events') -const { promisify } = require('util') -const { PassThrough, Readable } = require('stream') -const { kBusy, kPending, kRunning, kSize, kUrl } = require('../lib/core/symbols') -const eos = require('stream').finished +const { createServer } = require('http') const net = require('net') -const EE = require('events') +const { + finished, + PassThrough, + Readable +} = require('stream') +const { promisify } = require('util') +const proxyquire = require('proxyquire') +const { test } = require('tap') +const { + kBusy, + kPending, + kRunning, + kSize, + kUrl +} = require('../lib/core/symbols') +const { + Client, + Pool, + errors +} = require('..') + +test('throws when connection is inifinite', (t) => { + t.plan(2) + + try { + new Pool(null, { connections: 0 / 0 }) // eslint-disable-line + } catch (e) { + t.type(e, errors.InvalidArgumentError) + t.equal(e.message, 'invalid connections') + } +}) + +test('throws when connections is negative', (t) => { + t.plan(2) + + try { + new Pool(null, { connections: -1 }) // eslint-disable-line no-new + } catch (e) { + t.type(e, errors.InvalidArgumentError) + t.equal(e.message, 'invalid connections') + } +}) + +test('throws when connection is not number', (t) => { + t.plan(2) + + try { + new Pool(null, { connections: true }) // eslint-disable-line no-new + } catch (e) { + t.type(e, errors.InvalidArgumentError) + t.equal(e.message, 'invalid connections') + } +}) + +test('throws when factory is not a function', (t) => { + t.plan(2) + + try { + new Pool(null, { factory: '' }) // eslint-disable-line no-new + } catch (e) { + t.type(e, errors.InvalidArgumentError) + t.equal(e.message, 'factory must be a function.') + } +}) + +test('does not throw when connect is a function', (t) => { + t.plan(1) + + t.doesNotThrow(() => new Pool('http://localhost', { connect: () => {} })) +}) test('connect/disconnect event(s)', (t) => { const clients = 2 @@ -186,7 +248,7 @@ test('basic get with async/await', async (t) => { t.equal(headers['content-type'], 'text/plain') body.resume() - await promisify(eos)(body) + await promisify(finished)(body) await client.close() await client.destroy() @@ -396,31 +458,6 @@ test('busy', (t) => { }) }) -test('invalid options throws', (t) => { - t.plan(6) - - try { - new Pool(null, { connections: -1 }) // eslint-disable-line - } catch (err) { - t.type(err, errors.InvalidArgumentError) - t.equal(err.message, 'invalid connections') - } - - try { - new Pool(null, { connections: true }) // eslint-disable-line - } catch (err) { - t.type(err, errors.InvalidArgumentError) - t.equal(err.message, 'invalid connections') - } - - try { - new Pool(null, { factory: '' }) // eslint-disable-line - } catch (err) { - t.type(err, errors.InvalidArgumentError) - t.equal(err.message, 'factory must be a function.') - } -}) - test('invalid pool dispatch options', (t) => { t.plan(2) const pool = new Pool('http://notahost') @@ -741,7 +778,7 @@ test('pool request abort in queue', (t) => { } }) - const signal = new EE() + const signal = new EventEmitter() client.request({ path: '/', method: 'GET', @@ -786,7 +823,7 @@ test('pool stream abort in queue', (t) => { } }) - const signal = new EE() + const signal = new EventEmitter() client.stream({ path: '/', method: 'GET', @@ -831,7 +868,7 @@ test('pool pipeline abort in queue', (t) => { } }) - const signal = new EE() + const signal = new EventEmitter() client.pipeline({ path: '/', method: 'GET',