Client helpers (#1107)

* Added client helpers

* Updated test

* The search helper should return only the documents

* Added code comments

* Fixed bug

* Updated test

* Removed bulkSize and added flushBytes

* Updated test

* Added concurrency

* Updated test

* Added support for 429 handling in the scroll search helper

* Updated test

* Updated stats count

* Updated test

* Fix test

* Use client maxRetries as default

* Updated type definitions

* Refactored bulk helper to be more consistent with the client api

* Updated test

* Improved error handling, added refreshOnCompletion option and forward additinal options to the bulk api

* Updated type definitions

* Updated test

* Fixed test on Node v8

* Updated test

* Added TODO

* Updated docs

* Added Node v8 note

* Updated scripts

* Removed useless files

* Added helpers to integration test

* Fix cli argument position

* Moar fixes

* Test run elasticsearch in github actions

* Use master action version

* Add vm.max_map_count step

* Test new action setup

* Added Configure sysctl limits step

* Updated action to latest version

* Don't run helpers integration test in jenkins

* Run helpers integratino test also with Node v10

* Updated docs

* Updated docs

* Updated helpers type definitions

* Added test for helpers type definitions

* Added license header
This commit is contained in:
Tomas Della Vedova
2020-03-23 17:43:10 +01:00
committed by GitHub
parent 6c82a4967e
commit d7836a16af
24 changed files with 7654 additions and 11 deletions

View File

@ -4,6 +4,7 @@
'use strict'
const assert = require('assert')
const { Connection } = require('../../index')
const { TimeoutError } = require('../../lib/errors')
const intoStream = require('into-stream')
@ -99,6 +100,39 @@ class MockConnectionSniff extends Connection {
}
}
}
function buildMockConnection (opts) {
assert(opts.onRequest, 'Missing required onRequest option')
class MockConnection extends Connection {
request (params, callback) {
var { body, statusCode } = opts.onRequest(params)
if (typeof body !== 'string') {
body = JSON.stringify(body)
}
var aborted = false
const stream = intoStream(body)
stream.statusCode = statusCode || 200
stream.headers = {
'content-type': 'application/json;utf=8',
date: new Date().toISOString(),
connection: 'keep-alive',
'content-length': Buffer.byteLength(body)
}
process.nextTick(() => {
if (!aborted) {
callback(null, stream)
}
})
return {
abort: () => { aborted = true }
}
}
}
return MockConnection
}
function setStatusCode (path) {
const statusCode = Number(path.slice(1))
if (Number.isInteger(statusCode)) {
@ -111,5 +145,6 @@ module.exports = {
MockConnection,
MockConnectionTimeout,
MockConnectionError,
MockConnectionSniff
MockConnectionSniff,
buildMockConnection
}

View File

@ -38,11 +38,21 @@ function buildServer (handler, opts, cb) {
console.log('http server error', err)
process.exit(1)
})
server.listen(0, () => {
const port = server.address().port
debug(`Server '${serverId}' booted on port ${port}`)
cb(Object.assign({}, secureOpts, { port }), server)
})
if (cb === undefined) {
return new Promise((resolve, reject) => {
server.listen(0, () => {
const port = server.address().port
debug(`Server '${serverId}' booted on port ${port}`)
resolve([Object.assign({}, secureOpts, { port }), server])
})
})
} else {
server.listen(0, () => {
const port = server.address().port
debug(`Server '${serverId}' booted on port ${port}`)
cb(Object.assign({}, secureOpts, { port }), server)
})
}
}
module.exports = buildServer

View File

@ -4,12 +4,30 @@
'use strict'
const { promisify } = require('util')
const sleep = promisify(setTimeout)
const buildServer = require('./buildServer')
const buildCluster = require('./buildCluster')
const connection = require('./MockConnection')
async function waitCluster (client, waitForStatus = 'green', timeout = '50s', times = 0) {
if (!client) {
throw new Error('waitCluster helper: missing client instance')
}
try {
await client.cluster.health({ waitForStatus, timeout })
} catch (err) {
if (++times < 10) {
await sleep(5000)
return waitCluster(client, waitForStatus, timeout, times)
}
throw err
}
}
module.exports = {
buildServer,
buildCluster,
connection
connection,
waitCluster
}