service-pool
Advanced tools
Comparing version
| 'use strict' | ||
| const constants = { | ||
| MAX_POOL_SIZE: 10 | ||
| MAX_POOL_SIZE: 0 | ||
| } | ||
| module.exports = constants |
@@ -22,3 +22,3 @@ 'use strict' | ||
| getActiveClientCount () { | ||
| getActiveServiceCount () { | ||
| return Object.keys(this.pool).length | ||
@@ -39,3 +39,3 @@ } | ||
| for (let x in this.pool) { | ||
| this.pool[x].destroy(this.pool[x].client) | ||
| this.pool[x].destroy(this.pool[x].service) | ||
| delete this.pool[x] | ||
@@ -49,15 +49,22 @@ } | ||
| let length = 0 | ||
| let msg = null; | ||
| // Validate entry object returned | ||
| if (typeDetect(entry) !== EnumsTypeDetect.OBJECT) { | ||
| return callback('Invalid JS Object passed as parameter') | ||
| msg = 'Invalid JS Object passed as parameter' | ||
| } else if (!entry.onAdd) { | ||
| return callback('JS Object missing onAdd function call') | ||
| } else if (typeDetect(entry.onAdd) !== EnumsTypeDetect.FUNCTION) { | ||
| return callback('onAdd property not a Function') | ||
| msg = 'JS Object missing onAdd function call' | ||
| } | ||
| if(msg) { | ||
| if(callback) { | ||
| return callback(msg) | ||
| } else { | ||
| return msg | ||
| } | ||
| } | ||
| for (let x in this.pool) { | ||
| if (x === entry.id) { | ||
| // Destroy current client | ||
| // Destroy current service | ||
| this.destroy(x) | ||
@@ -67,3 +74,3 @@ } | ||
| // If max is reached remove last client | ||
| // If max is reached remove last service | ||
| length = Object.keys(this.pool).length | ||
@@ -75,5 +82,5 @@ | ||
| // Add new Client | ||
| // Add new Service | ||
| this.pool[entry.id] = { | ||
| client: entry.onAdd(), | ||
| service: entry.onAdd, | ||
| test: entry.onTest, | ||
@@ -83,3 +90,7 @@ destroy: entry.onDestroy | ||
| return callback(null, this.pool[entry.id].client) | ||
| if(callback) { | ||
| return callback(null, this.pool[entry.id].service) | ||
| } else { | ||
| return this.pool[entry.id].service | ||
| } | ||
| } | ||
@@ -89,3 +100,3 @@ | ||
| if (this.pool[id]) { | ||
| return this.pool[id].client | ||
| return this.pool[id].service | ||
| } else { | ||
@@ -99,3 +110,3 @@ return null | ||
| if (this.pool[id].test) { | ||
| return this.pool[id].test(this.pool[id].client) | ||
| return this.pool[id].test(this.pool[id].service) | ||
| } | ||
@@ -110,3 +121,3 @@ } | ||
| if (this.pool[id].destroy) { | ||
| this.pool[id].destroy(this.pool[id].client) | ||
| this.pool[id].destroy(this.pool[id].service) | ||
| } | ||
@@ -113,0 +124,0 @@ |
| { | ||
| "name": "service-pool", | ||
| "license": "MIT", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "A simple, lightweight, Resource Pool Manager for NodeJS", | ||
@@ -6,0 +6,0 @@ "homepage": "https://agilite.io", |
@@ -18,8 +18,16 @@ 'use strict' | ||
| const service = { | ||
| const service1 = { | ||
| id: id1, | ||
| onAdd: function () { | ||
| const url = 'mongodb://127.0.0.1/dbname' | ||
| return mongoose.createConnection(url) | ||
| onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
| onTest: function (service) { | ||
| return service.readyState === 1 | ||
| }, | ||
| onDestroy: function (service) { | ||
| return service.close() | ||
| } | ||
| } | ||
| const service2 = { | ||
| id: id2, | ||
| onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
| onTest: function (service) { | ||
@@ -33,2 +41,24 @@ return service.readyState === 1 | ||
| const service22 = { | ||
| id: id2, | ||
| onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
| onTest: function (service) { | ||
| return service.readyState === 1 | ||
| }, | ||
| onDestroy: function (service) { | ||
| return service.close() | ||
| } | ||
| } | ||
| const service3 = { | ||
| id: id3, | ||
| onAdd: mongoose.createConnection('mongodb://127.0.0.1/dbname'), | ||
| onTest: function (service) { | ||
| return service.readyState === 1 | ||
| }, | ||
| onDestroy: function (service) { | ||
| return service.close() | ||
| } | ||
| } | ||
| describe ('Mongoose', () => { | ||
@@ -42,3 +72,3 @@ it ('Check Max Prop is valid', (done) => { | ||
| it ('Create First Pool Entry', (done) => { | ||
| pool.add(service, (err, result) => { | ||
| pool.add(service1, (err, result) => { | ||
| expect(err).to.equal(null) | ||
@@ -54,4 +84,4 @@ done() | ||
| it ('Check for 1 Active Client', (done) => { | ||
| expect(pool.getActiveClientCount()).to.equal(1) | ||
| it ('Check for 1 Active Service', (done) => { | ||
| expect(pool.getActiveServiceCount()).to.equal(1) | ||
| done() | ||
@@ -65,4 +95,4 @@ }) | ||
| it ('Check for 0 Active Client', (done) => { | ||
| expect(pool.getActiveClientCount()).to.equal(0) | ||
| it ('Check for 0 Active Service', (done) => { | ||
| expect(pool.getActiveServiceCount()).to.equal(0) | ||
| done() | ||
@@ -72,11 +102,9 @@ }) | ||
| it ('Create 3 Pool Entries', (done) => { | ||
| pool.add(service, (err, result) => { | ||
| pool.add(service1, (err) => { | ||
| expect(err).to.equal(null) | ||
| service.id = id2 | ||
| pool.add(service, (err2, result) => { | ||
| pool.add(service2, (err2) => { | ||
| expect(err2).to.equal(null) | ||
| service.id = id3 | ||
| pool.add(service, (err, result) => { | ||
| pool.add(service3, (err) => { | ||
| expect(err).to.equal(null) | ||
@@ -89,4 +117,4 @@ done() | ||
| it (`Check for ${config.max} Active Client`, (done) => { | ||
| expect(pool.getActiveClientCount()).to.equal(config.max) | ||
| it (`Check for ${config.max} Active Service`, (done) => { | ||
| expect(pool.getActiveServiceCount()).to.equal(config.max) | ||
| done() | ||
@@ -96,5 +124,3 @@ }) | ||
| it ('Add Duplicate Entry', (done) => { | ||
| service.id = id2 | ||
| pool.add(service, (err, result) => { | ||
| pool.add(service22, (err, result) => { | ||
| expect(err).to.equal(null) | ||
@@ -110,8 +136,8 @@ done() | ||
| it (`Check for ${config.max - 1} Active Client`, (done) => { | ||
| expect(pool.getActiveClientCount()).to.equal(config.max - 1) | ||
| it (`Check for ${config.max - 1} Active Service`, (done) => { | ||
| expect(pool.getActiveServiceCount()).to.equal(config.max - 1) | ||
| done() | ||
| }) | ||
| it ('Test 1st Client is Active', (done) => { | ||
| it ('Test 1st Service is Active', (done) => { | ||
| expect(pool.test(id2)).to.equal(true) | ||
@@ -121,3 +147,3 @@ done() | ||
| it ('Get 1st Client', (done) => { | ||
| it ('Get 1st Service', (done) => { | ||
| expect(typeDetect(pool.get(id2))).to.equal(enumsTypeDetect.OBJECT) | ||
@@ -127,3 +153,3 @@ done() | ||
| it ('Reset Clients', (done) => { | ||
| it ('Reset Services', (done) => { | ||
| expect(pool.reset()).to.equal(true) | ||
@@ -133,6 +159,6 @@ done() | ||
| it ('Check for 0 Active Client', (done) => { | ||
| expect(pool.getActiveClientCount()).to.equal(0) | ||
| it ('Check for 0 Active Service', (done) => { | ||
| expect(pool.getActiveServiceCount()).to.equal(0) | ||
| done() | ||
| }) | ||
| }) |
17045
4.02%262
14.91%