From f12424272fb408937fe7f803c2c3e0d09d3aa250 Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 14 Jan 2019 16:09:47 +0100 Subject: [PATCH] Updated test --- test/behavior/sniff.test.js | 35 +++++++++++++++------- test/unit/connection-pool.test.js | 50 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 11 deletions(-) diff --git a/test/behavior/sniff.test.js b/test/behavior/sniff.test.js index c6ec1334f..22b84e2e2 100644 --- a/test/behavior/sniff.test.js +++ b/test/behavior/sniff.test.js @@ -37,17 +37,30 @@ test('Should update the connection pool', t => { const ids = Object.keys(nodes) for (var i = 0; i < hosts.length; i++) { const id = ids[i] - t.deepEqual(hosts[i], { - url: new URL(nodes[id].url), - id: id, - roles: { - master: true, - data: true, - ingest: true - }, - ssl: null, - agent: null - }) + // the first node will be an update of the existing one + if (id === 'node0') { + t.deepEqual(hosts[i], { + url: new URL(nodes[id].url), + id: id, + roles: { + master: true, + data: true, + ingest: true + } + }) + } else { + t.deepEqual(hosts[i], { + url: new URL(nodes[id].url), + id: id, + roles: { + master: true, + data: true, + ingest: true + }, + ssl: null, + agent: null + }) + } } t.strictEqual(client.connectionPool.connections.size, 4) diff --git a/test/unit/connection-pool.test.js b/test/unit/connection-pool.test.js index 37a9d3af2..5ca572ffe 100644 --- a/test/unit/connection-pool.test.js +++ b/test/unit/connection-pool.test.js @@ -30,6 +30,22 @@ test('API', t => { t.end() }) + t.test('addConnection (should store the auth data)', t => { + const pool = new ConnectionPool({ Connection }) + const href = 'http://localhost:9200/' + pool.addConnection('http://foo:bar@localhost:9200') + + t.ok(pool.connections.get(href) instanceof Connection) + t.strictEqual(pool.connections.get(href).status, Connection.statuses.ALIVE) + t.deepEqual(pool.dead, []) + t.deepEqual(pool._auth, { username: 'foo', password: 'bar' }) + + pool.addConnection('http://localhost:9201') + t.strictEqual(pool.connections.get('http://localhost:9201/').url.username, 'foo') + t.strictEqual(pool.connections.get('http://localhost:9201/').url.password, 'bar') + t.end() + }) + t.test('markDead', t => { const pool = new ConnectionPool({ Connection }) const href = 'http://localhost:9200/' @@ -381,6 +397,40 @@ test('API', t => { t.ok(pool.connections.get('a2').roles !== null) }) + t.test('Should not update existing connections (same url, different id)', t => { + t.plan(2) + class CustomConnectionPool extends ConnectionPool { + markAlive () { + t.fail('Should not be called') + } + } + const pool = new CustomConnectionPool({ Connection }) + pool.addConnection([{ + url: new URL('http://127.0.0.1:9200'), + id: 'http://127.0.0.1:9200/', + roles: { + master: true, + data: true, + ingest: true + } + }]) + + pool.update([{ + url: new URL('http://127.0.0.1:9200'), + id: 'a1', + roles: true + }]) + + // roles will never be updated, we only use it to do + // a dummy check to see if the connection has been updated + t.deepEqual(pool.connections.get('a1').roles, { + master: true, + data: true, + ingest: true + }) + t.strictEqual(pool.connections.get('http://127.0.0.1:9200/'), undefined) + }) + t.test('Add a new connection', t => { t.plan(2) const pool = new ConnectionPool({ Connection })