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 pool #1297

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
113 changes: 75 additions & 38 deletions 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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -741,7 +778,7 @@ test('pool request abort in queue', (t) => {
}
})

const signal = new EE()
const signal = new EventEmitter()
client.request({
path: '/',
method: 'GET',
Expand Down Expand Up @@ -786,7 +823,7 @@ test('pool stream abort in queue', (t) => {
}
})

const signal = new EE()
const signal = new EventEmitter()
client.stream({
path: '/',
method: 'GET',
Expand Down Expand Up @@ -831,7 +868,7 @@ test('pool pipeline abort in queue', (t) => {
}
})

const signal = new EE()
const signal = new EventEmitter()
client.pipeline({
path: '/',
method: 'GET',
Expand Down