WIP: Added some basic unit tests
- Added unit test - Added test fixtures - Added test utils
This commit is contained in:
137
test/unit/connection-pool.test.js
Normal file
137
test/unit/connection-pool.test.js
Normal file
@ -0,0 +1,137 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { URL } = require('url')
|
||||
const ConnectionPool = require('../../lib/ConnectionPool')
|
||||
const Connection = require('../../lib/Connection')
|
||||
const { RoundRobinSelector } = require('../../lib/Selectors')
|
||||
|
||||
test('API', t => {
|
||||
t.test('addConnection', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
pool.addConnection(href)
|
||||
t.ok(pool.connections.get(href) instanceof Connection)
|
||||
t.deepEqual(pool.alive, [href])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('markDead', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
pool.markDead(connection)
|
||||
connection = pool.connections.get(href)
|
||||
t.strictEqual(connection.deadCount, 1)
|
||||
t.true(connection.resurrectTimeout > 0)
|
||||
t.deepEqual(pool.alive, [])
|
||||
t.deepEqual(pool.dead, [href])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('markAlive', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
pool.markDead(connection)
|
||||
pool.markAlive(connection)
|
||||
connection = pool.connections.get(href)
|
||||
t.strictEqual(connection.deadCount, 0)
|
||||
t.strictEqual(connection.resurrectTimeout, 0)
|
||||
t.deepEqual(pool.alive, [href])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('resurrect', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
pool.markDead(connection)
|
||||
connection = pool.resurrect(Date.now() + 1000 * 60 * 3)
|
||||
t.strictEqual(connection.deadCount, 1)
|
||||
t.true(connection.resurrectTimeout > 0)
|
||||
t.deepEqual(pool.alive, [href])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('getConnection', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
pool.addConnection(href)
|
||||
t.ok(pool.getConnection() instanceof Connection)
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('removeConnection', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
t.ok(pool.getConnection() instanceof Connection)
|
||||
pool.removeConnection(connection)
|
||||
t.strictEqual(pool.getConnection(), null)
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('empty', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
pool.addConnection('http://localhost:9200/')
|
||||
pool.addConnection('http://localhost:9201/')
|
||||
pool.empty()
|
||||
t.strictEqual(pool.connections.size, 0)
|
||||
t.deepEqual(pool.alive, [])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('urlToHost', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const url = 'http://localhost:9200'
|
||||
t.deepEqual(
|
||||
pool.urlToHost(url),
|
||||
{ host: new URL(url) }
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('nodesToHost', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const nodes = {
|
||||
a1: {
|
||||
http: {
|
||||
publish_address: '127.0.0.1:9200'
|
||||
},
|
||||
roles: ['master', 'data', 'ingest']
|
||||
},
|
||||
a2: {
|
||||
http: {
|
||||
publish_address: '127.0.0.1:9202'
|
||||
},
|
||||
roles: ['master', 'data', 'ingest']
|
||||
}
|
||||
}
|
||||
|
||||
t.deepEqual(pool.nodesToHost(nodes), [{
|
||||
host: new URL('http://127.0.0.1:9200'),
|
||||
id: 'a1',
|
||||
roles: {
|
||||
master: true,
|
||||
data: true,
|
||||
ingest: true
|
||||
}
|
||||
}, {
|
||||
host: new URL('http://127.0.0.1:9201'),
|
||||
id: 'a2',
|
||||
roles: {
|
||||
master: true,
|
||||
data: true,
|
||||
ingest: true
|
||||
}
|
||||
}])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
194
test/unit/connection.test.js
Normal file
194
test/unit/connection.test.js
Normal file
@ -0,0 +1,194 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { buildServer } = require('../utils')
|
||||
const Connection = require('../../lib/Connection')
|
||||
|
||||
test('Basic (http)', t => {
|
||||
t.plan(4)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `http://localhost:${port}`,
|
||||
protocol: 'http:'
|
||||
}
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
|
||||
var payload = ''
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', chunk => { payload += chunk })
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Basic (https)', t => {
|
||||
t.plan(4)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, { secure: true }, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `https://localhost:${port}`,
|
||||
protocol: 'https:'
|
||||
}
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
|
||||
var payload = ''
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', chunk => { payload += chunk })
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Basic (https with ssl agent)', t => {
|
||||
t.plan(4)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, { secure: true }, ({ port, key, cert }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `https://localhost:${port}`,
|
||||
protocol: 'https:'
|
||||
},
|
||||
ssl: { key, cert }
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
|
||||
var payload = ''
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', chunk => { payload += chunk })
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Disable keep alive', t => {
|
||||
t.plan(3)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'close'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `http://localhost:${port}`,
|
||||
protocol: 'http:'
|
||||
},
|
||||
agent: { keepAlive: false }
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'close'
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Timeout support', t => {
|
||||
t.plan(1)
|
||||
|
||||
function handler (req, res) {
|
||||
setTimeout(
|
||||
() => res.end('ok'),
|
||||
1000
|
||||
)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `http://localhost:${port}`,
|
||||
protocol: 'http:'
|
||||
}
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
timeout: 500
|
||||
}, (err, res) => {
|
||||
t.ok(err.message, 'Request timed out')
|
||||
})
|
||||
})
|
||||
})
|
||||
24
test/unit/selectors.test.js
Normal file
24
test/unit/selectors.test.js
Normal file
@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { RoundRobinSelector, RandomSelector } = require('../../lib/Selectors')
|
||||
|
||||
test('RoundRobinSelector', t => {
|
||||
const s = new RoundRobinSelector()
|
||||
const arr = [0, 1, 2, 3, 4, 5]
|
||||
|
||||
t.plan(arr.length + 1)
|
||||
for (var i = 0; i <= arr.length; i++) {
|
||||
t.strictEqual(
|
||||
s.select(arr),
|
||||
i === arr.length ? arr[0] : arr[i]
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
test('RandomSelector', t => {
|
||||
t.plan(1)
|
||||
const s = new RandomSelector()
|
||||
const arr = [0, 1, 2, 3, 4, 5]
|
||||
t.type(s.select(arr), 'number')
|
||||
})
|
||||
39
test/unit/serializer.test.js
Normal file
39
test/unit/serializer.test.js
Normal file
@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const Serializer = require('../../lib/Serializer')
|
||||
const { SerializationError, DeserializationError } = require('../../lib/errors')
|
||||
|
||||
test('Basic', t => {
|
||||
t.plan(2)
|
||||
const s = new Serializer()
|
||||
const obj = { hello: 'world' }
|
||||
const json = JSON.stringify(obj)
|
||||
t.strictEqual(s.serialize(obj), json)
|
||||
t.deepEqual(s.deserialize(json), obj)
|
||||
})
|
||||
|
||||
test('SerializationError', t => {
|
||||
t.plan(1)
|
||||
const s = new Serializer()
|
||||
const obj = { hello: 'world' }
|
||||
obj.o = obj
|
||||
try {
|
||||
s.serialize(obj)
|
||||
t.fail('Should fail')
|
||||
} catch (err) {
|
||||
t.ok(err instanceof SerializationError)
|
||||
}
|
||||
})
|
||||
|
||||
test('DeserializationError', t => {
|
||||
t.plan(1)
|
||||
const s = new Serializer()
|
||||
const json = '{"hello'
|
||||
try {
|
||||
s.deserialize(json)
|
||||
t.fail('Should fail')
|
||||
} catch (err) {
|
||||
t.ok(err instanceof DeserializationError)
|
||||
}
|
||||
})
|
||||
471
test/unit/transport.test.js
Normal file
471
test/unit/transport.test.js
Normal file
@ -0,0 +1,471 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { URL } = require('url')
|
||||
const { buildServer } = require('../utils')
|
||||
const {
|
||||
NoLivingConnectionsError,
|
||||
SerializationError,
|
||||
DeserializationError,
|
||||
TimeoutError,
|
||||
ResponseError
|
||||
} = require('../../lib/errors')
|
||||
|
||||
const ConnectionPool = require('../../lib/ConnectionPool')
|
||||
const Serializer = require('../../lib/Serializer')
|
||||
const Transport = require('../../lib/Transport')
|
||||
const { RoundRobinSelector } = require('../../lib/Selectors')
|
||||
|
||||
test('Basic', t => {
|
||||
t.plan(2)
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Send POST', t => {
|
||||
t.plan(4)
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'content-type': 'application/json',
|
||||
'content-length': '17'
|
||||
})
|
||||
var json = ''
|
||||
req.setEncoding('utf8')
|
||||
req.on('data', chunk => { json += chunk })
|
||||
req.on('error', err => t.fail(err))
|
||||
req.on('end', () => {
|
||||
t.deepEqual(JSON.parse(json), { hello: 'world' })
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
})
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
path: '/hello',
|
||||
body: { hello: 'world' }
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('NoLivingConnectionsError', t => {
|
||||
t.plan(1)
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof NoLivingConnectionsError)
|
||||
})
|
||||
})
|
||||
|
||||
test('SerializationError', t => {
|
||||
t.plan(1)
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection('http://localhost:9200')
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
const body = { hello: 'world' }
|
||||
body.o = body
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
path: '/hello',
|
||||
body
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof SerializationError)
|
||||
})
|
||||
})
|
||||
|
||||
test('DeserializationError', t => {
|
||||
t.plan(1)
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end('{"hello)')
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof DeserializationError)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('TimeoutError (should call markDead on the failing connection)', t => {
|
||||
t.plan(2)
|
||||
|
||||
class CustomConnectionPool extends ConnectionPool {
|
||||
markDead (connection) {
|
||||
t.strictEqual(connection.id, 'node1')
|
||||
super.markDead(connection)
|
||||
}
|
||||
}
|
||||
|
||||
function handler (req, res) {
|
||||
setTimeout(() => {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new CustomConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 0,
|
||||
requestTimeout: 500,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Retry mechanism', t => {
|
||||
t.plan(2)
|
||||
|
||||
var count = 0
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
if (count++ === 1) {
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection([{
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
}, {
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node2'
|
||||
}])
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 1,
|
||||
requestTimeout: 500,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Should call markAlive with a successful response', t => {
|
||||
t.plan(3)
|
||||
|
||||
class CustomConnectionPool extends ConnectionPool {
|
||||
markAlive (connection) {
|
||||
t.strictEqual(connection.id, 'node1')
|
||||
super.markAlive(connection)
|
||||
}
|
||||
}
|
||||
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new CustomConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Should call resurrect on every request', t => {
|
||||
t.plan(3)
|
||||
|
||||
class CustomConnectionPool extends ConnectionPool {
|
||||
resurrect (now) {
|
||||
t.type(now, 'number')
|
||||
}
|
||||
}
|
||||
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new CustomConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Should return a request aborter utility', t => {
|
||||
t.plan(1)
|
||||
|
||||
function handler (req, res) {
|
||||
setTimeout(() => {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
const request = transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (_err, body) => {
|
||||
t.fail('Should not be called')
|
||||
})
|
||||
|
||||
request.abort()
|
||||
t.pass('ok')
|
||||
})
|
||||
})
|
||||
|
||||
test('ResponseError', t => {
|
||||
t.plan(3)
|
||||
|
||||
function handler (req, res) {
|
||||
res.statusCode = 500
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ status: 500 }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof ResponseError)
|
||||
t.deepEqual(err.response, { status: 500 })
|
||||
t.strictEqual(err.statusCode, 500)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Override requestTimeout', t => {
|
||||
t.plan(2)
|
||||
function handler (req, res) {
|
||||
setTimeout(() => {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 500,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello',
|
||||
timeout: 2000
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user