Updated test

This commit is contained in:
delvedor
2018-12-12 19:48:27 +01:00
parent f6020e68a4
commit 4bf9128cac
3 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,125 @@
'use strict'
const bench = require('nanobench')
const { Client } = require('../../index')
const { connection } = require('../utils')
bench('Initialization', { repetitions: 5 }, b => {
const client = new Client({ // eslint-disable-line
node: 'http://localhost:9200'
})
b.end()
})
bench('Call api with lazy loading', { repetitions: 5 }, b => {
const client = new Client({
node: 'http://localhost:9200',
Connection: connection.MockConnection
})
b.start()
client.info((err, result) => {
if (err) {
b.error(err)
return
}
b.end()
})
})
bench('Call api without lazy loading', { repetitions: 5 }, b => {
const client = new Client({
node: 'http://localhost:9200',
Connection: connection.MockConnection
})
client.info((err, result) => {
if (err) {
b.error(err)
return
}
b.start()
client.info((err, result) => {
if (err) {
b.error(err)
return
}
b.end()
})
})
})
bench('Basic get', { repetitions: 5 }, b => {
const client = new Client({
node: 'http://localhost:9200',
Connection: connection.MockConnection
})
// we run the method twice to skip the lazy loading overhead
client.search({
index: 'test',
type: 'doc',
q: 'foo:bar'
}, (err, result) => {
if (err) {
b.error(err)
return
}
b.start()
client.search({
index: 'test',
type: 'doc',
q: 'foo:bar'
}, (err, result) => {
if (err) {
b.error(err)
return
}
b.end()
})
})
})
bench('Basic post', { repetitions: 5 }, b => {
const client = new Client({
node: 'http://localhost:9200',
Connection: connection.MockConnection
})
// we run the method twice to skip the lazy loading overhead
client.search({
index: 'test',
type: 'doc',
body: {
query: {
match: { foo: 'bar' }
}
}
}, (err, result) => {
if (err) {
b.error(err)
return
}
b.start()
client.search({
index: 'test',
type: 'doc',
body: {
query: {
match: { foo: 'bar' }
}
}
}, (err, result) => {
if (err) {
b.error(err)
return
}
b.end()
})
})
})

View File

@ -88,6 +88,21 @@ test('qserialize (string)', t => {
)
})
test('qserialize (key with undefined value)', t => {
t.plan(1)
const s = new Serializer()
const obj = {
hello: 'world',
key: undefined,
foo: 'bar'
}
t.strictEqual(
s.qserialize(obj),
'hello=world&foo=bar'
)
})
test('SerializationError', t => {
t.plan(1)
const s = new Serializer()

View File

@ -53,6 +53,62 @@ test('Basic', t => {
})
})
test('Basic (promises support)', t => {
t.plan(1)
const pool = new ConnectionPool({ Connection: MockConnection })
pool.addConnection('http://localhost:9200')
const transport = new Transport({
emit: () => {},
connectionPool: pool,
serializer: new Serializer(),
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false
})
transport
.request({
method: 'GET',
path: '/hello'
})
.then(({ body }) => {
t.deepEqual(body, { hello: 'world' })
})
.catch(t.fail)
})
test('Basic (options + promises support)', t => {
t.plan(1)
const pool = new ConnectionPool({ Connection: MockConnection })
pool.addConnection('http://localhost:9200')
const transport = new Transport({
emit: () => {},
connectionPool: pool,
serializer: new Serializer(),
maxRetries: 3,
requestTimeout: 30000,
sniffInterval: false,
sniffOnStart: false
})
transport
.request({
method: 'GET',
path: '/hello'
}, {
requestTimeout: 1000
})
.then(({ body }) => {
t.deepEqual(body, { hello: 'world' })
})
.catch(t.fail)
})
test('Send POST', t => {
t.plan(4)
function handler (req, res) {