diff --git a/test/unit/connection-pool.test.js b/test/unit/connection-pool.test.js index cf054aade..734877d1f 100644 --- a/test/unit/connection-pool.test.js +++ b/test/unit/connection-pool.test.js @@ -490,3 +490,80 @@ test('API', t => { t.end() }) + +test('Node selector', t => { + t.test('round-robin', t => { + t.plan(1) + const pool = new ConnectionPool({ Connection, nodeSelector: 'round-robin' }) + pool.addConnection('http://localhost:9200/') + t.true(pool.getConnection() instanceof Connection) + }) + + t.test('random', t => { + t.plan(1) + const pool = new ConnectionPool({ Connection, nodeSelector: 'random' }) + pool.addConnection('http://localhost:9200/') + t.true(pool.getConnection() instanceof Connection) + }) + + t.test('custom function', t => { + t.plan(2) + const nodeSelector = connections => { + t.ok('called') + return connections[0] + } + const pool = new ConnectionPool({ Connection, nodeSelector }) + pool.addConnection('http://localhost:9200/') + t.true(pool.getConnection() instanceof Connection) + }) + + t.end() +}) + +test('Node filter', t => { + t.test('default', t => { + t.plan(1) + const pool = new ConnectionPool({ Connection }) + pool.addConnection({ url: new URL('http://localhost:9200/') }) + t.true(pool.getConnection() instanceof Connection) + }) + + t.test('Should filter master only nodes', t => { + t.plan(1) + const pool = new ConnectionPool({ Connection }) + pool.addConnection({ + url: new URL('http://localhost:9200/'), + roles: { + master: true, + data: false, + ingest: false, + ml: false + } + }) + t.strictEqual(pool.getConnection(), null) + }) + + t.test('custom', t => { + t.plan(2) + const nodeFilter = node => { + t.ok('called') + return true + } + const pool = new ConnectionPool({ Connection, nodeFilter }) + pool.addConnection({ url: new URL('http://localhost:9200/') }) + t.true(pool.getConnection() instanceof Connection) + }) + + t.test('custom (filter)', t => { + t.plan(2) + const nodeFilter = node => { + t.ok('called') + return false + } + const pool = new ConnectionPool({ Connection, nodeFilter }) + pool.addConnection({ url: new URL('http://localhost:9200/') }) + t.strictEqual(pool.getConnection(), null) + }) + + t.end() +}) diff --git a/test/unit/connection.test.js b/test/unit/connection.test.js index 8eb248c9b..92d6c6921 100644 --- a/test/unit/connection.test.js +++ b/test/unit/connection.test.js @@ -591,3 +591,63 @@ test('Should disallow two-byte characters in URL path', t => { ) }) }) + +test('setRole', t => { + t.test('Update the value of a role', t => { + t.plan(2) + + const connection = new Connection({ + url: new URL('http://localhost:9200') + }) + + t.deepEqual(connection.roles, { + master: true, + data: true, + ingest: true, + ml: false + }) + + connection.setRole('master', false) + + t.deepEqual(connection.roles, { + master: false, + data: true, + ingest: true, + ml: false + }) + }) + + t.test('Invalid role', t => { + t.plan(2) + + const connection = new Connection({ + url: new URL('http://localhost:9200') + }) + + try { + connection.setRole('car', true) + t.fail('Shoud throw') + } catch (err) { + t.true(err instanceof ConfigurationError) + t.is(err.message, 'Unsupported role: \'car\'') + } + }) + + t.test('Invalid value', t => { + t.plan(2) + + const connection = new Connection({ + url: new URL('http://localhost:9200') + }) + + try { + connection.setRole('master', 1) + t.fail('Shoud throw') + } catch (err) { + t.true(err instanceof ConfigurationError) + t.is(err.message, 'enabled should be a boolean') + } + }) + + t.end() +}) diff --git a/test/unit/transport.test.js b/test/unit/transport.test.js index 45a2ada5d..3cc88c9ce 100644 --- a/test/unit/transport.test.js +++ b/test/unit/transport.test.js @@ -377,6 +377,32 @@ test('SerializationError', t => { }) }) +test('SerializationError (bulk)', t => { + t.plan(1) + const pool = new ConnectionPool({ Connection }) + pool.addConnection('http://localhost:9200') + + const transport = new Transport({ + emit: () => {}, + connectionPool: pool, + serializer: new Serializer(), + maxRetries: 3, + requestTimeout: 30000, + sniffInterval: false, + sniffOnStart: false + }) + + const bulkBody = { hello: 'world' } + bulkBody.o = bulkBody + transport.request({ + method: 'POST', + path: '/hello', + bulkBody + }, (err, { body }) => { + t.ok(err instanceof SerializationError) + }) +}) + test('DeserializationError', t => { t.plan(1) function handler (req, res) {