From 4bf9128cac1afe2c4284c35af461e26284b169f3 Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 12 Dec 2018 19:48:27 +0100 Subject: [PATCH] Updated test --- test/benchmarks/basic.bench.js | 125 +++++++++++++++++++++++++++++++++ test/unit/serializer.test.js | 15 ++++ test/unit/transport.test.js | 56 +++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 test/benchmarks/basic.bench.js diff --git a/test/benchmarks/basic.bench.js b/test/benchmarks/basic.bench.js new file mode 100644 index 000000000..6a070ed8d --- /dev/null +++ b/test/benchmarks/basic.bench.js @@ -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() + }) + }) +}) diff --git a/test/unit/serializer.test.js b/test/unit/serializer.test.js index 4f6a491f6..369b94ca9 100644 --- a/test/unit/serializer.test.js +++ b/test/unit/serializer.test.js @@ -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() diff --git a/test/unit/transport.test.js b/test/unit/transport.test.js index 108ed0d14..70ceb7b20 100644 --- a/test/unit/transport.test.js +++ b/test/unit/transport.test.js @@ -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) {