From b72a9bf2724f5d62740cf78c6d638a725e30e8ce Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 2 Apr 2024 14:40:17 -0700 Subject: [PATCH 1/2] cleaned up PR #59 - index: turn off watchForUpdates, due to warning: - (node:52697) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 change listeners added to [StatWatcher]. Use emitter.setMaxListeners() to increase limit - pin dependency versions - use correct assert syntax --- index.js | 4 ++-- package.json | 14 +++++++------- test/geoip.js | 25 +++++++++++++++++++------ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 95ccbb2..26f0dae 100644 --- a/index.js +++ b/index.js @@ -88,8 +88,8 @@ exports.load_dbs = async function () { } this[`${db}Lookup`] = await this.maxmind.open(dbPath, { - // this causes tests to hang, which is why mocha runs with --exit - watchForUpdates: true, + // watchForUpdates causes tests to hang unless mocha runs with --exit + watchForUpdates: false, cache: { max: 1000, // max items in cache maxAge: 1000 * 60 * 60 // life time in milliseconds diff --git a/package.json b/package.json index 15d629a..8402fa0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "node": ">=14" }, "scripts": { - "test": "npx mocha --exit", + "test": "npx mocha", "lint": "npx eslint index.js test/*.js", "lintfix": "npx eslint --fix index.js test/*.js", "cover": "NODE_ENV=cov npx nyc --reporter=lcovonly npm run test", @@ -34,13 +34,13 @@ }, "homepage": "https://github.com/haraka/haraka-plugin-geoip#readme", "devDependencies": { - "eslint": ">=8", - "eslint-plugin-haraka": "*", - "haraka-test-fixtures": "*", - "mocha": "*" + "eslint": "^8.57.0", + "eslint-plugin-haraka": "^1.0.15", + "haraka-test-fixtures": "^1.3.3", + "mocha": "^10.4.0" }, "dependencies": { - "maxmind": "^4.3.8", - "haraka-net-utils": "*" + "maxmind": "^4.3.18", + "haraka-net-utils": "^1.5.4" } } diff --git a/test/geoip.js b/test/geoip.js index 44468e2..e1af7d6 100644 --- a/test/geoip.js +++ b/test/geoip.js @@ -13,23 +13,20 @@ describe('register', function () { this.plugin.register().then(done) }) - it('config loaded', function (done) { + it('config loaded', function () { assert.ok(this.plugin.cfg) assert.ok(this.plugin.cfg.main) - done() }) if (plugin_name === 'geoip') { - it('maxmind module loaded', function (done) { + it('maxmind module loaded', function () { assert.ok(this.plugin.maxmind) - done() }) } if (plugin_name === 'geoip-lite') { - it('geoip-lite module loads', function (done) { + it('geoip-lite module loads', function () { assert.ok(this.plugin.geoip) - done() }) } }) @@ -160,3 +157,19 @@ describe('haversine', function () { done() }) }) + +describe('received_headers', function () { + beforeEach(async function () { + this.plugin = new fixtures.plugin('geoip') + await this.plugin.register() + this.connection = fixtures.connection.createConnection() + this.connection.transaction = fixtures.transaction.createTransaction() + }) + + it('generates results for each received header', function () { + this.connection.transaction.header.add_end('Received', 'from [199.176.179.3]') + this.connection.transaction.header.add_end('Received', 'from [192.48.85.146]') + const results = this.plugin.received_headers(this.connection) + assert.equal(results.length, 2) + }) +}) \ No newline at end of file From 75e75f7a4cc3788dc1893b2a6c59006ee919d5a0 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 2 Apr 2024 15:13:35 -0700 Subject: [PATCH 2/2] load the DBs --- test/geoip.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/test/geoip.js b/test/geoip.js index e1af7d6..1140207 100644 --- a/test/geoip.js +++ b/test/geoip.js @@ -8,9 +8,9 @@ const fixtures = require('haraka-test-fixtures') const plugin_name = 'geoip' describe('register', function () { - beforeEach(function (done) { + beforeEach(async function () { this.plugin = new fixtures.plugin('geoip') - this.plugin.register().then(done) + await this.plugin.register() }) it('config loaded', function () { @@ -32,34 +32,28 @@ describe('register', function () { }) describe('database lookups', function () { - beforeEach(function (done) { + beforeEach(async function () { this.plugin = new fixtures.plugin('geoip') - this.plugin.register().then(() => { - this.connection = fixtures.connection.createConnection() - }) + await this.plugin.register() + this.connection = fixtures.connection.createConnection() if (plugin_name === 'geoip') { this.plugin.cfg.main.dbdir = path.resolve('test','fixtures') - this.plugin.load_dbs().then(done) - } - else { - done() + await this.plugin.load_dbs() } }) describe('get_geoip', function () { - it('no IP fails', function (done) { + it('no IP fails', function () { assert.ok(!this.plugin.get_geoip()) - done() }) - it('ipv4 private fails', function (done) { + it('ipv4 private fails', function () { assert.ok(!this.plugin.get_geoip('192.168.2.3')) - done() }) - it('ipv4 public passes', function (done) { + it('ipv4 public passes', function () { const r = this.plugin.get_geoip('192.48.85.146') if (plugin_name === 'geoip') { assert.equal(r.continent.code, 'NA') @@ -68,15 +62,13 @@ describe('database lookups', function () { if (plugin_name === 'geoip-lite') { assert.equal(r.country, 'US') } - done() }) if (plugin_name === 'geoip') { - it('ipv6 public passes', function (done) { + it('ipv6 public passes', function () { const r = this.plugin.get_geoip('2607:f060:b008:feed::6') assert.equal(r.continent.code, 'NA') assert.equal(r.country.iso_code, 'US') - done() }) } }) @@ -164,6 +156,11 @@ describe('received_headers', function () { await this.plugin.register() this.connection = fixtures.connection.createConnection() this.connection.transaction = fixtures.transaction.createTransaction() + + if (plugin_name === 'geoip') { + this.plugin.cfg.main.dbdir = path.resolve('test','fixtures') + await this.plugin.load_dbs() + } }) it('generates results for each received header', function () {