From 7e35f6a9037bc1f33cfd34bb944643176ead8c71 Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 5 Dec 2018 17:29:21 +0100 Subject: [PATCH] Updated behavior test --- package.json | 4 ++-- test/behavior/sniff.test.js | 42 +++++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 4d4b9c666..8c20128fe 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,8 @@ ], "scripts": { "test": "npm run lint && npm run test:unit && npm run test:behavior && npm run test:types", - "test:unit": "tap test/unit/*.test.js -J -T", - "test:behavior": "tap test/behavior/*.test.js -J -T", + "test:unit": "tap test/unit/*.test.js -J -t 300", + "test:behavior": "tap test/behavior/*.test.js -J -t 300", "test:integration": "tap test/integration/index.js -T --harmony", "test:types": "tsc --project ./test/types/tsconfig.json", "lint": "standard", diff --git a/test/behavior/sniff.test.js b/test/behavior/sniff.test.js index 97e184126..c6ec1334f 100644 --- a/test/behavior/sniff.test.js +++ b/test/behavior/sniff.test.js @@ -92,6 +92,29 @@ test('Sniff interval', t => { }) }) +test('Sniff on start', t => { + t.plan(4) + + buildCluster(({ nodes, shutdown, kill }) => { + const client = new Client({ + node: nodes[Object.keys(nodes)[0]].url, + sniffOnStart: true + }) + + client.on(events.SNIFF, (err, { hosts, reason }) => { + t.error(err) + t.strictEqual( + client.connectionPool.connections.size, + hosts.length + ) + t.strictEqual(reason, Transport.sniffReasons.SNIFF_ON_START) + }) + + t.strictEqual(client.connectionPool.connections.size, 1) + t.teardown(shutdown) + }) +}) + test('Should not close living connections', t => { t.plan(3) @@ -126,18 +149,29 @@ test('Should not close living connections', t => { test('Sniff on connection fault', t => { t.plan(5) - buildCluster(({ nodes, shutdown }) => { + buildCluster(({ nodes, shutdown, kill }) => { + class MyConnection extends Connection { + request (params, callback) { + if (this.id === 'http://localhost:9200/') { + callback(new Error('kaboom'), null) + return {} + } else { + return super.request(params, callback) + } + } + } + const client = new Client({ nodes: [ - // TODO: this url may cause a flaky test 'http://localhost:9200', nodes[Object.keys(nodes)[0]].url ], maxRetries: 0, - sniffOnConnectionFault: true + sniffOnConnectionFault: true, + Connection: MyConnection }) - t.strictEqual(client.connectionPool.connections.size, 2) + t.strictEqual(client.connectionPool.connections.size, 2) // this event will be triggered by the connection fault client.on(events.SNIFF, (err, { hosts, reason }) => { t.error(err)